Creating a TensorFlow Neural Network that can Recognize Handwritten Digits

Tiempo de lectura: 4 minutos

Reading time: 5 minutes

Step 1: Install TensorFlow

The first thing you need to do is install TensorFlow. You can do this by following the instructions on the TensorFlow website: https://www.tensorflow.org/install/.

If you want to install it in Docker:

 docker pull tensorflow/tensorflow:latest  # Download latest stable image
 docker run -it -p 8888:8888 tensorflow/tensorflow:latest-jupyter  # Start Jupyter server

If you want to install it locally:

# Requires the latest pip
pip install --upgrade pip

# Current stable release for CPU and GPU
pip install tensorflow

# Or try the preview build (unstable)
pip install tf-nightly

Step 2: Get the Data

For this tutorial, we will be using the MNIST dataset, which consists of images of handwritten digits. You can download the dataset from the TensorFlow website using the following code:

from tensorflow import keras

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

Step 3: Data Preprocessing

Before training our neural network, we need to preprocess the data. First, we will normalize the pixel values to be in the range of 0 to 1 instead of 0 to 255. Then, we will flatten the 28×28 pixel images into a 784-pixel vector.

# Normalize pixel values
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255

# Flatten the images
x_train = x_train.reshape((len(x_train), 784))
x_test = x_test.reshape((len(x_test), 784))

We will also convert the labels into one-hot vectors.

from tensorflow.keras.utils import to_categorical

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

Step 4: Create the Model

We will use a three-layer neural network: an input layer, a hidden layer, and an output layer. The input layer will have 784 nodes, the hidden layer will have 64 nodes, and the output layer will have 10 nodes (one for each possible digit).

from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(64, activation="relu", input_shape=(784,)),
    layers.Dense(10, activation="softmax")
])

Step 5: Compile the Model

Before training the model, we need to compile it. In this step, we need to specify the loss function, the optimizer, and the metrics we will use to evaluate the model.

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])

Step 6: Train the Model

Now that we have prepared our data and model, we can train the model.

model.fit(x_train, y_train, epochs=10, batch_size=128, validation_data=(x_test, y_test))

In this example, we train the model for 10 epochs with a batch size of 128. We also use the test data as the validation set.

Step 7: Evaluate the Model

Once the model has been trained, we can evaluate its performance using the test data.

test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test accuracy:", test_acc)

Step 8: Use the Model

Finally, we can use the model to make predictionson new data

To make predictions using the trained model, we can use the predict method of the model. For example, if we have a new image of a digit, we can use the model to predict what digit it is:

import numpy as np

# Load an example image
new_image = np.array([[
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
]])

# Make a prediction
prediction = model.predict(new_image)

# Print the prediction
print('Prediction:', np.argmax(prediction))

Here is the complete code:

import tensorflow as tf
from tensorflow.keras.datasets import mnist

# Load the training and test data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize the input data
x_train = x_train / 255.0
x_test = x_test / 255.0

# Create the neural network model
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train the model
model.fit(x_train, y_train, epochs=5)

# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('Accuracy:', test_acc)

# Use the model to make predictions
import numpy as np

# Load an example image
new_image = np.array([[
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
]])
Make a prediction
prediction = model.predict(new_image)
Print the prediction
print('Prediction:', np.argmax(prediction))

In this code, we load an example image that is simply a 28×28 matrix with all values set to zero. Then, we make a prediction on the trained model and use np.argmax to get the class of the prediction.

Now, if you want to test the model with your own images, you can load and preprocess them similarly to how we did with the MNIST data in the tutorial. Here’s an example of how to load and preprocess an image:

from PIL import Image

# Load the image
img = Image.open('my_image.png')

# Convert the image to grayscale and resize it to 28x28 pixels
img = img.convert('L').resize((28, 28))

# Convert the image to a numpy array
img_array = np.array(img)

# Invert the colors of the image (since MNIST has white numbers on a black background)
img_array = 255 - img_array

# Normalize the image (divide each pixel value by 255 to get values in the range [0, 1])
img_array = img_array.astype('float32') / 255.0

# Flatten the image (convert a 2D matrix into a 1D vector)
img_array = img_array.reshape((1, 28*28))

Once you have preprocessed your image, you can make a prediction on the model using the above code. I hope this tutorial has been helpful.

Leave a Comment