Building Your First Neural Network: A Step-by-Step Tutorial

Building Your First Neural Network: A Step-by-Step Tutorial

Introduction

Imagine teaching a computer to recognize handwritten numbers—like those on your mailbox or in your notes. That’s exactly what we’re doing here! We will create a neural network that can identify digits from 0 to 9. We'll use the MNIST dataset, which is a huge set of images of handwritten digits. Each image is 28x28 pixels and has a label (the correct number). MNIST is super popular because it’s simple, clean, and perfect for getting started with AI. So, let’s dive in and have some fun with machine learning!


Article content
Fig. MNIST dataset

Prerequisites

Before we get started, make sure you have:

  • Python 3.x installed on your computer.
  • Basic Python knowledge (don’t worry— I will walk you through everything 😃).
  • An idea of what arrays and functions are.
  • The necessary libraries. Open your terminal and run:

pip install tensorflow numpy matplotlib        


Article content
Fig. Download and install libraries

Step 1: Setting Up Your Environment

Create a new Python file (e.g., first_neural_network.py) or open a Jupyter notebook. Then, import the libraries you’ll need:

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

Step 2: Loading and Preprocessing the Data

Here’s where MNIST comes into play. This dataset contains thousands of 28x28 pixel images of handwritten digits. Keras makes it easy to load MNIST directly.

# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# Normalize the data: Convert pixel values from 0-255 to 0-1
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# Flatten the images into 1D arrays (28x28 pixels -> 784 features)
x_train = x_train.reshape(-1, 28 * 28)
x_test = x_test.reshape(-1, 28 * 28)
        

Why normalize? Normalizing helps our model train faster and more effectively by ensuring that all input values are on a similar scale.

Step 3: Defining the Neural Network Architecture

We’ll build a simple feedforward neural network (also called a dense network). Our network will have:

  • An input layer that matches our flattened image size (784 features).
  • A couple of hidden layers that help the network learn complex patterns.
  • An output layer with 10 neurons (one for each digit) using softmax activation to output probabilities.

# Define the model architecture
model = models.Sequential([
    layers.Dense(128, activation='relu', input_shape=(28 * 28,)),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])
        

Step 4: Compiling the Model

Before training, we need to compile our model by choosing a loss function, an optimizer, and some metrics to evaluate performance. For multi-class classification, we'll use sparse categorical cross-entropy.

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

Step 5: Training the Neural Network

Now, let’s train the model using our training data. We'll run the training process over several epochs (complete passes through the training data) and keep an eye on how our model improves.

history = model.fit(x_train, y_train, epochs=10, validation_split=0.1)
        

Tip: If your computer isn’t super fast, you can reduce the number of epochs to get quicker results.

Step 6: Evaluating the Model

After training, it’s time to see how well our model performs on unseen data. We use the test dataset to evaluate its accuracy.

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

Step 7: Visualizing the Results

Visualizing the training process can help you understand how the model is learning over time. Let’s plot the training and validation accuracy:

plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
plt.show()
        


Article content
Fig. Visualising the results


Article content
Fig. Full code

Conclusion

And there you have it—you’ve built and trained your very first neural network! In this tutorial, we:

  • Set up our Python environment.
  • Loaded and preprocessed the MNIST dataset (a collection of handwritten digit images).
  • Defined a simple neural network architecture.
  • Compiled, trained, and evaluated our model.
  • Visualized the training process to see how our model improved.

Now that you’ve got the basics down, you can experiment further by tweaking the network, trying out different datasets, or exploring more complex models.

Happy coding, and enjoy your journey into AI!

Harsh Kumar

Ex - Axis | Ex Intern - Tata Technologies |

2w

Great insights 👍

Chinmay P

Simulation Engineer at Man Energy Solutions | VNIT Nagpur | VJTI, Mumbai

1mo

Thanks for sharing, Akash

To view or add a comment, sign in

More articles by Akash Shahade

Insights from the community

Others also viewed

Explore topics