如何使用TensorFlow张量索引列表? [英] How to index a list with a TensorFlow tensor?

查看:1265
本文介绍了如何使用TensorFlow张量索引列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个包含不可连接对象的列表需要通过查找表进行访问。因此列表索引将是张量对象,但这是不可能的。

Assume a list with non concatenable objects which needs to be accessed via a look up table. So the list index will be a tensor object but this is not possible.

 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]]

这将显示以下错误消息。

This will bring out the following error message.

 TypeError: list indices must be integers or slices, not Tensor

这是使用张量索引列表的方式/解决方法吗?

Is the a way/workaround to index lists with tensors?

推荐答案

tf.gather 专为此目的而设计。

只需运行 tf.gather (list,tf_look_up [index]),你会得到你想要的。

Simply run tf.gather(list, tf_look_up[index]), you'll get what you want.

这篇关于如何使用TensorFlow张量索引列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆