Tensorflow - shape error when reading data from file
<p>I am trying to train a single layer perceptron (basing my code on <a href="https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py" rel="nofollow">this</a>) on the following data file in tensor flow:</p>

<pre><code>1,1,0.05,-1.05
1,1,0.1,-1.1
....
</code></pre>

<p>where the last column is the label (function of 3 parameters) and the first three columns are the function argument. The code that reads the data and trains the model (I simplify it for readability):</p>

<pre><code>import tensorflow as tf

... # some basics to read the data
example, label = read_file_format(filename_queue)
... # model construction and parameter setting

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    for epoch in range(training_epochs):
        _, c = sess.run([optimizer, cost], feed_dict={x: example, y: label})
    print("Optimization Finished!")
</code></pre>

<p>but when I run it, it gives the following error:</p>

<pre><code>Traceback (most recent call last):
  File "nn.py", line 85, in &lt;module&gt;
    _, c = sess.run([optimizer, cost], feed_dict={x: example, y: label})
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 710, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 887, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (3,) for Tensor u'Placeholder:0', which has shape '(?, 3)'
</code></pre>

<p>Your graph expects X to be a tensor of shape (?, 3). Your example data is of the shape (3,) i.e. a 1 dimensional vector of length 3. Either reshape example to (1, 3), or pass a batch of examples in one shot (e.g. 10, giving a shape of (10, 3))</p>

python tensorflow