-
Notifications
You must be signed in to change notification settings - Fork 6
/
mnist_digit_classification.py
64 lines (53 loc) · 2.25 KB
/
mnist_digit_classification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.utils import to_categorical
# Load the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Preprocess the data
X_train = X_train.astype('float32') / 255 # Normalize the images to [0, 1]
X_test = X_test.astype('float32') / 255
y_train = to_categorical(y_train, 10) # Convert labels to one-hot encoding
y_test = to_categorical(y_test, 10)
# Define the neural network model
model = Sequential()
model.add(Flatten(input_shape=(28, 28))) # Flatten the input image
model.add(Dense(128, activation='relu')) # Hidden layer with 128 neurons
model.add(Dropout(0.2)) # Dropout layer for regularization
model.add(Dense(64, activation='relu')) # Another hidden layer with 64 neurons
model.add(Dropout(0.2)) # Another dropout layer
model.add(Dense(10, activation='softmax')) # Output layer for 10 classes
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model
history = model.fit(X_train, y_train, epochs=10, batch_size=128, validation_split=0.2, verbose=2)
# Evaluate the model on the test set
test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.2f}")
# Plot the training and validation accuracy and loss
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
# Predict a few samples from the test set
predictions = model.predict(X_test)
for i in range(5):
plt.imshow(X_test[i], cmap='gray')
plt.title(f"Predicted: {np.argmax(predictions[i])}, True: {np.argmax(y_test[i])}")
plt.axis('off')
plt.show()