What Are Neural Networks?
> A neural network is a type of machine learning model that mimics the way the human brain works. It’s made up of layers of nodes (neurons) that process data to recognize patterns and make predictions.
Here’s how it works:
Input layer: This takes the raw data (e.g., an image of a handwritten digit).
Hidden layers: These process the data, looking for patterns (e.g., edges, curves, shapes).
Output layer: This produces the final prediction (e.g., the number 7).
For example, in our project:
Neural networks are great at recognizing patterns in images, text, and other complex data.
Prerequisites
Before we start, here’s what you need to follow along:
Python environment: You can either:
Basic Python knowledge: Familiarity with Python syntax will help, but this guide is beginner-friendly.
Internet access: Required if you’re using Google Colab or downloading datasets.
In this post, we’ll use Google Colab for simplicity, as it requires no installation and comes with TensorFlow pre-installed.
Let’s Build Your First Neural Network
As mentioned earlier, we’ll use the MNIST dataset for our neural network. But why do we need a dataset? Think of a dataset as the "textbook" that a neural network studies to learn. It provides examples for the model to analyze, along with the correct answers, helping it understand patterns and make accurate predictions.
In this project, we’re using the MNIST dataset, which is a collection of 70,000 grayscale images of handwritten digits. Here’s why it’s ideal:
Each image is 28x28 pixels—small and easy to process.
Every image is labeled with the correct digit (0–9), so the model knows what it’s trying to predict.
With this dataset, we’ll teach our neural network to recognize and classify handwritten digits. Let’s dive in!
Step 1: Import Libraries
Before we begin building the neural network, we need to import a few libraries. These libraries are pre-written tools that make it easier to work with machine learning, numerical operations, and data visualization.
Here’s what we’re importing and why:
TensorFlow: This is the core library we’ll use to build and train our neural network.
Keras (part of TensorFlow): Provides a simple interface for building deep learning models.
Matplotlib: Used to visualize the data, such as images and training results.
NumPy: Helps with numerical operations, like handling arrays of data.
Here’s the code to import these libraries:
import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
import numpy as np
Once this is done, we’re ready to move on to the next step: loading the dataset.
Step 2: Load and Preprocess the Dataset
To train our neural network, we are using the MNIST dataset. Let’s load and prepare the dataset for our model.
TensorFlow makes it easy to load the MNIST dataset. Here’s the code to do it:
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
The train_images and train_labels are 60,000 images and their corresponding labels (digits 0–9). This data will be used to train the neural network. Then the test_images and test_labels are 10,000 images and labels that we’ll use to evaluate how well the trained model performs on unseen data.
Neural networks work best with normalized data, so we need to scale the pixel values from their original range of 0–255 down to 0–1. We also convert the pixel values to floating-point numbers (float32) for better compatibility with the model.
Here’s the code to normalize the data:
train_images = train_images.astype('float32') / 255
test_images = test_images.astype('float32') / 255
This is important because neural networks train faster and more efficiently when input data is scaled to a uniform range (0 to 1) and reduces the risk of instability during training.
Now that the dataset is loaded and normalized, let’s inspect it to understand its structure. We’ll print the shape of the datasets and look at the shape of an individual image.
print(f"Training data shape: {train_images.shape}, Training labels shape: {train_labels.shape}")
print(f"Testing data shape: {test_images.shape}, Testing labels shape: {test_labels.shape}")
print(f"Shape of the images in the training dataset: {train_images[0].shape}")
Here is what the model now looks like: