Reputation: 21
I am working on the mnist classification code. Such errors continue to occur in the code below.
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(units=10, input_dim=784, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=tf.optimizers.Adam(learning_rate=0.001), metrics=['accuracy'])
model.summary()
model.fit(x_train, y_train, batch_size=100, epochs=10, validation_data=(x_test, y_test))
ValueError: Input 0 of layer sequential_12 is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape (100, 28, 28)
Should I say "resize"? I tried to fix the number, but I couldn't solve it. I'd appreciate it if you could help me.
Upvotes: 2
Views: 979
Reputation:
You must reshape mnist dataset before using into model as below:
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_labels = train_labels[:1000] # to take only 1000 rows of dataset
test_labels = test_labels[:1000]
train_images = train_images[:1000].reshape(-1, 28 * 28) / 255.0
test_images = test_images[:1000].reshape(-1, 28 * 28) / 255.0
Then define the model and fit the train and test dataset to the model.
model = tf.keras.models.Sequential([
keras.layers.Dense(512, activation='relu', input_shape=(784,)),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.losses.SparseCategoricalCrossentropy(),
metrics=['Accuracy'])
# Display the model's architecture
model.summary()
model.fit(train_images,train_labels, epochs=10, validation_data=(test_images, test_labels))
Please refer this for more detail.
Upvotes: 1