SVHN Deep Learning Track — Lesson 1 of 6

Neural Networks — How Machines Learn to Think

The mathematical building block behind all of modern AI — explained from a single neuron upward, through to the 68,000-parameter model we built for digit recognition.

1. The Biological Inspiration

The human brain contains roughly 86 billion neurons. Each neuron receives electrical signals through branching structures called dendrites, integrates those signals in its cell body, and if the total input exceeds a threshold, fires an output signal down its axon to the next neuron. Information is encoded not in any single neuron, but in the patterns of activation across billions of them.

Artificial neural networks were designed to mimic this structure — not biologically faithfully, but mathematically. The core insight is simple: a computation unit that receives multiple weighted inputs, sums them, and applies a threshold function is both easy to build in code and, when combined in large numbers, astonishingly powerful.

BIOLOGICAL NEURON Dendrites (inputs) Cell body Axon (output) x₁ x₂ x₃ Integrate Σ signals if Σ > threshold fire! ARTIFICIAL NEURON x₁ ×w₁ x₂ ×w₂ x₃ ×w₃ + bias b weighted Σ + b z activation f(z) output f(Σwᵢxᵢ + b)

Each input is multiplied by a weight (how important that signal is). A bias shifts the result. An activation function then transforms the sum — introducing the non-linearity that makes deep networks capable of learning complex patterns. The weights are the parameters the network learns during training.

2. A Single Artificial Neuron

Let's make this concrete. A single neuron has these components:

z = w₁x₁ + w₂x₂ + … + wₙxₙ + b     output = f(z)

Worked Example: A Brightness-Detection Neuron

Suppose we have a neuron that looks at 3 pixels of an image to decide if a region is "bright." The neuron has been trained and its current weights are:

Inputs (pixel values, 0=black, 1=white): x₁ = 0.9 (bright pixel) x₂ = 0.8 (bright pixel) x₃ = 0.1 (dark pixel) Weights (learned): w₁ = 0.7 (strongly trusts x₁) w₂ = 0.6 (trusts x₂) w₃ = -0.4 (dark pixels reduce activation) Bias: b = -0.3 (raises the threshold slightly) Weighted sum: z = (0.9 × 0.7) + (0.8 × 0.6) + (0.1 × -0.4) + (-0.3) z = 0.63 + 0.48 + (-0.04) + (-0.3) z = 0.77 With ReLU activation: f(0.77) = max(0, 0.77) = 0.77 ➔ "bright region detected"

If all three pixels had been dark (e.g., 0.1, 0.1, 0.1), the weighted sum would be about −0.21, and ReLU would output 0 — "not a bright region." The neuron has learned a simple but meaningful detector.

3. Layers — From Pixels to Predictions

A single neuron can only learn a single simple function. The power of neural networks comes from stacking many neurons into layers, and stacking many layers into a network. Each layer learns to represent the data at a different level of abstraction.

Input Layer
Raw pixel values

No computation — just holds the data. 1,024 units for a 32×32 image.
Hidden Layer 1
Low-level features

Learns edges, brightness gradients, simple textures.
Hidden Layer 2
Mid-level features

Combines edges into corners, stroke junctions, curves.
Output Layer
Class probabilities

10 neurons — one per digit class (0–9). Softmax applied.

Why does depth help? Because each layer builds on what the previous layer learned. If Layer 1 can detect edges, Layer 2 doesn't need to relearn what an edge is — it can combine edge detectors to find corners. If Layer 2 can find corners and curves, Layer 3 can combine them into loops and strokes. By Layer 4, the network has built up enough abstract vocabulary to recognise an entire digit.

Key Insight A single hidden layer with enough neurons can theoretically approximate any function (the Universal Approximation Theorem). But in practice, deep networks (many thinner layers) learn far more efficiently than wide networks (one huge layer). Depth lets the network learn a hierarchy of reusable features.

The input layer just holds the raw data — no transformation is applied. The hidden layers are where all the learned computation happens. The output layer produces the final prediction — for classification, it typically has one neuron per class, with a softmax function converting raw scores to probabilities.

4. Weights Are Everything

When a network is first created, all its weights are initialised randomly — small random values, often drawn from a normal distribution. At this point, the network's predictions are essentially random too. Training is the process of systematically adjusting those weights until the predictions become accurate.

The total number of weights and biases in a network is called its parameter count. To calculate it for a dense (fully-connected) layer:

Parameters in a dense layer = (inputs × outputs) + outputs (for biases)

Let's count for a concrete example — a network with a 1,024-unit input (flattened 32×32 image), a hidden layer of 64 neurons, and an output layer of 10 neurons:

LayerShapeWeightsBiasesTotal
Dense 11024 → 6465,5366465,600
Dense 2 (output)64 → 1064010650
Total Parameters66,250

That single hidden layer of 64 neurons already requires 65,600 numbers to be learned. In the SVHN project, ANN Model 1 had 68,010 parameters total — the network was 3 layers deep with 64 hidden units in its single hidden layer. Every one of those 68,010 numbers was initialised randomly and adjusted through training.

Why parameter count matters More parameters = more capacity to learn complex patterns. But also more risk of memorising noise (overfitting), more compute needed, and more training data required. Choosing the right model size is one of the central design decisions in deep learning.

5. The Forward Pass

When you feed an input to a neural network and get a prediction out, you are performing a forward pass. Data flows left to right through the network: input layer → hidden layers → output layer. Each layer receives a tensor of values, multiplies by its weight matrix, adds biases, and applies an activation function.

INPUT (1024 pixels of a 32×32 image, flattened) ↓ DENSE LAYER 1 [1024 → 64] z = W₁x + b₁ (matrix multiply: 1024 values → 64 values) a = ReLU(z) (apply activation element-wise) ↓ DENSE LAYER 2 [64 → 10] z = W₂a + b₂ (64 values → 10 raw scores, one per digit class) output = Softmax(z) (convert to probabilities summing to 1.0) ↓ PREDICTION: the class with highest probability

Worked Example: Predicting the Digit "7"

An image of the digit "7" enters the network. After passing through all the layers, the output layer (before softmax) produces 10 raw scores called logits. Softmax converts them to probabilities:

Raw logits (before softmax): Class 0: -1.8 Class 1: 0.1 Class 2: -0.9 Class 3: -1.2 Class 4: -0.7 Class 5: -1.1 Class 6: -1.4 Class 7: 4.2 Class 8: -0.6 Class 9: 0.2 After Softmax (probabilities summing to 1.0): [0.02, 0.01, 0.03, 0.01, 0.01, 0.01, 0.01, 0.88, 0.01, 0.01] ┬─────────────────────────────────────────────────┴ The digit at index 7 has 88% probability. Prediction: 7 ✓

The network is most confident about class 7 (88%), with small residual probabilities spread across the other classes — particularly class 9 (1%) and class 2 (3%), which may have some visual similarity. The argmax of the probability vector gives the final prediction.

Why Softmax? The output layer produces raw real-valued scores that can be any number. Softmax converts these into a valid probability distribution: all values between 0 and 1, all summing to exactly 1. This allows us to interpret the output as confidence levels and use cross-entropy loss for training.

6. What ANNs Can't See — The Limits of Flattening

Here is the fundamental problem with applying a standard (fully-connected) neural network to images. When you flatten a 32×32 image into a 1,024-element vector, you destroy the spatial structure of the image.

Original image (32×32): Pixel at row 5, column 3 → position (5, 3) Pixel at row 5, column 4 → position (5, 4) ← these are adjacent! Pixel at row 6, column 3 → position (6, 3) ← these are adjacent! After flattening (1×1024): Pixel (5, 3) → index 5*32 + 3 = 163 Pixel (5, 4) → index 5*32 + 4 = 164 (adjacent in 1D → ok) Pixel (6, 3) → index 6*32 + 3 = 195 (was adjacent, now 32 apart) The neuron connected to index 163 has NO IDEA that index 195 was right next to it. Every spatial relationship must be re-learned from position alone.

For a standard neural network, the weight connecting input 163 to a hidden neuron has no relationship to the weight connecting input 195 to the same neuron. The network must learn, independently for every position, that pixels in vertical proximity are likely to be part of the same stroke.

This is enormously inefficient and explains why ANNs plateau quickly on image tasks. Convolutional Neural Networks (CNNs) solve this by keeping the 2D structure intact and sharing weights across spatial positions — one edge-detector filter works everywhere in the image, regardless of position.

An ANN looks at a flattened list of numbers. A CNN looks at an image. That difference in perspective is responsible for the entire gap between the two model families on visual tasks.
In the SVHN Project

ANN Model 1 (68,010 parameters, 3 layers) scored 63.57% test accuracy — barely better than chance for a 10-class problem. ANN Model 2 (310,570 parameters, 5 layers + dropout + batch normalisation) reached 77.33% — a significant gain from regularisation and capacity, but still held back by the fundamental limitation of flattening images. The breakthrough came from switching to CNNs: CNN Model 2 hit 92.22% with fewer parameters than ANN Model 2, simply by preserving spatial structure through convolutional layers.

Interactive: Forward Pass & Backpropagation

Run Forward Pass to make a prediction. Then run Backprop to watch the error signal travel backwards, computing a gradient for every single weight. Or hit Full Training Step to see both in one go — that is exactly one iteration of training.

ANN · 4 layers · 68,010 parameters
▶ Forward Pass predicts  →  ↩ Backprop sends the error back  →  ⚡ Full Step does both

How Does the Model Know Which Weight to Change?

The gradient ∂L/∂wᵢ tells the model exactly which direction and how much to adjust each weight. Two key rules: the sign tells direction (increase or decrease), and the magnitude tells step size — and it scales with the input value xᵢ.

Gradient Descent — Zoomed In
Phase 1 of 3 — Forward Pass

Network Values

Run the forward pass first — the model makes a prediction and calculates the loss.