Learning · Deep learning · Step 1

Layers, weights, activations

A handwritten 3 is trivial for your brain — and you can't describe how you did it. There's no algorithm that takes a 28×28 grid of pixel brightnesses and spits out the digit. That gap is the entire reason neural networks exist: instead of writing the recognition rule, we write a parametric function flexible enough to express many rules, then learn the parameters from labelled examples.

This page builds up the smallest such function — a feedforward network — from its simplest building block (a single number) up to the matrix equation that defines one layer. The canonical example throughout is the same one Michael Nielsen and 3Blue1Brown use to teach this material: handwritten-digit recognition on 28×28 images.

A neuron is a number

The atomic unit is the neuron — just a number between 0 and 1. The number is the neuron's activation: high means "lit up / firing", low means "quiet". That's it; there is no other piece of state inside a neuron.

For digit recognition the input layer is 784 neurons — one per pixel of the 28×28 image — each holding a pixel's brightness (0 = black, 1 = white). The output layer is 10 neurons, one per possible answer; the activations there read as the network's confidence that the digit is 0, 1, 2, … 9. Between them sit hidden layers — neurons whose meaning isn't fixed in advance; whatever feature happens to be useful for the task, training will arrange the weights so a hidden neuron fires for it.

⋮ 0 1 2 3 4 5 6 7 8 9 input · 784 hidden · 16 hidden · 16 output · 10

Total trainable parameters in this network — every weight on every wire, plus one bias per non-input neuron — 784·16 + 16 + 16·16 + 16 + 16·10 + 10 = 13 002. Each one is a real number; the entire job of training is finding 13 002 numbers that make the output activations point at the right digit for every input image.

Why have layers at all?

A single layer mapping pixels straight to digits would have to memorise every digit shape independently — a recipe for overfitting and zero generalisation. Layers buy you composability: each one can build on the abstractions the previous one already extracted.

The hope, never designed in but consistently observed when the training works:

This isn't enforced by the math — the math is just matrix multiply, add, squash. The hierarchy emerges because that's the cheapest way for the network to fit the data. When people say a network has "learned" a feature, they mean: some neuron consistently fires for some pattern in the input, because that pattern turned out to be useful for the loss.

Weights — what pattern is this neuron looking for?

Every connection between layers has a weight — a real number, positive or negative. A neuron in layer l+1 computes a weighted sum of all the activations in layer l:

z  =  w0·a0  +  w1·a1  +  w2·a2  +  …  +  wn−1·an−1  +  b
  

Think of the weight vector as the pattern this neuron is looking for. If you took one digit-recognising hidden neuron's 784 weights and arranged them back into a 28×28 grid (positive = blue, negative = red, brightness = magnitude), you'd see something concrete: maybe a horizontal bar near the top, with negative weights surrounding it. That neuron's activation will spike when the input image has a horizontal bar in that spot — the weighted sum lights up because the positive weights line up with bright pixels and the negatives line up with dark ones.

Bias — the firing threshold

The weighted sum alone isn't quite enough. We also need to control when a neuron decides to fire — how much evidence it needs before turning on. That's the bias: a single number added to the weighted sum that shifts the activation threshold.

With a bias of −2, the weighted sum has to climb above 2 before the neuron starts turning on; the neuron is "shy". With a bias of +5, the neuron is already fired by default and only quiets down when its inputs strongly disagree; the neuron is "eager". Each non-input neuron has its own bias, learned independently — that's where the + 16 + 16 + 10 extra parameters in the count above came from.

One layer in one line

Put the weighted sum, the bias, and a final "squash this into the firing range" step together, and one layer's math fits in one line:

a(l+1)  =  σ(  W(l) · a(l)  +  b(l)  )
  

a(l) is the activations at layer l (the input layer is a(0)), W(l) is the weight matrix of shape (out-dim × in-dim) — one row per output neuron, holding that neuron's input weights — b(l) is the bias vector (length = out-dim), and σ is the activation function, applied to every element. A network with L hidden layers chains L+1 of these operations; the parameters that get learned during training are every W(l) and b(l) in the stack.

The activation σ

We need σ because we want activations bounded — historically because biological neurons fire in a limited range, today because unbounded linear layers can be collapsed into one big linear layer and gain nothing from depth (see the interactive below). The classic choice is the sigmoid, σ(z) = 1 / (1 + e−z) — a smooth S-curve mapping every real number into (0, 1). z = 0 gives 0.5; large positive z saturates near 1; large negative near 0.

Modern networks almost always swap sigmoid for something cheaper or better-behaved:

A concrete example

A tiny but complete network — 2 inputs, 3 hidden units, 1 output — with every weight and bias spelled out. The numbers on arrows are weights; the number inside each hidden / output node is its bias. Input nodes hold the input vector x.

0.5 0.8 −0.2 −0.3 0.4 0.6 1.0 −0.5 0.3 1.0 1.0 +0.1 −0.2 +0.0 +0.2 a⁽⁰⁾ a⁽¹⁾ a⁽²⁾ input · 2 hidden · 3 output · 1

The same network as matrices

The diagram above, written out as the linear-algebra it actually is. Each layer has one weight matrix and one bias vector — exactly the shapes you see below. On disk these are flat float arrays; on a GPU they're the arguments of matmul and add kernels. Same structure, four million parameters or four — only the numbers change.

W⁽¹⁾  =  ⎡  0.5  −0.3 ⎤        b⁽¹⁾  =  ⎡  0.1 ⎤
         ⎢  0.8   0.4 ⎥                  ⎢ −0.2 ⎥
         ⎣ −0.2   0.6 ⎦                  ⎣  0.0 ⎦
              (3 × 2)                         (3)


W⁽²⁾  =  ⎡  1.0  −0.5   0.3 ⎤   b⁽²⁾  =  [ 0.2 ]
              (1 × 3)                         (1)


a⁽¹⁾  =  σ( W⁽¹⁾ · a⁽⁰⁾ + b⁽¹⁾ )         ← layer 1
a⁽²⁾  =  σ( W⁽²⁾ · a⁽¹⁾ + b⁽²⁾ )         ← layer 2

Read each row of W⁽ˡ⁾ as "the weights into one output neuron". Row 0 of W⁽¹⁾ is [ 0.5, −0.3 ] — the two weights feeding hidden unit 0; combined with bias b⁽¹⁾₀ = 0.1 it produces the pre-activation z⁽¹⁾₀ = 0.5·x₀ − 0.3·x₁ + 0.1.

Forward pass with x = [1, 1]

Plug the input vector through the matrices. Two matmuls, two bias adds, two element-wise sigmoids — that's the entire prediction.

a⁽⁰⁾  =  x  =  [ 1,  1 ]


z⁽¹⁾  =  W⁽¹⁾ · a⁽⁰⁾  +  b⁽¹⁾
       =  ⎡  0.5·1  +  (−0.3)·1 ⎤  +  ⎡  0.1 ⎤    =   ⎡  0.3 ⎤
          ⎢  0.8·1  +  ( 0.4)·1 ⎥     ⎢ −0.2 ⎥        ⎢  1.0 ⎥
          ⎣ −0.2·1  +  ( 0.6)·1 ⎦     ⎣  0.0 ⎦        ⎣  0.4 ⎦

a⁽¹⁾  =  σ(z⁽¹⁾)  ≈  [ 0.574,  0.731,  0.599 ]     ← sigmoid, element-wise


z⁽²⁾  =  W⁽²⁾ · a⁽¹⁾  +  b⁽²⁾
       =  1.0·0.574  +  (−0.5)·0.731  +  0.3·0.599  +  0.2
       ≈  0.588

a⁽²⁾  =  σ(z⁽²⁾)  ≈  0.643                          ← the network's output

If σ were ReLU instead of sigmoid, the hidden activations would be z⁽¹⁾ as-is (all three are positive here, so ReLU passes them through unchanged), then 1.0·0.3 + (−0.5)·1.0 + 0.3·0.4 + 0.2 = 0.12, and ReLU leaves that as 0.12 — same matrix multiplies, very different output (0.12 vs 0.643). Try it in the interactive below.

Try it

A single layer with 2 inputs and 3 outputs. Edit the input vector x, the weights W, or the biases b, and pick an activation — the pre-activation z = Wx + b and the post-activation a = σ(z) update live.

Input x (2)
Activation σ
Weights W (3 × 2)
Bias b (3)
Presets:

What to notice

Step exam

Answer all 3 questions correctly to complete this step.

  1. What does ONE layer of a feed-forward neural network compute?

  2. If every activation function in a deep network is `linear` (σ(z) = z), the whole network is equivalent to:

  3. Which of these is NOT a commonly used activation function in modern deep networks?