Total Pageviews

Monday, August 31, 2026

๐Ÿง  Two-Layer ANN — Step-by-Step Mathematics Example of handwritten digit recognition using a two-layer Artificial Neural Network.

๐Ÿง  Two-Layer ANN — Step-by-Step Mathematics

Example of handwritten digit recognition using a two-layer Artificial Neural Network.

1. What Does Two-Layer ANN Mean?

A two-layer neural network contains two computational layers:

Layer 1: Input Layer → Hidden Layer

Layer 2: Hidden Layer → Output Layer

For digit recognition, the input image is converted into numerical pixel values.

16 × 10 pixels
= 160 input values

Input: X = [x₁, x₂, x₃, ... , x₁₆₀]

2. Two-Layer ANN Architecture

INPUT LAYER
x₁
x₂
x₃
...
x₁₆₀
OUTPUT LAYER
0
1
2
...
9
160 Input Neurons ↓ Hidden Layer ↓ 10 Output Neurons

Output 0 → Digit 0
Output 1 → Digit 1
...
Output 9 → Digit 9

3. Small Numerical Example

To understand the mathematics easily, we will first use 4 input pixels and 2 hidden neurons. The same mathematics is then expanded to 160 pixels.

Suppose our small input image is:

X = [x₁, x₂, x₃, x₄]

X = [1, 0, 1, 0]

This means:

Pixel Value
x₁ 1
x₂ 0
x₃ 1
x₄ 0

1 Layer 1 — Input to Hidden Layer

Assume the hidden layer contains two neurons:

H₁
H₂

The weights are:

Input Weight → H₁ Weight → H₂
x₁ 0.5 0.2
x₂ 0.3 0.4
x₃ 0.8 0.6
x₄ 0.1 0.7

Bias values:

b₁ = -0.5
b₂ = -0.4

4. Calculate Hidden Neuron H₁

Step 1 — Weighted Sum

z₁ = (x₁ × 0.5) + (x₂ × 0.3) + (x₃ × 0.8) + (x₄ × 0.1) + b₁

Substitute the input values:

z₁ = (1 × 0.5) + (0 × 0.3) + (1 × 0.8) + (0 × 0.1) - 0.5

z₁ = 0.5 + 0 + 0.8 + 0 - 0.5

z₁ = 0.8

Step 2 — Activation

Using the sigmoid activation function:

Sigmoid(z) = 1 / (1 + e⁻แถป)

H₁ = 1 / (1 + e⁻⁰·⁸)

H₁ ≈ 0.690
Hidden Neuron H₁ ≈ 0.690

5. Calculate Hidden Neuron H₂

Step 1 — Weighted Sum

z₂ = (x₁ × 0.2) + (x₂ × 0.4) + (x₃ × 0.6) + (x₄ × 0.7) + b₂

Substitute the input:

z₂ = (1 × 0.2) + (0 × 0.4) + (1 × 0.6) + (0 × 0.7) - 0.4

z₂ = 0.2 + 0 + 0.6 + 0 - 0.4

z₂ = 0.4

Step 2 — Activation

H₂ = 1 / (1 + e⁻⁰·⁴)

H₂ ≈ 0.599
Hidden Neuron H₂ ≈ 0.599

6. Hidden Layer Output

After applying the activation function, the original four inputs have been transformed into two hidden-layer values.

Input:
[1, 0, 1, 0]



Hidden Layer:
[0.690, 0.599]

These two values now become the input to Layer 2.

7. Layer 2 — Hidden Layer to Output Layer

For a complete digit classifier, we use 10 output neurons.

O₀ → Digit 0
O₁ → Digit 1
O₂ → Digit 2
...
O₉ → Digit 9

For simplicity, we calculate three output neurons here:

O₀ → Digit 0
O₁ → Digit 1
O₂ → Digit 2

Suppose the weights are:

Hidden O₀ O₁ O₂
H₁ 0.8 0.3 0.2
H₂ 0.4 0.9 0.5

Bias values:

b₀ = -0.3
b₁ = -0.2
b₂ = -0.4

8. Calculate Output Neuron O₀

z₀ = (H₁ × 0.8) + (H₂ × 0.4) + b₀

z₀ = (0.690 × 0.8) + (0.599 × 0.4) - 0.3

z₀ = 0.552 + 0.2396 - 0.3

z₀ ≈ 0.4916

Apply sigmoid:

O₀ = 1 / (1 + e⁻⁰·⁴⁹¹⁶)

O₀ ≈ 0.621

9. Calculate Output Neuron O₁

z₁ = (H₁ × 0.3) + (H₂ × 0.9) + b₁

z₁ = (0.690 × 0.3) + (0.599 × 0.9) - 0.2

z₁ = 0.207 + 0.5391 - 0.2

z₁ ≈ 0.5461
O₁ = 1 / (1 + e⁻⁰·⁵⁴⁶¹)

O₁ ≈ 0.633

10. Calculate Output Neuron O₂

z₂ = (H₁ × 0.2) + (H₂ × 0.5) + b₂

z₂ = (0.690 × 0.2) + (0.599 × 0.5) - 0.4

z₂ = 0.138 + 0.2995 - 0.4

z₂ ≈ 0.0375
O₂ = 1 / (1 + e⁻⁰·⁰³⁷⁵)

O₂ ≈ 0.509

11. Select the Highest Output

Output Neuron Digit Activation
O₀ 0 0.621
O₁ 1 0.633
O₂ 2 0.509
Maximum Output:
max(0.621, 0.633, 0.509)

= 0.633

Corresponding Neuron = O₁
๐Ÿ”ข Predicted Digit = 1

12. Now Expand the Same Mathematics to 16 × 10 Pixels

The small example used only four inputs so that every calculation could be written manually. For the actual 16 × 10 digit image:

16 × 10 = 160 pixels

X = [x₁,x₂,x₃,...,x₁₆₀]

If we use 32 hidden neurons:

Input Layer = 160 neurons
Hidden Layer = 32 neurons
Output Layer = 10 neurons

The first hidden neuron is calculated as:

z₁ = w₁₁x₁ + w₁₂x₂ + w₁₃x₃ + ... + w₁,₁₆₀x₁₆₀ + b₁

H₁ = Activation(z₁)

The second layer then calculates:

z₀ = v₀₁H₁ + v₀₂H₂ + ... + v₀,₃₂H₃₂ + c₀

O₀ = Activation(z₀)

The same calculation is performed for all ten output neurons.

13. Matrix Form of the Two-Layer ANN

The complete calculation can be written compactly using matrices.

Layer 1:

Z¹ = W¹X + B¹

H = Activation(Z¹)

Layer 2:

Z² = W²H + B²

Output:

Y = Activation(Z²)

For our 160-pixel digit recognizer:

X = 160 × 1
W¹ = 32 × 160
B¹ = 32 × 1
H = 32 × 1
W² = 10 × 32
B² = 10 × 1
Y = 10 × 1

14. Complete Two-Layer ANN Calculation

16 × 10 Image

160 Pixel Values

X = [x₁,...,x₁₆₀]

Layer 1
Z¹ = W¹X + B¹

H = Activation(Z¹)

Layer 2
Z² = W²H + B²

Y = Activation(Z²)

10 Output Values

argmax(Y)

Predicted Digit 0–9

15. Interactive Step-by-Step Summary

Step 1 — Input

16 × 10 = 160 pixels

X = [x₁,x₂,...,x₁₆₀]

Step 2 — Input → Hidden Layer

Z¹ = W¹X + B¹

Each hidden neuron calculates a weighted sum of all input pixels.

Step 3 — Hidden Activation

H = Activation(Z¹)

For example:
H₁ = 0.690
H₂ = 0.599

Step 4 — Hidden → Output

Z² = W²H + B²

Y = Activation(Z²)

The ten output neurons produce scores for digits 0 through 9.

Step 5 — Final Prediction

Prediction = argmax(Y)

Largest Output ↓ Corresponding Digit
The ANN selects the digit having the highest output activation.

16. Important Terms

Input Layer Hidden Layer Output Layer Weight Bias Weighted Sum Activation Function Neuron Pixel 160 Inputs 10 Outputs Sigmoid Prediction argmax

No comments:

Post a Comment