【问题标题】:tensorflow scan on a matrix对矩阵进行张量流扫描
【发布时间】:2018-04-17 13:47:02
【问题描述】:

如何使以下扫描示例起作用?我打算用这个例子来测试函数f中的一些代码。

def f(prev_y, curr_y):
  fval = tf.nn.softmax(curr_y)
  return fval

a = tf.constant([[.1, .25, .3, .2, .15],
                 [.07, .35, .27, .17, .14]])
c = tf.scan(f, a, initializer=0)
with tf.Session() as sess:
  print(sess.run(c))

【问题讨论】:

    标签: python tensorflow scanning


    【解决方案1】:

    您的initializer=0 无效。作为documented

    如果提供了initializer,则fn的输出必须与initializer具有相同的结构;并且fn 的第一个参数必须匹配这个结构。

    f 的输出与第二个参数curr_y 具有相同的类型和形状,与0 不匹配。在这种情况下,您需要:

    init = tf.constant([0., 0., 0., 0., 0.])
    c = tf.scan(f, a, initializer=init)
    

    对于这种特定情况(特别是您的f),您不需要(也许不应该)使用tf.scan,因为您的f 仅使用一个参数。 tf.map_fn 可以胜任:

    c = tf.map_fn(tf.nn.softmax, a)
    

    【讨论】:

      猜你喜欢
      • 2018-05-24
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2019-08-04
      相关资源
      最近更新 更多