【发布时间】:2023-03-15 22:35:01
【问题描述】:
假设一个列表包含需要通过查找表访问的不可连接对象。所以列表索引将是一个张量对象,但这是不可能的。
tf_look_up = tf.constant(np.array([3, 2, 1, 0, 4]))
index = tf.constant(2)
list = [0,1,2,3,4]
target = list[tf_look_up[index]]
这将显示以下错误消息。
TypeError: list indices must be integers or slices, not Tensor
是用张量索引列表的方法/解决方法吗?
【问题讨论】:
-
先使用sess.run将张量转为numpy
-
@YaroslavBulatov 如果列表是动态生成的,例如RNN 产生的状态。任何方式列表动态索引都可以工作吗?
-
也许
tf.gather就像@soloice 的回答一样?
标签: python list indexing tensorflow