THE GENIUS PROJECT
๐Ÿ”ฌ Lesson 4 of 6 ยท Live lab

Train a Network, Live

๐Ÿ•’ About 35 minutes ๐Ÿ“‰ Watch loss fall in real time ๐Ÿ Same model in Keras for Colab

Everything so far becomes real on this page. Below is a genuine neural network, weights, gradients and all, implemented in your browser. Its job: learn 30 days of Kingston temperatures and predict day 31. You choose its size and learning rate, press Train, and watch gradient descent do its thing, epoch by epoch.

The setup

The data: 30 daily maximum temperatures (a realistic Kingston July: warm, a gentle warming trend, a rainy-day dip). The task: given 3 consecutive days, predict the next day. That gives us 27 training examples from 30 days. We hold out the last 6 examples as a validation set: the network never trains on them, so they tell us honestly whether it learned the pattern or memorised the answers.

The network: 3 inputs → a hidden layer you size yourself → 1 output. Exactly the Lesson 1 machinery, trained with exactly the Lesson 2 algorithm.

๐Ÿ“ Why we split the data: past papers vs the real exam

Training data is like practising past exam papers: the network sees the questions AND the answers, over and over. The validation set is the real exam: fresh questions it has never seen before. If you score 95% on past papers you have practised twenty times but only 40% on the real exam, the papers got memorised rather than understood. That is exactly why we hide the last 6 examples from the network during training: its score on those is the only score you can trust.

What actually happens when you press Train

No magic. Every epoch, the network runs the same four-step loop from Lessons 1 and 2 on each of the 21 training examples, dozens of times per second:

โžก๏ธForward passweighted sums → prediction
๐Ÿ“Measure loss(prediction − truth)²
๐ŸงญFind gradientswhich way is downhill?
๐Ÿ”งNudge weightsa tiny step downhill

The nudge is the Lesson 2 update rule, applied to every weight in the network at once:

new weight = old weight − learning rate × gradient

One worked example so the numbers feel real. Take a one-weight network where prediction = weight × input. The weight is 2, the input is 2, and the true answer is 6. The forward pass gives 2 × 2 = 4, so the error is 4 − 6 = −2. For squared loss the gradient is 2 × error × input = 2 × (−2) × 2 = −8. With a learning rate of 0.05, the update is 2 − 0.05 × (−8) = 2.4. Next forward pass: 2.4 × 2 = 4.8, which is closer to 6 than 4 was. One small improvement. Repeat it for every weight, on every example, for hundreds of epochs, and you get the show you are about to watch: the loss sliding downhill and the orange line locking onto the dots.

๐Ÿ”
New Word!Epoch

One full pass through the entire training set. When the counter below says epoch 50, the network has seen every training example 50 times, taking one small downhill step after each look.

๐Ÿ”ฌ The lab

Experiments to run (in order)
  1. Baseline run: hidden size 8, learning rate 0.05. Press Train. Watch both loss curves fall and the orange prediction line snap onto the data.
  2. Tiny brain: hidden size 2. It struggles to fit the wiggles: underfitting.
  3. Big brain, long training: hidden size 32. Watch the train loss keep falling while the validation loss stalls or creeps up: overfitting, memorisation in action.
  4. Reckless learning rate: 1.0. Enjoy the chaos, then respect the knob forever.
Epoch0
Train lossโ€“
Validation lossโ€“
Day 31 predictionโ€“

Teal dots: the 30 real days. Orange line: what the network currently predicts for each day (given the previous 3). The shaded zone is the validation region the network never trains on.

Loss curves per epoch. Teal: training loss. Orange: validation loss. When orange stops following teal down, you are watching overfitting begin.

Troubleshooting: when training looks wrong

Check your understanding

Your train loss is 0.02 (tiny) but your validation loss is 4.5 (huge). What happened?

Great train loss + bad validation loss = memorisation, not learning. Real fixes: fewer neurons, more training data, early stopping, or regularisation. Validation loss is the number that tells the truth.

๐Ÿ The same model in Keras

Everything you just did by hand is three lines in Keras, the friendly deep learning library you will use in your final assignment on Google Colab (free, runs in the browser, nothing to install; you used it in Week 2). Read it and notice you can now explain EVERY line:

weather_net.py ยท Keras
import numpy as np
from tensorflow import keras

# 30 days of Kingston max temperatures (ยฐC)
temps = np.array([30.1, 30.4, 29.8, 30.6, 31.0, 30.7, 31.2,
                  31.5, 30.9, 28.4, 29.1, 30.8, 31.4, 31.6,
                  31.2, 31.8, 32.0, 31.5, 31.9, 32.2, 31.7,
                  30.2, 30.9, 31.8, 32.1, 32.4, 32.0, 32.5,
                  32.3, 32.6])

# windows of 3 days -> next day
X = np.array([temps[i:i+3] for i in range(27)])
y = temps[3:]

model = keras.Sequential([
    keras.layers.Dense(8, activation="relu", input_shape=(3,)),
    keras.layers.Dense(1)
])
model.compile(optimizer=keras.optimizers.Adam(0.01), loss="mse")
model.fit(X, y, epochs=200, validation_split=0.2, verbose=0)

tomorrow = model.predict(temps[-3:].reshape(1, 3), verbose=0)
print(f"Day 31 prediction: {tomorrow[0][0]:.1f} ยฐC")
>>> Output: Day 31 prediction: 32.6 ยฐC

Line by line: Dense(8, activation="relu") is your hidden layer of 8 Lesson-1 neurons. loss="mse" is the Lesson-2 wrongness measure. Adam(0.01) is gradient descent with a 0.01 learning rate (plus momentum). epochs=200 is 200 passes through the data, and validation_split=0.2 holds out 20% to catch overfitting. You know all of this now, for real.

๐Ÿš€ Try it yourself before the assignment

Open colab.research.google.com, click New notebook, paste the code above, and press the play button. Then experiment: change the hidden layer to 32, the epochs to 1000, and watch the validation loss in model.fit(..., verbose=1). Five minutes of this and the final assignment will feel easy.

๐Ÿ“Œ Lesson recap

1) A validation set is data held back to test honesty. 2) Underfitting: too little capacity, misses the pattern. Overfitting: too much memorisation, fails on new data. 3) In Keras, the whole lab is Dense + mse + Adam + fit. You are ready for the real thing.

Next: the final assignment. Two models, two real predictions. โšฝ๐ŸŒฆ๏ธ