How to batch inputs together for tensorflow?

一世执手 提交于 2020-01-14 03:55:21

问题


I'm trying to batch together the inputs for a neural network I'm working on so I can feed them into tensorflow like in the tensorflow MNIST tutorial. However I can't find anyway of doing this and it isn't covered in the tutorial.

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})

This will throw the following error:

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

I understand that why I'm getting the above error, I just don't know how to get tensorflow to treat my inputs as a batch of inputs rather than think I'm trying to feed it all in as one shape.

Thanks very much for any help!


回答1:


You need to modify the signature of your placeholder. Let's break down the error message:

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

your inputs variable is the one that has shape (50, 10, 10) that means 50 elements of shape (10, 10) and Tensor Placeholder:0 is your input variable. If you print (input.name you will get the value Placeholder:0. Cannot feed value means that it cannot assign inputs to input.

A first quick solution is to fix the shape of the placeholder input to

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

but each time you want to modify the size of the batch you will need to update the batch size in your input. A better way to specify the batch size is to put a undefined shape dimension for the batch size using None:

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

This now will works with any batch size, from 1 to the hardware limits of your architecture.




回答2:


You would have to setup your input differently to have shape [batch_size, 10, 10], and use the parameter reduction_indices when calling tf.reduce_mean(). As an aside, tf.contrib.learn.read_batch_examples() would take care of batching the examples using a batch queue, see documentation here.



来源:https://stackoverflow.com/questions/40685452/how-to-batch-inputs-together-for-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!