Example 4 · two hidden layers · 7 inputs · 51 parameters · train and test

Rain Tomorrow, Two Hidden Layers

Same question as example 3, now with a fuller diary kept in Australia: 72 evenings across a year, with temperature, whether it rained today, and the month. January is the hottest month and July the coldest, and summer brings the storm season. The network is the shape of a real one: two hidden layers of ReLU neurons, a sigmoid output, random starting weights. Twelve days are held back and never trained on, so we can see whether the network learned the weather or just memorized the diary.

The network

Every line is a weight: darker means larger, dotted means negative. Values in circles are the current forward pass. Green = flowing forward, red dashed = blame flowing back, yellow = being computed now, dotted circle = ReLU gate closed.

Keyboard: → or space for Next, ← for Back.

What is new in this example

Preparing the inputs. Neurons only see numbers, and they work best when the numbers are on similar scales. Yes/no signs are already 0 or 1. Temperature in °C is divided by 30 so 24 °C becomes 0.8. The month is the interesting one: if we fed 1 to 12, December (12) and January (1) would look as far apart as possible, yet in Australia they are the two hottest, stormiest months of the year. So the month becomes two numbers, sin and cos of its angle around the year, and neighbouring months get neighbouring values.

Random starting weights. With 51 weights we can no longer pick a sensible start by hand, and starting every weight at the same value would make every neuron in a layer identical forever. So each weight starts as a small random number, scaled by how many inputs the neuron has. A seed makes the "random" numbers repeatable.

Blame adds up. In example 2 each hidden neuron fed one output, so it got one share of blame. Here every layer-1 neuron feeds all three layer-2 neurons, so its blame is the sum of the three shares coming back, each weighted by the connection it travelled along. That is the whole of backpropagation: repeat this, one layer at a time, however deep the network.

Train versus test. Only the first 60 days are used for updates. The last 12 are scored but never learned from. If the test loss falls with the training loss, the network is picking up the real pattern. If the training loss keeps falling while the test loss rises, it has started memorizing, which is called overfitting.

The hidden rule that generated the diary

score = humid + clouds + falling + ½·rained today + ½·storm season (Nov–Feb) + ½·(25 °C or warmer and humid). It rains tomorrow when score ≥ 2. Temperatures follow a southern-hemisphere year: about 30 °C in January, 5 °C in July. The other fields come from a seeded random generator, so the page and nn4.py see exactly the same 72 days.

Ask the network

chance of rain

Loss per epoch

training days (60)held-out test days (12)

Mean cross-entropy over each set, measured after every epoch. Train at least one epoch to see it.

epochtrain lossrighttest lossright

The weights right now

Each row is one neuron: its weight on every input, then its bias. Learning rate η = 0.05. Parameters are rounded to 4 decimals after each update.

The diary

#monhumcldfall°Ctodaynextp

Days 1–60 train the network; days 61–72 are the held-out test. The last column is the network's current probability of rain, green when it lands on the right side of 0.5.

Cheat sheet

Forward pass, layer by layer

layer 1: z⁽¹⁾ⱼ = Σᵢ w⁽¹⁾ⱼᵢ·xᵢ + b⁽¹⁾ⱼ   h⁽¹⁾ⱼ = ReLU(z⁽¹⁾ⱼ)
layer 2: z⁽²⁾ₖ = Σⱼ w⁽²⁾ₖⱼ·h⁽¹⁾ⱼ + b⁽²⁾ₖ   h⁽²⁾ₖ = ReLU(z⁽²⁾ₖ)
output: zₒ = Σₖ vₖ·h⁽²⁾ₖ + c   p = σ(zₒ)
L = −ln p if rain, −ln(1 − p) if dry

Backward pass, layer by layer

δₒ = p − y
δ⁽²⁾ₖ = δₒ · vₖ · ReLU′(z⁽²⁾ₖ)
δ⁽¹⁾ⱼ = (Σₖ δ⁽²⁾ₖ · w⁽²⁾ₖⱼ) · ReLU′(z⁽¹⁾ⱼ)
every weight: ∂L/∂w = δ of its neuron × the input it multiplied
every bias: ∂L/∂b = δ of its neuron

Update

p ← p − η · ∂L/∂p   η = 0.05