RESEARCH

BuildingSimple Neural Network(NN) from Scratch

27 JANUARY 2025
Mark Sikaundi - Data Scientist and AI Researcher.

Share this post

A new generation of African talent brings cutting-edge AI to scientific challenges

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.

Step 1: Import Libraries

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

Step 2: Load the MNIST dataset


# 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)
            

Step 3: Build the Neural Network


# 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'))
            
            

Step 4: Compile the Model


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

Step 5: Train the Model


# Train the model
history = model.fit(x_train, y_train, epochs=10, batch_size=32, validation_split=0.2)
            

Step 6: Evaluate the Model


# 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}')

Step 7: Visualize Training History


# 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()
            
           

Conclusion

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.

Access to full source codes

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 Repository

Credit source from:Google DeepMind