How a neural network computes
From one neuron to a layer to a network: what actually happens as data flows through, and why depth does something different from width.
The idea
A neuron takes several numbers, multiplies each by its own weight, adds them up, adds an offset, and passes the result through a simple function that sets everything negative to zero. That is it.
A layer is a row of such neurons side by side. A network is a sequence of layers. The first sees the raw data; every later one sees what the previous made of it.
What depth achieves
In an image, the first layer detects brightness jumps, the second corners from those, the third simple shapes, the fourth object parts. Each level composes the building blocks of the one before. Hence "deep learning".
A network by hand
import numpy as np
rng = np.random.default_rng(0)
def relu(x): return np.maximum(0, x)
# He initialisation: standard deviation sqrt(2/fan_in). With the wrong
# initialisation the signal dies out across the layers.
def layer(fan_in, fan_out):
return (rng.normal(0, np.sqrt(2 / fan_in), (fan_in, fan_out)),
np.zeros(fan_out))
W1, b1 = layer(784, 256)
W2, b2 = layer(256, 128)
W3, b3 = layer(128, 10)
x = rng.normal(size=(32, 784)) # 32 images of 28x28
a1 = relu(x @ W1 + b1)
a2 = relu(a1 @ W2 + b2)
logits = a2 @ W3 + b3 # 32 x 10, not yet probabilities
for name, a in [("a1", a1), ("a2", a2)]:
print(name, "mean", round(float(a.mean()), 3),
"zero fraction", round(float((a == 0).mean()), 3))The zero fraction is the most important diagnostic when building a network. Above 90 percent means many neurons are dead and contribute nothing.
The parameter count
| Layer | In | Out | Parameters |
|---|---|---|---|
| 1 | 784 | 256 | 200,960 |
| 2 | 256 | 128 | 32,896 |
| 3 | 128 | 10 | 1,290 |
| Total | 235,146 |
The formula
Without σ, W⁽²⁾(W⁽¹⁾x + b⁽¹⁾) + b⁽²⁾ = (W⁽²⁾W⁽¹⁾)x + (W⁽²⁾b⁽¹⁾ + b⁽²⁾), again
a single linear map. Twenty layers would have exactly the expressiveness of one.
Initialisation
The reason is a variance calculation: with ReLU about half the outputs are zero,
halving the variance. The factor 2 compensates. Without that correction,
activation variance falls by half per layer and has dropped to 2⁻²⁰ ≈ 1e-6
after twenty.
Universal approximation, read correctly
The theorem says a network with one hidden layer can approximate any continuous function on a compact domain arbitrarily well. What it does not say:
- How many neurons that takes. The count can grow exponentially in the input dimension.
- Whether training finds those weights. The theorem is an existence statement.
- Whether the approximation holds outside the domain considered. It does not.
The theorem is therefore not an argument for shallow networks. Deep networks reach the same accuracy with exponentially fewer neurons because they reuse features rather than placing them side by side.
Related courses and sources
Batch Normalization
Why normalising intermediate values is what makes deep training stable in the first place.
For anyone asking why normalisation is what makes deep training possible.
Deep Learning
The standard work by Goodfellow, Bengio and Courville, free to read. Mathematically dense, complete, and in its foundational parts timeless.
For the systematic route. Strong as a reference, too dense as a first read.
Deep Learning Specialization
Five courses from the basics of neural networks to sequence models. Thorough, with programming exercises, and in places older than current practice.
For anyone who can program and wants to work through the field completely.
Dive into Deep Learning
A textbook with runnable code beside every derivation. Each chapter opens as a notebook you can recompute yourself.
For anyone who wants to compute along while reading; every chapter opens as a notebook.
Netron
Opens a model file and draws its structure. The fastest way to see what a delivered model actually contains.
For a quick look inside a delivered model before putting it into operation.
Neural networks, explained visually
From a single weight through gradient descent to the attention mechanism. The best available intuition for what the formulas describe.
Watch it before your first textbook, not after. It saves weeks of confusion.
Practical Deep Learning for Coders
Starts with a working model in the first hour and supplies the theory afterwards. The shortest route from basic Python to a model you trained yourself.
For impatient readers who know Python: your first model runs within the first hour.
PyTorch tutorials
The official guides, from a first tensor operation to distributed training. Short, runnable and continuously updated.
For getting into the library most research is written in.
TensorFlow Playground
A neural network in the browser with sliders for layers, activation and learning rate. You see in seconds what each knob does.
For anyone who wants to see what learning rate, layers and activation actually do.
Understanding Deep Learning
A modern textbook with unusually clear figures that already covers transformers and diffusion models in full. Free as a PDF.
For a modern entry point; it already covers transformers and diffusion in full.