🧠 Three-Layer ANN — Digit Recognition
Step-by-step mathematical example for recognizing a handwritten digit using a 16 × 10 pixel image.
1. What Is a Three-Layer ANN?
In this example, the ANN contains three computational layers:
Layer 2: Hidden Layer 1 → Hidden Layer 2
Layer 3: Hidden Layer 2 → Output Layer
The input image contains:
Total Input Pixels = 16 × 10 = 160
Therefore, the ANN receives 160 input values.
2. Three-Layer ANN Architecture
3. Small Numerical Example
x₁ = 1
x₂ = 0
x₃ = 1
x₄ = 0
Architecture for our mathematical demonstration:
The three output neurons represent:
O₁ → Digit 1
O₂ → Digit 2
1 Input Layer
The four values are sent to every neuron in Hidden Layer 1.
2 Input → Hidden Layer 1
Assume three neurons:
| Input | H₁ Weight | H₂ Weight | H₃ Weight |
|---|---|---|---|
| x₁ | 0.5 | 0.2 | 0.4 |
| x₂ | 0.3 | 0.4 | 0.1 |
| x₃ | 0.8 | 0.6 | 0.7 |
| x₄ | 0.1 | 0.7 | 0.3 |
Bias values:
b₂ = -0.4
b₃ = -0.3
4. Calculate H₁
Step 1 — Weighted Sum
z₁ = 0.5 + 0 + 0.8 + 0 - 0.5
z₁ = 0.8
Step 2 — Sigmoid Activation
H₁ ≈ 0.690
5. Calculate H₂
z₂ = 0.2 + 0 + 0.6 + 0 - 0.4
z₂ = 0.4
H₂ ≈ 0.599
6. Calculate H₃
z₃ = 0.4 + 0 + 0.7 + 0 - 0.3
z₃ = 0.8
H₃ ≈ 0.690
7. Hidden Layer 1 Output
H = [0.690, 0.599, 0.690]
3 Hidden Layer 1 → Hidden Layer 2
Now the three values from Hidden Layer 1 become inputs to Hidden Layer 2.
Hidden Layer 2 contains two neurons:
G₂
| Input | G₁ Weight | G₂ Weight |
|---|---|---|
| H₁ | 0.6 | 0.3 |
| H₂ | 0.5 | 0.8 |
| H₃ | 0.7 | 0.4 |
b₄ = -0.6
b₅ = -0.5
8. Calculate G₁
Substitute the values:
z₄ = 0.414 + 0.2995 + 0.483 - 0.6
z₄ ≈ 0.5965
Activation:
G₁ ≈ 0.645
9. Calculate G₂
z₅ = 0.207 + 0.4792 + 0.276 - 0.5
z₅ ≈ 0.4622
G₂ ≈ 0.614
10. Hidden Layer 2 Output
G = [0.645,0.614]
4 Hidden Layer 2 → Output Layer
The output layer contains 10 neurons in the actual digit-recognition system.
O₁ → 1
O₂ → 2
O₃ → 3
O₄ → 4
O₅ → 5
O₆ → 6
O₇ → 7
O₈ → 8
O₉ → 9
For demonstrating the calculation, we will calculate three outputs:
| Hidden | O₀ | O₁ | O₂ |
|---|---|---|---|
| G₁ | 0.7 | 0.4 | 0.2 |
| G₂ | 0.3 | 0.8 | 0.6 |
b₀ = -0.4
b₁ = -0.3
b₂ = -0.2
11. Calculate O₀ — Digit 0
z₀ = (0.645 × 0.7) + (0.614 × 0.3) - 0.4
z₀ = 0.4515 + 0.1842 - 0.4
z₀ ≈ 0.2357
O₀ ≈ 0.559
12. Calculate O₁ — Digit 1
z₁ = (0.645 × 0.4) + (0.614 × 0.8) - 0.3
z₁ = 0.258 + 0.4912 - 0.3
z₁ ≈ 0.4492
O₁ ≈ 0.610
13. Calculate O₂ — Digit 2
z₂ = (0.645 × 0.2) + (0.614 × 0.6) - 0.2
z₂ = 0.129 + 0.3684 - 0.2
z₂ ≈ 0.2974
O₂ ≈ 0.574
14. Final Prediction
| Output | Digit | Activation |
|---|---|---|
| O₀ | 0 | 0.559 |
| O₁ | 1 | 0.610 |
| O₂ | 2 | 0.574 |
max(0.559, 0.610, 0.574)
= 0.610
Corresponding Output Neuron = O₁
Therefore:
Prediction = Digit 1
15. Scaling the Mathematics to the Real 16 × 10 Image
The classroom example used only four input values. For the actual image:
X = [x₁,x₂,x₃,...,x₁₆₀]
Suppose the network contains:
Hidden Layer 1 = 32 neurons
Hidden Layer 2 = 16 neurons
Output Layer = 10 neurons
Layer 1:
W¹ = 32 × 160
X = 160 × 1
B¹ = 32 × 1
H¹ = Activation(Z¹)
Layer 2:
W² = 16 × 32
H¹ = 32 × 1
B² = 16 × 1
H² = Activation(Z²)
Layer 3:
W³ = 10 × 16
H² = 16 × 1
B³ = 10 × 1
Y = Activation(Z³)
16. Complete Mathematical Flow
↓
160 Pixel Values
↓
Layer 1
Z¹ = W¹X + B¹
H¹ = Activation(Z¹)
↓
Layer 2
Z² = W²H¹ + B²
H² = Activation(Z²)
↓
Layer 3
Z³ = W³H² + B³
Y = Activation(Z³)
↓
10 Output Neurons
↓
argmax(Y)
↓
Predicted Digit 0–9
17. Interactive Step-by-Step Explanation
① Input Layer
X = [x₁,x₂,...,x₁₆₀]
② Hidden Layer 1
H¹ = Activation(Z¹)
The first hidden layer detects basic patterns in the pixels, such as lines, curves and edges.
③ Hidden Layer 2
H² = Activation(Z²)
The second hidden layer combines simpler patterns into larger digit structures.
④ Output Layer
Y = Activation(Z³)
Y = [Y₀,Y₁,Y₂,...,Y₉]
⑤ Final Prediction
Largest output ↓ Corresponding digit
No comments:
Post a Comment