Correct backpropagation in simple perceptron
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Backpropagation is a central mechanism for training neural networks, including simple perceptrons. A perceptron is the most basic unit of a neural network, which consists of a linear combination of input features followed by a non-linear activation function. Backpropagation helps in adjusting the weights of these inputs to minimize the error between the predicted and actual outputs.
Understanding Perceptron
A perceptron takes several inputs, passes them through a weighted summation, and finally applies an activation function to produce an output. Mathematically, it can be expressed as:
Where:
• is the output. • is the activation function (e.g., sigmoid, hyperbolic tangent). • are the weights. • are the inputs. • is the bias term.
Error Calculation
The error of the perceptron is determined using a loss function, commonly the mean squared error (MSE) for regression tasks or cross-entropy loss for classification tasks. For MSE, it can be expressed as:
Where:
• is the actual output for data point. • is the predicted output.
Backpropagation Algorithm
Backpropagation is an optimization technique for neural networks, and it involves two major steps:
1. Forward Pass
In the forward pass, inputs are fed through the network, and outputs are calculated. The output is compared to the actual output using a defined error metric (like MSE).
2. Backward Pass
In the backward pass, the error propagated backwards through the network to update the input weights. The system uses the following steps:
Step 1: Compute the gradient
Calculate the gradient of the loss function concerning each weight, using the chain rule of calculus. This gradient indicates how a slight change in each weight affects the loss. If we consider a single perceptron with weights and loss , the gradient for each weight is computed as:
Where:
• •
Step 2: Update Weights
Once the gradient is computed, update the weights using gradient descent. The weight update rule is expressed as:
Where is the learning rate, a hyperparameter that controls the step size during optimization.
Examples of Backpropagation
Consider a simple perceptron with inputs and , weights and , bias , and a sigmoid activation function. The predicted output and actual output are:
• Predicted: • Actual:
Forward Pass
Calculate the output:
Calculate the error using MSE:
Backward Pass
Calculate the error gradient for :
Update weights:
Key Considerations in Backpropagation
• Learning Rate: A crucial hyperparameter in training neural networks. A small learning rate results in slow convergence, while a large learning rate can lead to divergence. • Activation Functions: The choice of activation function affects the performance of a neural network. Sigmoid functions were popular in early days but suffer from the vanishing gradient problem. ReLU and its variants are preferred in deeper networks. • Weight Initialization: Poor weight initialization can lead to slow convergence or getting stuck in local minima.
Summary Table
| Steps in Backpropagation | Description |
| Forward Pass | Compute predicted output by passing inputs forward through network. |
Compute Loss | Calculate loss between actual and predicted output using a cost function. |
| Backward Pass | Calculate gradients by propagating loss backwards through network. |
| Update Weights | Adjust weights using gradient descent. |
Understanding and implementing correct backpropagation in a simple perceptron lays the foundation for constructing and training more complex neural networks effectively. As you gain familiarity with the basic concepts and nuances of perceptrons, transitioning to multi-layer networks will be considerably more manageable.
Related reading
- Correct Implementation of Dice `Loss` in Tensorflow / Keras
- Correct Implementation of Dice `Loss` in Tensorflow / Keras
- Could Keras prefetch data like tensorflow Dataset?
- could not create cudnn handle CUDNN_STATUS_INTERNAL_ERROR
- Correct way of doing data augmentation in TensorFlow with the dataset api?
- Correct way of normalizing and scaling the MNIST dataset
- Could not create cudnn handle CUDNN STATUS INTERNAL ERROR
- Could not interpret optimizer identifier error in Keras
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.