Will It Rain Tomorrow?

Each evening we note three warning signs: is the air humid, how cloudy is it, is the pressure falling. The next day we note whether it rained. The network learns to turn today's signs into a probability of rain. Sigmoid neurons keep every value between 0 and 1, and a new loss, cross-entropy, scores a probability by how surprised we are.

The network

humid x₁ clouds x₂ pressure falling x₃ 1 always 1 (bias) hidden 1 hidden 2 1 always 1 (bias) p(rain) surprise ?

Green = numbers flowing forward. Red dashed = blame flowing backward. Yellow = being computed now. Every neuron is a sigmoid, so it never fully switches off: instead its derivative h(1 − h) gets small when h is near 0 or 1, which quietly shrinks the blame passing through.

FORWARD

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

What is new in this example

Sigmoid instead of ReLU. σ(z) = 1 / (1 + e−z) squashes any number into the range 0 to 1, so the output can be read as a probability. You cannot compute e−z in your head, so use the small table on the right. Its derivative has a lovely shortcut: σ′(z) = σ(z) · (1 − σ(z)), which you can compute from the value you already have.

Cross-entropy instead of squared error. If it rained, the loss is −ln(p); if it stayed dry, −ln(1 − p). In words: the loss is how surprised you are by what happened, given the probability you gave it. Saying 0.5 always costs ln 2 ≈ 0.69. Saying 0.99 for rain and then getting rain costs only 0.01. Saying 0.01 for rain and then getting rain costs 4.6.

The output error signal is just p − y. The derivative of cross-entropy and the derivative of the sigmoid cancel each other almost perfectly, leaving prediction minus truth, exactly what we had in examples 1 and 2. This is why sigmoid outputs and cross-entropy loss are always used together. You never need the loss number itself to do the update.

Ask the network

chance of rain

Sigmoid lookup table

zσ(z)σ′ = σ(1−σ)−ln σ(z)

σ(−z) = 1 − σ(z). Values rounded to 3 decimals, so a hand result may differ from the page in the last digit. The first training step lands exactly on z = 0.

What the network has learned

hidden 1 · z₁ = w₁₁·x₁ + w₁₂·x₂ + w₁₃·x₃ + b₁
w₁₁
w₁₂
w₁₃
b₁
hidden 2 · z₂ = w₂₁·x₁ + w₂₂·x₂ + w₂₃·x₃ + b₂
w₂₁
w₂₂
w₂₃
b₂
output · zₒ = v₁·h₁ + v₂·h₂ + c
v₁
v₂
c

Start: hidden 1 watches humidity and clouds, hidden 2 watches clouds and pressure, the output adds them and starts a little pessimistic (c = −1). Learning rate η = 1: sigmoid gradients are small, so the steps can be big. Parameters are rounded to 4 decimals after each update.

The diary

#humidcloudsfallingrainedp(rain)

Eight evenings and what happened next. Hidden rule the network never sees: it rains when the signs add up to 2 or more (partly cloudy counts ½). Training starts on the quiet day so the first step is all table values.

Training log

stepp(rain)rainedlossoutput (v₁, v₂, c)

Nothing trained yet. Hidden weights after each step are in the cards above; the log shows the output neuron so you can watch it change.

Cheat sheet

Forward pass

z₁ = w₁₁·x₁ + w₁₂·x₂ + w₁₃·x₃ + b₁   h₁ = σ(z₁)
z₂ = w₂₁·x₁ + w₂₂·x₂ + w₂₃·x₃ + b₂   h₂ = σ(z₂)
zₒ = v₁·h₁ + v₂·h₂ + c   p = σ(zₒ)
L = −ln p if it rained, −ln(1 − p) if dry

Backward pass

δₒ = p − y
∂L/∂v₁ = δₒ·h₁   ∂L/∂v₂ = δₒ·h₂   ∂L/∂c = δₒ
δ₁ = δₒ · v₁ · h₁(1 − h₁)
δ₂ = δₒ · v₂ · h₂(1 − h₂)
∂L/∂w₁ᵢ = δ₁·xᵢ   ∂L/∂b₁ = δ₁
∂L/∂w₂ᵢ = δ₂·xᵢ   ∂L/∂b₂ = δ₂

Update

every parameter: p ← p − η · ∂L/∂p   η = 1