【问题标题】:how to gather element with index in tensorflow如何在张量流中收集具有索引的元素
【发布时间】:2018-08-04 22:46:29
【问题描述】:

例如,

import tensorflow as tf

index = tf.constant([[1],[1]])
values = tf.constant([[0.2, 0.8],[0.4, 0.6]])

如果我使用extract = tf.gather_nd(values, index) 回报是

[[0.4 0.6]
 [0.4 0.6]]

但是,我想要的结果是

[[0.8], [0.6]]

索引沿axis = 1的地方,但是tf.gather_nd中没有axis参数设置。

我该怎么办?谢谢!

【问题讨论】:

  • 嗨,我是 Google 的一名软件工程师,我想在我们正在撰写的一篇研究论文中引用这个问题,以展示一些关于 TensorFlow 的代表性问题。你会这样吗?谢谢,大卫
  • @DavidBieber 当然,感谢您的提问 :) 也感谢您构建这个神奇的工具

标签: python tensorflow tensor


【解决方案1】:

将范围连接到index

index = tf.stack([tf.range(index.shape[0])[:, None], index], axis=2)
result = tf.gather_nd(values, index)

result.eval(session=tf.Session())
array([[0.8],
       [0.6]], dtype=float32)

【讨论】:

    猜你喜欢
    • 2018-02-13
    • 2017-10-12
    • 2019-09-16
    • 1970-01-01
    • 2021-09-14
    • 2018-07-02
    • 1970-01-01
    • 2018-04-02
    • 2018-06-25
    相关资源
    最近更新 更多