【问题标题】:Which values to use when feeding placeholders in Tensorflow?在 Tensorflow 中输入占位符时使用哪些值?
【发布时间】:2020-04-27 02:24:25
【问题描述】:

在下面的代码中,有很多张量运算和计算。我想看看其中一些计算的结果,以便更好地理解它们。具体来说,我想看看在使用print(Session.Run(h)) 执行图形期间h 的样子。但是,计算取决于占位符 X。因此,为了查看它们,我需要使用提要字典。

我已经阅读了这个 SO 问题:How to feed a placeholder? 和其他几个问题。我仍然不知道我应该在这个占位符中输入什么。

要查看 h 的值,在尝试打印时我应该如何或者更确切地说应该将什么放入 feed 字典?

def expand_tile(value, size):
    """Add a new axis of given size."""
    value = tf.convert_to_tensor(value, name='value')
    ndims = value.shape.ndims
    return tf.tile(tf.expand_dims(value, axis=0), [size] + [1]*ndims)

def positions_for(tokens, past_length):
    batch_size = tf.shape(tokens)[0]
    nsteps = tf.shape(tokens)[1]
    return expand_tile(past_length + tf.range(nsteps), batch_size)


def model(hparams, X, past=None, scope='model', reuse=tf.AUTO_REUSE):
    with tf.variable_scope(scope, reuse=reuse):
        results = {}

        batch_size = 1
        X = tf.placeholder(tf.int32, [batch_size, None])

        batch, sequence = shape_list(X)

        wpe = tf.get_variable('wpe', [1024, 768],
                             initializer=tf.random_normal_initializer(stddev=0.01))
        wte = tf.get_variable('wte', [50256, 768],
                             initializer=tf.random_normal_initializer(stddev=0.02))
        past_length = 0 if past is None else tf.shape(past)[-2]
        h = tf.gather(wte, X) + tf.gather(wpe, positions_for(X, past_length))

【问题讨论】:

  • 使用像this这样的交互式会话。
  • 您好。谢谢你的帮助。我确实尝试过使用 tf.InteractiveSession() 和 print(h.eval())。我收到错误消息说我需要将值提供给形状为 (1, ?) 的占位符 X。这就是你的意思吗?

标签: python tensorflow


【解决方案1】:

当您使用交互式会话时,您可以在 python x = 57 中设置值,完全绕过占位符,然后根据需要评估图表的其余部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2017-01-02
    • 2017-07-27
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    相关资源
    最近更新 更多