开发者社区> 问答> 正文

我如何在tensorflow中使用索引数组?

如果给定的矩阵a与形状(5,3)和索引数组b具有形状(5,),我们可以很容易地得到相应的向量c通过,

c = a[np.arange(5), b]

但是,我不能用张量流做同样的事情,

a = tf.placeholder(tf.float32, shape=(5, 3))
b = tf.placeholder(tf.int32, [5,])
# this line throws error
c = a[tf.range(5), b]

展开
收起
祖安文状元 2020-02-22 16:05:31 1477 0
2 条回答
写回答
取消 提交回答
  • TF当前不支持啊

    2020-03-14 14:29:21
    赞同 展开评论 打赏
  • TensorFlow当前未实现此功能。GitHub 问题#4638正在跟踪NumPy样式的“高级”索引的实现。但是,您可以使用tf.gather_nd()运算符来实现您的程序:

    a = tf.placeholder(tf.float32, shape=(5, 3))
    b = tf.placeholder(tf.int32, (5,))
    
    row_indices = tf.range(5)
    
    # `indices` is a 5 x 2 matrix of coordinates into `a`.
    indices = tf.transpose([row_indices, b])
    
    c = tf.gather_nd(a, indices)
    
    2020-02-22 16:05:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载