TechTorch

Location:HOME > Technology > content

Technology

Creating a Neural Network from Scratch: A Step-by-Step Guide

March 04, 2025Technology3400
Creating a Neural Network from Scratch: A Step-by-Step Guide In this d

Creating a Neural Network from Scratch: A Step-by-Step Guide

In this detailed guide, we will walk you through the process of implementing a simple feedforward neural network from scratch using Python. This guide is designed to help you understand the essential components of a neural network, namely, initialization, forward propagation, backpropagation, and training. If you're looking to build a neural network without relying on premade simulators or libraries, this tutorial is perfect for you. Let's dive in!

Step 1: Import Libraries

The first step in creating a neural network is to import the necessary libraries. For this example, we will use the NumPy library, which provides support for multi-dimensional arrays and is essential for numerical operations.

import numpy as np

Step 2: Initialize the Neural Network

Next, we define a class for our neural network. Here, we create a simple network with one hidden layer. This class will handle the initialization of weights and biases.

class NeuralNetwork: def __init__(self, input_size, hidden_size, output_size, learning_rate0.01): # Initialize weights and biases self.learning_rate learning_rate self.weights_input_hidden np.random.rand(input_size, hidden_size) self.weights_hidden_output np.random.rand(hidden_size, output_size) _input_hidden np.random.rand(1, hidden_size) _hidden_output np.random.rand(1, output_size) def sigmoid(self, x): return 1 / (1 np.exp(-x)) def sigmoid_derivative(self, x): return x * (1 - x)

Step 3: Forward Propagation

The forward pass involves computing the output of the network. We calculate the hidden layer input and output, followed by the output layer input and output.

def forward(self, x): # Hidden layer input and output self.hidden_layer_input (x, self.weights_input_hidden) _input_hidden self.hidden_layer_output (self.hidden_layer_input) # Output layer input and output self.output_layer_input (self.hidden_layer_output, self.weights_hidden_output) _hidden_output self.output (self.output_layer_input) return self.output

Step 4: Backpropagation

The backpropagation algorithm is used to update the weights and biases based on the error. This step is crucial for training the neural network to minimize the loss function.

def backward(self, x, y): # Calculate the error output_error y - self.output output_delta output_error * _derivative(self.output) # Hidden layer error hidden_layer_error (output_delta, self.weights_hidden_output.T) hidden_layer_delta hidden_layer_error * _derivative(self.hidden_layer_output) # Update weights and biases self.weights_hidden_output (self.hidden_layer_output.T, output_delta) * self.learning_rate _hidden_output (output_delta, axis0, keepdimsTrue) * self.learning_rate self.weights_input_hidden (x.T, hidden_layer_delta) * self.learning_rate _input_hidden (hidden_layer_delta, axis0, keepdimsTrue) * self.learning_rate

Step 5: Training the Network

To train the neural network, we iterate through the dataset for a specified number of epochs. During each epoch, we update the weights and biases using backpropagation. Additionally, we calculate and print the loss to monitor the training process.

def train(self, X, y, epochs): for epoch in range(epochs): # Forward pass (X) # Backward pass (X, y) # Calculate and print the loss if epoch % 1000 0: mean_squared_error ((y - self.output) ** 2) print(f'Epoch {epoch} Loss: {mean_squared_error}')

Step 6: Using the Network

After training the neural network, you can use it to make predictions. Here, we demonstrate the network with a simple XOR problem.

if __name__ '__main__': # Dummy data for the XOR problem X ([[0, 0], [0, 1], [1, 0], [1, 1]]) y ([[0], [1], [1], [0]]) # Create the neural network with one hidden layer nn NeuralNetwork(input_size2, hidden_size2, output_size1) # Train the neural network (X, y, epochs10000) # Test the neural network for x in X: print(f'Input: {x} - Predicted Output: {(x)}')

Summary

In this guide, we have covered the essential components of a neural network: initialization, forward propagation, backpropagation, and training. By following these steps, you can create and train a neural network from scratch using Python. While this simple implementation serves educational purposes, for more complex tasks, you might want to consider established libraries like TensorFlow or PyTorch, which provide optimized implementations and additional functionalities.

To further enhance your neural network, you can add features such as different activation functions (ReLU, Tanh), multiple hidden layers, regularization techniques, and batch training.