【问题标题】:How to batch inputs together for tensorflow?如何为张量流批量输入?
【发布时间】:2016-11-19 04:56:27
【问题描述】:

我正在尝试将我正在研究的神经网络的输入批量处理,这样我就可以像在 tensorflow MNIST 教程中那样将它们输入到 tensorflow 中。但是,我无论如何都找不到这样做,并且教程中没有介绍。

input = tf.placeholder(tf.float32, [10, 10])
...
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
inputs = #A list containing 50 of the inputs
sess.run(accuracy, feed_dict={input: inputs})

这将引发以下错误:

ValueError: Cannot feed value of shape (50, 10, 10) for Tensor'Placeholder:0', which has shape '(10, 10)'

我明白为什么会出现上述错误,我只是不知道如何让 tensorflow 将我的输入视为一批输入,而不是认为我正在尝试将其全部输入为一个形状。

非常感谢您的帮助!

【问题讨论】:

    标签: python neural-network tensorflow


    【解决方案1】:

    您需要修改占位符的签名。让我们分解错误消息:

    ValueError: Cannot feed value of shape (50, 10, 10) for 
    Tensor'Placeholder:0', which has shape '(10, 10)'
    

    您的inputs 变量是具有(50, 10, 10) 形状的变量,这意味着50 形状为(10, 10) 的元素和张量Placeholder:0 是您的input 变量。如果你打印 (input.name 你会得到值Placeholder:0Cannot feed value 意味着它不能将inputs 分配给input

    第一个快速解决方案是将占位符input 的形状固定为

    input = tf.placeholder(tf.float32, [50, 10, 10])
    

    但每次您想修改批次大小时,您都需要在输入中更新批次大小。 指定批量大小的更好方法是使用 None 为批量大小设置未定义的形状尺寸:

    input = tf.placeholder(tf.float32, [None, 10, 10])
    

    这现在适用于任何批量大小,从 1 到您架构的硬件限制。

    【讨论】:

    • 非常感谢 [None, 10, 10] 这正是我想要的。这实际上是在教程中,它只是没有在我的脑海中点击,这就是它的用途。
    【解决方案2】:

    您必须以不同的方式设置输入以获得形状 [batch_size, 10, 10],并在调用 tf.reduce_mean() 时使用参数 reduction_indices。顺便说一句,tf.contrib.learn.read_batch_examples() 将负责使用批处理队列对示例进行批处理,请参阅文档here

    【讨论】:

    • 非常感谢!!
    猜你喜欢
    • 2018-04-04
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 2018-10-31
    • 2019-02-10
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多