BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / python / #22120同步于 2018/6/9
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖

求助,tensorflow数组索引

cheng1997
2018/6/9镜像同步6 回复
现在有a = [[1,2,3], [4,5,6], [7,8,9]], b = [0,2,1]. 要求对a中的每一行用b中的数去索引,得到[1,2,3]中的第0个数,[4,5,6]中的第2个数, [7,8,9]中的第1个数。也就是想得到[1,6,8]的结果。想请问一下,这个结果在a和b为np.array的时候要怎么得到呢?a和b为tensor的时候又有什么方法呢?[ema1]
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
byrssss机器人#1 · 2018/6/9
np.choose(b, a.T) https://stackoverflow.com/a/17081678/3100697
cheng1997机器人#2 · 2018/6/9
谢谢!学习到了!但是这个方法好像不能用于tensor上。 In [13]: c = tf.constant(a.T) In [14]: d = tf.constant(b) In [15]: np.choose(d,c) TypeError: object of type 'Tensor' has no len() 【 在 byrssss 的大作中提到: 】 : np.choose(b, a.T) : https://stackoverflow.com/a/17081678/3100697
byrssss机器人#3 · 2018/6/9
不懂tensorflow [ema1][ema1][ema1] 【 在 cheng1997 的大作中提到: 】 : 谢谢!学习到了!但是这个方法好像不能用于tensor上。 : In [13]: c = tf.constant(a.T) : In [14]: d = tf.constant(b) : ...................
jaegerstar机器人#4 · 2018/6/9
可以先tensor转成numpy试试?
moonfighting机器人#5 · 2018/6/9
gather_nd https://www.tensorflow.org/api_docs/python/tf/gather_nd
w86568376机器人#6 · 2018/6/10
五楼正解