Training a Basic Single-Layer Neural Network Through Backpropagation
A single-layer neural network, despite its simplicity, demonstrates the core concepts of neural network training through backpropagation. Let's break down how this works using matrix operations.
Basic Structure
A single-layer neural network consists of:
Input layer (not counted as a layer since it doesn't perform computations)
One layer of neurons with weights, biases, and an activation function
Output layer that produces predictions
The goal of training is to adjust the weights and biases to minimize the difference between predicted outputs and actual outputs.
Forward Propagation with Matrices
The forward pass through a single-layer neural network can be expressed using matrix operations:
Start with input data X (where each row represents a training example)
Apply weights and biases: Z = XW + b
Apply activation function: Y = σ(Z)
In matrix form, if we have n inputs and m outputs:
X is an [examples × n] matrix
W is an [n × m] matrix
b is an [1 × m] bias vector
Y is an [examples × m] matrix of predictions
For a single-layer network with sigmoid activation, this becomes:
python
# Forward propagation
Z = np.dot(X, W) + b # Weighted sum
Y_pred = sigmoid(Z) # Apply activation function
Calculating the Error
After forward propagation, we calculate the error between predicted outputs and actual outputs:
python
error = Y_actual - Y_pred
This error will drive the backpropagation process[10].
Backpropagation Process
The core of backpropagation is calculating the gradient of the error with respect to each weight, which tells us how to update weights to reduce error. In a single-layer network, this becomes:
Calculate the local gradient (delta):
python
delta = error * sigmoidDerivative(Y_pred)
Where sigmoidDerivative is σ(x) * (1 - σ(x))[10].
Calculate the gradient of weights:
python
gradient = np.dot(X.T, delta)
This matrix multiplication efficiently calculates the gradient for all weights simultaneously[10].
Weight Update
Using gradient descent, we update the weights:
python
W += learning_rate * gradient
Or written in full:
python
W += learning_rate * np.dot(X.T, delta)
This adjusts each weight in the direction that reduces the error[10].
Complete Training Loop
A complete training loop for a single-layer neural network using matrices would look like:
python
def train_single_layer_network(X, Y_actual, learning_rate=0.1, max_iterations=1000, tolerance=0.001):
# Initialize weights randomly
n_inputs = X.shape[1]
n_outputs = Y_actual.shape[1]
W = np.random.randn(n_inputs, n_outputs) * 0.01
b = np.zeros((1, n_outputs))
for i in range(max_iterations):
# Forward propagation
Z = np.dot(X, W) + b
Y_pred = sigmoid(Z)
# Calculate error
error = Y_actual - Y_pred
# Check for convergence
if np.max(np.abs(error)) < tolerance:
print(f"Converged after {i} iterations")
break
# Backpropagation
delta = error * sigmoidDerivative(Y_pred)
# Update weights and biases
W += learning_rate * np.dot(X.T, delta)
b += learning_rate * np.sum(delta, axis=0, keepdims=True)
return W, b
How Backpropagation Works with Matrices
The beauty of using matrices for backpropagation is that they allow us to process all training examples simultaneously. When we compute np.dot(X.T, delta):
X.T has shape [n × examples]
delta has shape [examples × m]
The result has shape [n × m], which matches our weight matrix W
Each element in this gradient matrix represents how much a particular weight contributes to the overall error[6][10].
Why This Works
The backpropagation algorithm works by applying the chain rule of calculus to compute partial derivatives. In a single-layer network, we're calculating ∂C/∂W (how the cost function changes with respect to each weight).
For a single-layer neural network, backpropagation is relatively straightforward because the error signals don't need to be propagated through multiple layers. This makes it an excellent starting point for understanding the fundamental principles of neural network training[7].
By iteratively performing forward propagation, error calculation, and weight updates, the network gradually learns to minimize the error and produce better predictions.