"how numbers are stored and used in computers"
This Python script trains a Convolutional Neural Network (CNN) using Keras to recognize handwritten digits from the MNIST dataset. The MNIST dataset contains 60,000 training images and 10,000 testing images of handwritten digits 0 through 9, each stored in
This is an illustrative example of loading and preprocessing data, normalizing pixel values to the interval
You will need to install Keras to follow along with this tutorial.
code.py1# mnist_convnet.py 2import numpy as np 3import keras 4from keras import layers 5from keras.utils import to_categorical
There are ten possible digits that can be classified, and each digit is represented by a
code.py1# mnist_convnet.py 2num_classes = 10 3input_shape = (28, 28, 1)
The mnist.load_data()
function returns tuples of training and test data.
code.py1# Load the data and split it between train and test sets 2(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
First we normalize the image data to the range
code.py1x_train = x_train.astype("float32") / 255 2x_test = x_test.astype("float32") / 255
This next step is required for Conv2D
layers, where we use np.expand_dims
to reshape the images so there is a channel dimension, by adding a new dimension at the end (axis -1
) of the x_train
array. The original shape of x_train
is
code.py1x_train = np.expand_dims(x_train, -1) 2x_test = np.expand_dims(x_test, -1)
This next step converts class labels to one-hot encoded vectors, so
code.py1y_train = to_categorical(y_train, num_classes) 2y_test = to_categorical(y_test, num_classes)
Now that the data has been prepared, it's finally time to start training! We will start by defining a batch_size
, which determines the number of training examples the model sees in each training batch. We will also define epochs
, which determines the number of times the model will see the entire dataset.
code.py1batch_size = 128 2epochs = 3
Now we can define our model with keras.Sequential
, which constructs a CNN model with the given ordering of layers.
code.py1model = keras.Sequential( 2 [ 3 layers.Input(shape=input_shape), # Input layer matching image shape 4 layers.Conv2D(32, kernel_size=(3, 3), activation="relu"), # First convolutional layer (32 filters, 3x3 kernel) 5 layers.MaxPooling2D(pool_size=(2, 2)), # Downsamples the feature map 6 layers.Conv2D(64, kernel_size=(3, 3), activation="relu"), # Second convolutional layer (64 filters) 7 layers.MaxPooling2D(pool_size=(2, 2)), # Second pooling layer 8 layers.Flatten(), # Flattens 2D feature maps into 1D vector 9 layers.Dropout(0.5), # Randomly drop 50% of activations to prevent overfitting 10 layers.Dense(num_classes, activation="softmax"), # Output layer: 10 units, one for each class 11 ] 12)
Let's display the model architecture and compile it with the specified loss function, optimizer, and evaluation metric.
code.py1model.summary() 2 3model.compile( 4 loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"] 5)
Finally, let's evaluate the model on the test data.
code.py1model.fit( 2 x_train, y_train, 3 batch_size=batch_size, 4 epochs=epochs, 5 validation_split=0.1 6) 7 8score = model.evaluate(x_test, y_test, verbose=0) 9print("Test loss:", score[0]) 10print("Test accuracy:", score[1])