I am learning TensorFLow. So to understand how to make something, I tried to copy some code from a source and execute it. But I\'m hitting an error message. So I tried some
You need to reshape X.
X = tf.placeholder(tf.float32 , [None ,28 , 28 , 1])
X = tf.reshape(X , [-1 , 784])
When you define a placeholder in TensorFlow, the shape of the input during the session should be the same as the shape of the placeholder.
In batch_X, batch_Y = mnist.train.next_batch(100)
, the batch_x
is a 2D array of pixel values, which will have a shape of [batch_size, 28*28]
.
In X = tf.placeholder(tf.float32,[None, 28, 28, 1])
, the input placeholder is defined to have a 4D shape of [batch_size, 28, 28, 1]
You can either 1) reshape batch_x
before the feeding to TensorFlow. e.g.batch_x = np.reshape(batch_x, [-1, 28, 28, 1])
or 2) Change the shape of the placeholder. e.g. X = tf.placeholder(tf.float32,[None, 784])
I would recommend 2), since this saves you from doing any reshaping operations both in and outside of the TensorFlow graph.
You are getting that error because there is a mismatch between the shape of what you are feeding in and what the TensorFlow is expecting. To fix the issue, you might want to reshape your data at placeholder:0
which is batch_X
to
(?, 28, 28, 1)
. For example, you would do the following:
batch_X = np.reshape(batch_X, (-1, 28, 28, 1))