Today we are going to build and train a simple neural network using the MNIST dataset. The MNIST dataset consists of 60,000 training images and 10,000 testing images of handwritten digits (0-9). We'll use TensorFlow and Keras to build and train our neural network. Below is a step-by-step guide with detailed comments and explanations.
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Normalize the images to values between 0 and 1
x_train = x_train / 255.0
x_test = x_test / 255.0
# Convert labels to one-hot encoding
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# Initialize the model
model = Sequential()
# Flatten the input data (28x28 images) to a 1D array of 784 elements
model.add(Flatten(input_shape=(28, 28)))
# Add a dense layer with 128 neurons and ReLU activation function
model.add(Dense(128, activation='relu'))
# Add an output layer with 10 neurons (one for each digit) and softmax activation function
model.add(Dense(10, activation='softmax'))
# 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=32, validation_split=0.2)
# Evaluate the model on the test data
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f'Test loss: {test_loss}')
print(f'Test accuracy: {test_accuracy}')
# Plot training & validation accuracy values
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
# Plot training & validation loss values
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.show()
In this tutorial, we built and trained a simple neural network using the MNIST dataset. We used TensorFlow and Keras to build the model and train it on the training data. We then evaluated the model on the test data and visualized the training history. We achieved an accuracy of over 98% on the test data, which is quite good for a simple neural network. You can further improve the model by experimenting with different architectures, hyperparameters, and optimization algorithms.
You can access the full source code for this tutorial on our GitHub repository. Feel free to fork the repository and experiment with the code on your own.
GitHub RepositoryCredit source from:Google DeepMind