๐ง ANN Backpropagation: Weight & Bias Update
A simple numerical example showing how an ANN calculates the loss, sends the error backward, and updates its weights and biases.
1. What Are We Going to Learn?
During training, an ANN initially has randomly selected weights and biases. The network makes a prediction. That prediction is compared with the correct answer.
The goal is to make the loss smaller.
2. Small ANN Used in This Example
1 Input → 1 Hidden → 1 Output
We deliberately use one neuron in each layer so that every calculation can be understood easily.
3. Training Data
| Input X | Correct Output Y |
|---|---|
| 1 | 1 |
The ANN receives:
Actual / Target: y = 1
4. Initial Weight and Bias
Suppose the ANN starts with:
| Parameter | Initial Value |
|---|---|
| Input → Hidden Weight | 0.5 |
| Hidden Bias | 0.1 |
| Hidden → Output Weight | 0.4 |
| Output Bias | 0.1 |
| Learning Rate ฮท | 0.1 |
1 Forward Propagation — Hidden Layer
First calculate the weighted sum of the hidden neuron.
Substitute the values:
zโ = 0.6
Now apply sigmoid activation.
h = 1 / (1 + e⁻⁰·⁶)
h ≈ 0.646
2 Forward Propagation — Output Layer
The hidden neuron output becomes the input to the output neuron.
Substitute:
zโ = 0.2584 + 0.1
zโ = 0.3584
Apply sigmoid:
ลท ≈ 0.589
3 Calculate the Loss
For this demonstration, we use Mean Squared Error for one example:
Actual value:
ลท = 0.589
Therefore:
= ½(0.411)²
= ½(0.1689)
Loss ≈ 0.08445
4 Backpropagation — Output Error
Now the ANN asks:
For sigmoid:
Therefore:
= 0.589 × 0.411
≈ 0.242
The derivative of the loss with respect to prediction is:
= 0.589 - 1
= -0.411
Therefore the output error signal is:
= (-0.411)(0.242)
ฮดโ ≈ -0.09946
5 Calculate Gradient of Output Weight
The output weight connects hidden neuron h to output neuron.
Substitute:
≈ -0.06425
6 Calculate Gradient of Output Bias
∂L/∂bโ ≈ -0.09946
The bias gradient is simply the output error signal.
7 Update Output Weight
The basic gradient-descent update rule is:
For the output weight:
= 0.4 + 0.006425
w₂(new) ≈ 0.40643
8 Update Output Bias
= 0.1 - (0.1 × -0.09946)
= 0.1 + 0.009946
bโ(new) ≈ 0.10995
9 Send Error Back to Hidden Neuron
The hidden neuron also needs to know how much it contributed to the final error.
We already know:
w₂ = 0.4
h = 0.646
Calculate the hidden sigmoid derivative:
= 0.646(1-0.646)
= 0.646 × 0.354
≈ 0.2287
Therefore:
ฮดโ ≈ -0.00910
10 Calculate Gradient of Hidden Weight
Since:
ฮดโ ≈ -0.00910
Therefore:
≈ -0.00910
11 Calculate Hidden Bias Gradient
∂L/∂bโ ≈ -0.00910
12 Update Hidden Weight
= 0.5 - (0.1 × -0.00910)
= 0.5 + 0.00091
w₁(new) ≈ 0.50091
13 Update Hidden Bias
= 0.1 - (0.1 × -0.00910)
= 0.1 + 0.00091
bโ(new) ≈ 0.10091
14. Before vs After Weight & Bias
| Parameter | Before | Gradient | After |
|---|---|---|---|
| w₁ | 0.50000 | -0.00910 | 0.50091 |
| bโ | 0.10000 | -0.00910 | 0.10091 |
| w₂ | 0.40000 | -0.06425 | 0.40643 |
| bโ | 0.10000 | -0.09946 | 0.10995 |
15. Why Did the Weights Increase?
Notice that the gradients are negative.
Therefore: New Weight > Old Weight
The network predicted:
Target = 1
The prediction was too low. Gradient descent therefore moves the parameters in a direction that should increase the prediction and reduce the loss.
16. Perform Another Forward Pass
Now use the updated parameters.
bโ = 0.10091
w₂ = 0.40643
bโ = 0.10995
Hidden Layer:
zโ ≈ 0.60182
h ≈ 0.6462
Output Layer:
zโ ≈ 0.3726
ลท ≈ 0.5921
Old Prediction ≈ 0.589
17. New Loss
= ½(0.4079)²
New Loss ≈ 0.08318
New Loss ≈ 0.08318
✅ LOSS DECREASED
18. Complete Training Cycle
19. Most Important Formulas
Weighted Sum
Sigmoid
Sigmoid Derivative
Mean Squared Error
Gradient Descent
b(new) = b(old) - ฮท × ∂L/∂b
20. Interactive Step-by-Step
① Forward Pass
h = Sigmoid(zโ)
zโ = hw₂ + bโ
ลท = Sigmoid(zโ)
② Prediction
③ Loss
L ≈ 0.08445
④ Backpropagation
Output Error ↓ Output Weight Gradient ↓ Hidden Error ↓ Hidden Weight Gradient
⑤ Update Weights
⑥ Update Bias
⑦ New Loss
New Loss ≈ 0.08318
No comments:
Post a Comment