gopinathbalu
gopinathbalu

Reputation: 43

Python - TensorFlow/tf ValueError: Cannot feed value of shape (100, 784) for Tensor 'Placeholder:0', which has shape '(?, 28, 28, 1)'

I just started learning deep learning with tensorflow, so I wanted to implement a tutorial which I read today, link . And the actual code is this, I have tried to implement a very basic NN to classify digits from 0-9.

import tensorflow as tf
# load dataset
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)

X = tf.placeholder(tf.float32, [None, 28, 28, 1])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

init = tf.initialize_all_variables()

# model
Y = tf.nn.softmax(tf.matmul(tf.reshape(X,[-1, 784]), W) + b)
# placeholder fr correct labels
Y_ = tf.placeholder(tf.float32, [None, 10])

# loss function 
cross_entropy = -tf.reduce_sum(Y_ * tf.log(Y))

# % of a correct answer found in batch
is_correct = tf.equal(tf.argmax(Y,1), tf.argmax(Y_,1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))


# optimizer, workhorse of a NN
optimizer = tf.train.GradientDescentOptimizer(0.003)
train_step = optimizer.minimize(cross_entropy)

# start the session
sess = tf.Session() 
sess.run(init)

# feed the data for training
for i in range(10000):
    # load batch of images and correct answers
    batch_X, batch_Y = mnist.train.next_batch(100)
    train_data={X: batch_X, Y_: batch_Y}

    # train
    sess.run(optimizer, feed_dict=train_data)

    # succes ?
    a,c = sess.run([accuracy, cross_entropy], feed=train_data)

    # success on test data
    test_data={X: mnist.test.images, Y_: mnist.test.labels}
    a,c = sess.run([accuracy, cross_entropy], feed=test_data)

But when I try to run this I get error like this,

I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties: 
name: GeForce 840M
major: 5 minor: 0 memoryClockRate (GHz) 1.124
pciBusID 0000:03:00.0
Total memory: 1.96GiB
Free memory: 1.72GiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce 840M, pci bus id: 0000:03:00.0)
Traceback (most recent call last):
  File "mnist_v1.py", line 41, in <module>
    sess.run(optimizer, feed_dict=train_data)
  File "/home/gopi34/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/home/gopi34/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 944, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (100, 784) for Tensor 'Placeholder:0', which has shape '(?, 28, 28, 1)'

Upvotes: 0

Views: 917

Answers (1)

Giuseppe Marra
Giuseppe Marra

Reputation: 1104

You should use a placeholder of size None x 784, i.e. linearize each image.

Upvotes: 1

Related Questions