我有两个张量,形状如下:
tensor1 => shape(10, 99, 106)
tensor2 => shape(10, 99)
tensor2
包含从0 - 105
到tensor1
的值,我希望使用这些值来剖切tensor1
的最后一个维度并获得形状的tensor3
tensor3 => shape(10, 99, 99)
我试过使用:
tensor4 = tf.gather(tensor1, tensor2)
# this causes tensor4 to be of shape (10, 99, 99, 106)
另外,使用
tensor4 = tf.gather_nd(tensor1, tensor2)
# gives the error: last dimension of tensor2 (which is 99) must be
# less than the rank of the tensor1 (which is 3).
我要找的东西类似于numpy的交叉索引
您可以使用^{} :
你可以把这一行看作是一个循环,它在
tensor1
和tensor2
的第一个维度上运行,对于它们的第一个维度中的每个索引i
,它在tensor1[i,:,:]
和tensor2[i,:]
上应用tf.gather
相关问题 更多 >
编程相关推荐