CNN Train Mnist
CNN Train Mnist
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
[10]: # Display the first 10 images from the training set with colorful borders and␣
↪styled titles
plt.figure(figsize=(14, 2))
for i in range(10):
ax = plt.subplot(1, 10, i+1)
plt.imshow(train_images[i], cmap='gray')
1
ax.add_patch(rect)
plt.axis('off')
[5]: # Reshape the input images to (28, 28, 1) to match the input shape of the model
train_images = train_images.reshape(-1, 28, 28, 1)
test_images = test_images.reshape(-1, 28, 28, 1)
Epoch 1/5
1875/1875 [==============================] - 69s 36ms/step - loss: 0.1480 -
accuracy: 0.9554 - val_loss: 0.0522 - val_accuracy: 0.9832
Epoch 2/5
1875/1875 [==============================] - 56s 30ms/step - loss: 0.0445 -
accuracy: 0.9861 - val_loss: 0.0366 - val_accuracy: 0.9875
Epoch 3/5
1875/1875 [==============================] - 53s 28ms/step - loss: 0.0305 -
accuracy: 0.9903 - val_loss: 0.0309 - val_accuracy: 0.9896
Epoch 4/5
1875/1875 [==============================] - 54s 29ms/step - loss: 0.0234 -
accuracy: 0.9923 - val_loss: 0.0447 - val_accuracy: 0.9860
Epoch 5/5
1875/1875 [==============================] - 53s 28ms/step - loss: 0.0177 -
accuracy: 0.9942 - val_loss: 0.0315 - val_accuracy: 0.9906
2
[8]: # Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc*100}')
[8]: