0

I'm started on tensorflow and I'm trying to read handwritten letters from a MNIST. I've got an error in my code but I don't understand why. I've found a post which is similar to this one but i've got the same error with this code. (link of this topic TensorFlow Cannot feed value of shape (100, 784) for Tensor 'Placeholder:0')

enter code here import tensorflow as tf
import numpy as np
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.global_variables_initializer()
#Model
Y = tf.nn.softmax(tf.matmul(tf.reshape(X,[-1,784]),W)+B)
#Placeholder for correct answer
Y_ = tf.placeholder(tf.float32,[None,10])
#Calcul de l'erreur
cross_entropy = -tf.reduce_sum(Y_ * tf.log(Y))  
# pourcentage de bonne réponse
is_correct = tf.equal(tf.argmax(Y,1),tf.argmax(Y_,1))
accuracy = tf.reduce_mean(tf.cast(is_correct,tf.float32))

#Regression linéaire

optimizer = tf.train.GradientDescentOptimizer(0.003)
train_step = optimizer.minimize(cross_entropy)
#Training process


sess = tf.Session()
sess.run(init)  


for i in range(1000):
    #On charge les images
    batch_X,batch_Y = mnist.train.next_batch(100)
    batch_X = np.reshape(batch_X, (-1, 28, 28, 1))
    train_data = {X: batch_X, Y_: batch_Y}
#train
sess.run(train_step, feed_dict = train_data)
#success ? 
a,c = sess.run([accuracy,cross_entropy],feed_dict = train_data) 

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

1 Answers1

1

Change last lines to:

test_images = np.reshape(mnist.test.images, (-1, 28, 28, 1))
test_data = {X:mnist.test.images, Y_:test_images}
a,c = sess.run([accuracy, cross_entropy],feed_dict=test_data)
Vladimir Bystricky
  • 1,320
  • 1
  • 11
  • 13
  • Thank you so much, I've got a new error : ValueError: Cannot feed value of shape (10000, 784) for Tensor 'Placeholder_9:0', which has shape '(?, 28, 28, 1)' – Matthieu Rousseau Oct 22 '17 at 17:51