【发布时间】:2019-01-31 07:12:04
【问题描述】:
我正在尝试使用张量 b 中定义的索引访问张量 a 的元素。
a=tf.constant([[1,2,3,4],[5,6,7,8]])
b=tf.constant([0,1,1,0])
我希望输出是
out = [1 6 7 4]
我尝试过的:
out=[]
for i in range(a.shape[1]):
out.append(a[b[i],i])
out=tf.stack(out) #[1 6 7 4]
这给出了正确的输出,但我正在寻找一种更好、更紧凑的方法。
当a 的形状类似于(2,None) 时,我的逻辑也不起作用,因为我无法使用range(a.shape[1]) 进行迭代,如果答案也包括这种情况,这将对我有所帮助
谢谢
【问题讨论】:
标签: python python-3.x tensorflow