Library › Calculating AI by Hand ✍️
Backpropagation by Hand ✍️
Reinforcement Learning with Human Feedback (RLHF) by Hand ✍️
Backpropagation computes the gradient of the loss with respect to every weight, working backward layer by layer, so the network can learn.
Forward Pass
Step 1 of 11: Given
Given a multi layer perceptron (3 levels), an input vector X, predictions Y^{Pred} = [0.5, 0.5, 0], and ground truth label Y^{Target} = [0, 1, 0].
Backward Pass
Step 2 of 11: Insert Cells
Insert cells to hold our calculations.
Step 3 of 11: Layer 3 Softmax
Calculate ∂L / ∂z3 directly using the simple equation: Y^{Pred} - Y^{Target} = [0.5, -0.5, 0].
This simple equation is the benefit of using Softmax and Cross Entropy Loss together.
Step 4 of 11: Layer 3 Weights & Biases
Calculate ∂L / ∂W3 and ∂L / ∂b3 by multiplying ∂L / ∂z3 and [ a2 | 1 ].
Step 5 of 11: Layer 2 Activations
Calculate ∂L / ∂a2 by multiplying ∂L / ∂z3 and W3.
Step 6 of 11: Layer 2 ReLU
Calculate ∂L / ∂z2 by multiplying ∂L / ∂a2 with 1 for positive values and 0 otherwise.
Step 7 of 11: Layer 2 Weights & Biases
Calculate ∂L / ∂W2 and ∂L / ∂b2 by multiplying ∂L / ∂z2 and [ a1 | 1 ].
Step 8 of 11: Layer 1 Activations
Calculate ∂L / ∂a1 by multiplying ∂L / ∂z2 and W2.
Step 9 of 11: Layer 1 ReLU
Calculate ∂L / ∂z1 by multiplying ∂L / ∂a1 with 1 for positive values and 0 otherwise.
Step 10 of 11: Layer 1 Weights & Biases
Calculate ∂L / ∂W1 and ∂L / ∂b1 by multiplying ∂L / ∂z1 and [ x | 1 ].
Gradient Descent
Step 11 of 11: Update Weights & Biases
Update weights and biases (typically a learning rate is applied here).
Insights
💡 Matrix Multiplication is All You Need: Just like in the forward pass, backpropagation is all about matrix multiplications. You can definitely do everything by hand as I demonstrated in this exercise, albeit slow and imperfect. This is why GPU's ability to multiply matrices efficiently plays such an important role in the deep learning evolution.
💡Exploding Gradients: We can already see the gradients are getting larger as we back-propagate up, even in this simple 3-layer network. This motivates using methods like skip connections to handle exploding (or diminishing) gradients as in the ResNet.
Download
Next:
4. SVM by Hand ✍️













z3 should be [3, 3, 7]
There might be a mistake? ∂L / ∂z1 should be [ 1 , -2, 2 , -1] ?