Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tensorflow中tensor对象的非连续索引切片(高级索引,如numpy)_Python_Tensorflow_Keras_Slice - Fatal编程技术网

Python tensorflow中tensor对象的非连续索引切片(高级索引,如numpy)

Python tensorflow中tensor对象的非连续索引切片(高级索引,如numpy),python,tensorflow,keras,slice,Python,Tensorflow,Keras,Slice,我研究了tensorflow中不同的切片方法,即tf.gather和tf.gather\n。 在tf.gather中,它只在维度上切片,在tf.gather nd中,它只接受一个索引应用于输入张量 我需要的是不同的,我想用两个不同的张量对输入张量进行切片;一个在行上切片,第二个在列上切片,它们的形状不一定相同 例如: 假设这是我的输入张量,我想从中提取一部分 input_tf = tf.Variable([ [9.968594, 8.655439, 0., 0. ]

我研究了tensorflow中不同的切片方法,即
tf.gather
tf.gather\n
。 在tf.gather中,它只在维度上切片,在
tf.gather nd
中,它只接受一个
索引
应用于输入张量

我需要的是不同的,我想用两个不同的张量对输入张量进行切片;一个在行上切片,第二个在列上切片,它们的形状不一定相同

例如:

假设这是我的输入张量,我想从中提取一部分

input_tf = tf.Variable([ [9.968594,  8.655439,  0.,        0.       ],
                         [0.,        8.3356,    0.,        8.8974   ],
                         [0.,        0.,        6.103182,  7.330564 ],
                         [6.609862,  0.,        3.0614321, 0.       ],
                         [9.497023,  0.,        3.8914037, 0.       ],
                         [0.,        8.457685,  8.602337,  0.       ],
                         [0.,        0.,        5.826657,  8.283971 ],
                         [0.,        0.,        0.,        0.       ]])
二是:

 rows_tf = tf.constant (
[[1, 2, 5],
 [1, 2, 5],
 [1, 2, 5],
 [1, 4, 6],
 [1, 4, 6],
 [2, 3, 6],
 [2, 3, 6],
 [2, 4, 7]])
第三张量:

columns_tf = tf.constant(
[[1],
 [2],
 [3],
 [2],
 [3],
 [2],
 [3],
 [2]])
现在,我想使用
rows\u tf
columns\u tf
input\u tf
进行切片。索引行中的
[1 2 5]
和列中的
[1]
。同样,在
列中使用
[2]
的行
[1 2 5]

或者,
[1 4 6]
带有
[2]

总的来说,
行中的每个索引都将提取部分
输入内容

因此,预期输出为:

[[8.3356,    0.,        8.457685 ],
 [0.,        6.103182,  8.602337 ],
 [8.8974,    7.330564,  0.       ],
 [0.,        3.8914037, 5.826657 ],
 [8.8974,    0.,        8.283971 ],
 [6.103182,  3.0614321, 5.826657 ],
 [7.330564,  0.,        8.283971 ],
 [6.103182,  3.8914037, 0.       ]]
例如,这里第一行
[8.3356,0.,8.457685]
是使用

rows in rows_tf [1,2,5] and column in columns_tf [1](row 1 and column 1, row 2 and column 1 and row 5 and column 1 in the input_tf)
尽管他们使用了
tf.gather
tf.gather-nd
tf.stack
,但在tensorflow中有几个关于切片的问题,它没有给出我想要的输出

无需提及,在
numpy
中,我们可以通过调用:
input\u tf[rows\u tf,columns\u tf]
轻松实现这一点

我还研究了这个高级索引,它试图模拟numpy中可用的高级索引,但是它仍然不像numpy那样灵活

这是我尝试过的不正确的方法:

tf.gather(tf.transpose(tf.gather(input_tf,rows_tf)),columns_tf)
此代码的尺寸输出为
(8,1,3,8)
,完全不正确


提前谢谢

思想是首先将稀疏索引(通过连接行索引和列索引)作为列表获取。然后,您可以使用
聚集\u nd
检索值


tf.reset_default_graph()
输入_tf=tf.变量([[9.968594,8.655439,0,0.],
[0.,        8.3356,    0.,        8.8974   ],
[0.,        0.,        6.103182,  7.330564 ],
[6.609862,  0.,        3.0614321, 0.       ],
[9.497023,  0.,        3.8914037, 0.       ],
[0.,        8.457685,  8.602337,  0.       ],
[0.,        0.,        5.826657,  8.283971 ],
[0.,        0.,        0.,        0.       ]])
行_tf=tf.constant(
[[1, 2, 5],
[1, 2, 5],
[1, 2, 5],
[1, 4, 6],
[1, 4, 6],
[2, 3, 6],
[2, 3, 6],
[2, 4, 7]])
列_tf=tf.常数(
[[1],
[2],
[3],
[2],
[3],
[2],
[3],
[2]])
rows\u tf=tf.重塑(rows\u tf,shape=[-1,1])
列_tf=tf.重塑(
tile(列_-tf,倍数=[1,3]),
形状=[-1,1])
稀疏_索引=tf.reformate(
concat([行,列],轴=-1),
形状=[-1,2])
v=tf.聚集(输入、稀疏索引)
v=tf.重塑(v,[-1,3])
使用tf.Session()作为sess:
sess.run(tf.initialize\u all\u variables())
#打印'rows\n',sess.run(rows\u tf)
#打印'columns\n',sess.run(columns\u tf)
打印sess.run(v)
结果将是:

[[ 8.3355999   0.          8.45768547]
 [ 0.          6.10318184  8.60233688]
 [ 8.8973999   7.33056402  0.        ]
 [ 0.          3.89140368  5.82665682]
 [ 8.8973999   0.          8.28397083]
 [ 6.10318184  3.06143212  5.82665682]
 [ 7.33056402  0.          8.28397083]
 [ 6.10318184  3.89140368  0.        ]]

您应该编辑您的问题,以便所有常量的格式都正确(添加
)@DSC您是对的,我现在就做,谢谢您为什么对您提到的“聚集”操作的输出不满意?听起来好像可以。是不是因为它会变平?如果是这样的话,你可以在知道“行”和“列”的尺寸的情况下对其进行整形,一旦你有了
scatter\u idx
,你就应该能够使用
tf.gather\nd(params=input\u tf,index=scatter\u idx)
,然后是
tf.revorme
,以获得你想要的形状。我复制了下面的完整代码作为答案,您可以使用其他线程中任何其他答案的类似方式来获取稀疏索引。我真诚地感谢您的帮助:)np,享受tf的乐趣。:)我这里有一个困惑,听起来像是一个逻辑问题。正是这段代码运行良好,当我在程序中包含这段代码时,它的输出不正确。我找到了问题的根源,但对我来说毫无意义。我想知道你对此是否有任何想法<代码>行_tf
和列_tf
tf.constant'中给出,但在我的代码中,它们是其他过程的结果。如果我给它
tf.constant`就像这里一样,它工作得很好,但是一旦我改变了它,而不是
tf.constant
它的输出将是不正确的,就像我上面报告的一样。为什么会这样?您是否正确获取了
行\u tf
列\u tf
?您可能希望包含所有代码以进行澄清。请随时在这里更新您的问题,或者开始一个新的线程。我很高兴马上看一看。很高兴知道这一点。