【问题标题】:Tensorflow gather_nd over input with varied shapeTensorflowgather_nd 不同形状的输入
【发布时间】:2018-03-21 21:01:25
【问题描述】:

我需要根据另一个张量的值沿axis=1 提取子张量。

state = tf.placeholder(shape=[None, None, 10], dtype=tf.float32)
length = tf.placeholder(shape=[None], dtype=tf.int32)

# this won't work, just be put here to demonstrate what I need
next_init_state = state[:, length - 1, :]

如果statelength具有确定的形状,那么next_init_state可以通过gather_nd导出

state = tf.placeholder(shape=[10, 10, 10], dtype=tf.float32)
length = tf.placeholder(shape=[10], dtype=tf.int32)

index = tf.stack([tf.range(0, 10), length])
next_init_state = tf.gather_nd(state, index)

但是,由于状态和长度在我遇到的问题中都具有不确定的形状None,因此gather_nd 方法将不起作用。至少我想不出一种使它起作用的方法。有什么办法可以解决吗?

【问题讨论】:

    标签: python tensorflow tensor


    【解决方案1】:

    我意识到这实际上是通过 Tensorflow 的高阶函数解决的。

    tf.map_fn(lambda x: x[0][x[1], :], (state, length), dtype=tf.float32)
    

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 2019-01-22
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多