I am new to to machine learning and am trying a simple program with keras. When I run the following code, I get an error saying "ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape [32, 28, 28]". Could someone help me with this? I am trying to follow along with this video:https://www.youtube.com/watch?v=tjsHSIG8I08, but it doesnt seem to work. Thanks in advance!
import tensorflow as tf
import numpy as np
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(512, activation=tf.nn.relu, input_shape=(784,)))
model.add(tf.keras.layers.Dense(256, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit(train_images, train_labels, epochs=5)
loss, accuracy = model.evaluate(test_images, test_labels)
print('Accuracy', accuracy)
scores = model.predict(test_images[0:1])
print(np.argmax(scores))
The problem come from input_shape=(784,)
. First you need to understand how input dimension works. Input data must follow this dimension (number of sample, input dimension), so in your case with (32, 28, 28) which is a mnist datasets dimension of (28 X 28) with number of sample equal to 32. Therefore, you must change to input_shape=(28,28)
and you are good to go.