Build a Neural Network

How to Build a Neural Network and Make Predictions?

In this tutorial, we will learn about How to Build a Neural Network and Make Predictions. Lately, people have been really into neural networks. They’re like a computer system that works like a brain, with nodes connected together. These networks are great at sorting through big piles of data and figuring out patterns to solve hard problems or guess stuff. And you know what’s super cool? They can keep on learning forever.

How to Build a Neural Network

Creating and deploying neural networks can be a challenging process, which largely depends on the specific task and dataset you’re dealing with. To succeed in this endeavor, it’s crucial to possess a solid grasp of machine learning concepts, along with strong programming skills. Additionally, a deep understanding of the chosen deep learning framework is essential. Moreover, it’s imperative to prioritize responsible and ethical usage of AI models, especially when integrating them into real-world applications.

What is a Neural Network?

Neural networks are a type of artificial intelligence modeled after the human brain. They consist of interconnected nodes, or “neurons,” organized in layers.

  • Input Layer: The input layer receives data, like images or text.
  • Hidden Layers: Between the input and output layers, there can be one or more hidden layers. These layers process and transform the data.
  • Output Layer: The output layer produces the final results, like predicting numbers or classifying objects.

In the training phase, neural networks learn from data by actively tweaking the connections (weights) between neurons. This learning process consists of two key steps: forward propagation, where predictions are made, and backward propagation, where weights are updated based on errors.

Furthermore, neural networks find extensive use in diverse applications such as image recognition, natural language processing, and even gaming. Their remarkable strength lies in their ability to handle complex patterns and vast datasets, making them a potent asset within the realm of machine learning.

How to Build Our First Neural Network?

Certainly! Building your first neural network in Python is an exciting step in the world of artificial intelligence and deep learning. Let’s walk through a simple example of creating a neural network to solve a basic problem: classifying handwritten digits using the popular library TensorFlow and its high-level API, Keras.

Step 1: Import Libraries

First, you need to import the necessary libraries:

import tensorflow as tf
from tensorflow import keras

Step 2: Load and Prepare the Data

Before you can train a neural network, you need to prepare your data. This typically involves:

  • Data Collection: Gather a dataset that is relevant to your task. For example, if you want to classify images of cats and dogs, you need a dataset of labeled cat and dog images.
  • Data Preprocessing: Clean and preprocess your data. This can include tasks like resizing images, normalizing values, and splitting the data into training and testing sets.

For this, we’ll use the MNIST dataset, which contains handwritten digits. You can load it using TensorFlow’s built-in function:

mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

Next, preprocess the data by normalizing the pixel values to a range between 0 and 1:

train_images = train_images / 255.0
test_images = test_images / 255.0

Step 3: Build the Neural Network

Choose a neural network architecture that is suitable for your problem. The architecture defines the structure of the neural network, including the number of layers, the type of layers, and the number of neurons in each layer. For example, you can choose between feedforward neural networks, convolutional neural networks (CNNs), and recurrent neural networks (RNNs) based on your data and task.

Now, let’s proceed to create a straightforward neural network. In this example, we’ll opt for a feedforward neural network featuring one hidden layer:

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),  # Flatten the 28x28 images to a 1D array
    keras.layers.Dense(128, activation='relu'),  # Hidden layer with 128 neurons and ReLU activation
    keras.layers.Dense(10, activation='softmax') # Output layer with 10 neurons (for 0-9 digits) and softmax activation
])

Step 4: Compile the Model

Compile the neural network model by specifying:

  • Loss Function: This defines the measure of error between the predicted output and the actual target. The choice of loss function depends on the type of problem you’re solving, such as mean squared error for regression or categorical cross-entropy for classification.
  • Optimizer: Select an optimization algorithm (e.g., Adam, SGD) that adjusts the network’s parameters to minimize the loss function.
  • Metrics: Specify evaluation metrics (e.g., accuracy, precision, recall) to assess the model’s performance during training.

Compile the model by specifying the loss function, optimizer, and evaluation metric:

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

Step 5: Train the Model

The training process involves feeding your training data into the neural network and iteratively updating the model’s weights and biases to minimize the loss. This process includes:

  • Forward Propagation: The neural network makes predictions on the training data.
  • Loss Computation: The difference between the predicted values and the actual target values is computed using the chosen loss function.
  • Backward Propagation (Backpropagation): Errors are propagated backward through the network, and the optimizer updates the weights and biases using gradients to reduce the loss.
  • Epochs: Training typically occurs over multiple epochs, where one epoch is a complete pass through the entire training dataset.

Now, it’s time to train the neural network on the training data:

model.fit(train_images, train_labels, epochs=5)

Step 6: Evaluate the Model

After training, evaluate the model’s performance on the test data. During training, it’s essential to monitor the model’s performance on a separate validation dataset. This helps detect overfitting, where the model learns the training data too well but fails to generalize to new data. You can fine-tune hyperparameters, adjust the architecture, or employ techniques like dropout and regularization to improve performance.

After training, evaluate the model’s performance on a separate testing dataset to assess its ability to generalize to new, unseen data.

test_loss, test_acc = model.evaluate(test_images, test_labels)
print("Test accuracy:", test_acc)

Step 7: Make Predictions

Once you are satisfied with your model’s performance, you can use it to make predictions on new, real-world data.

You can make predictions using the trained model on new data:

predictions = model.predict(test_images)

Example: How to Train Your First Neural Network in Python

There is an example of training a simple neural network to recognize handwritten digits from the MNIST dataset using Python and TensorFlow/Keras. We’ll also provide sample output after training.

import tensorflow as tf
from tensorflow import keras

# Load the MNIST dataset
mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Preprocess the data
train_images = train_images / 255.0
test_images = test_images / 255.0

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

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

# Train the model
model.fit(train_images, train_labels, epochs=5)

# Evaluate the model on test data
test_loss, test_acc = model.evaluate(test_images, test_labels)
print("Test accuracy:", test_acc)

Output:

Epoch 1/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2617 - accuracy: 0.9256
Epoch 2/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1166 - accuracy: 0.9664
Epoch 3/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0814 - accuracy: 0.9752
Epoch 4/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0610 - accuracy: 0.9815
Epoch 5/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0481 - accuracy: 0.9856
313/313 [==============================] - 0s 2ms/step - loss: 0.0791 - accuracy: 0.9761
Test accuracy: 0.9761000275611877

In this example, we began by loading the MNIST dataset. Afterward, we proceeded to create a neural network with a single hidden layer. Following the network’s architecture setup, we initiated the training process, which spanned over five epochs. Subsequently, we assessed its performance on the test dataset. The noteworthy outcome was that the model achieved an impressive test accuracy of approximately 97.61%. This outcome underscores the model’s capability to accurately classify handwritten digits, demonstrating a high level of accuracy.

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!