0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Watch a network learn to read digits
Brightcart's drivers handwrite numbers on delivery notes. We designed a network to read them two episodes ago, and last episode we learned how to compute every gradient by hand. Today we put it all together and actually train it, from scratch, on nearly eighteen hundred handwritten digits. At the start, the network knows nothing. It guesses almost at random: about one in ten right. By the end, it reads digits it has never seen before with ninety-seven percent accuracy. We'll watch every stage: the training loop, the loss curves, the moment it starts to overfit, the mistakes it still makes, and even what its neurons have learned to look for.
0:55Where we are on the map
Season Two, Module Seven, stop four: training a network from scratch.
1:02Data, splits and the training loop
First, the data. Nearly eighteen hundred digits, split three ways: one thousand one hundred and forty-nine for training, two hundred and eighty-eight for validation, and three hundred and sixty locked away as the test set. Each pixel runs from zero to sixteen, so we divide by sixteen to put every input between zero and one. Now the loop that trains every neural network on Earth. Shuffle the training images. Take a batch of thirty-two. Run the forward pass to get probabilities. Measure the loss. Run backpropagation to get the gradients.
Let the optimiser update the weights. Then take the next batch. Thirty-six batches make one pass through the training data, one epoch. After each epoch, we check the validation set to see how we're really doing.
1:55Softmax, cross-entropy and the 2.3 sanity check
The output layer gives ten scores, and softmax turns them into ten probabilities. The loss for each image is cross-entropy: minus the log of the probability given to the correct digit. Ninety percent on the right answer costs about zero point one. Ten percent costs about two point three. And the gradient at the output is wonderfully simple: each probability minus the truth. Predicted minus actual, exactly as in logistic regression. Here's a sanity check professionals always use. An untrained network should spread its bets roughly evenly, about one in ten for each digit.
So the starting loss should be about minus the log of one tenth, which is two point three zero three. Our network starts at two point three one nine. Right on target. If your starting loss is far from that, something's broken, before training has even begun.
2:54Watch it learn: loss and accuracy curves
Let's press play. Watch the dashboard: loss on the left, accuracy on the right, and three digits from the validation set at the bottom. After just one epoch, thirty-six small steps, the network already reads eighty-three percent of the validation digits correctly. You can see the probability bars leaning towards the right answers. After two epochs, ninety-one percent. After three, ninety-four. After five, ninety-five. Then progress slows. By epoch ten, the validation accuracy is about ninety-six percent, and the training loss has dropped to under seven hundredths.
There's the shape from our gradient-descent episode again: a cliff, and then a long, slow slope.
3:40Overfitting and early stopping
Now keep watching. After about epoch ten, the two loss curves part ways. The training loss keeps diving: down to six thousandths by epoch forty. But the validation loss bottoms out and starts creeping back up. Training accuracy reaches one hundred percent by epoch thirty. Pause and predict: does validation accuracy follow it to a hundred?
No. It stays around ninety-six percent. The network has started memorising the quirks of its training images, the overfitting from episode six, happening live. The gap between the curves is the warning sign. The standard response is early stopping: keep a copy of the weights from the epoch that did best on validation, and stop training once validation hasn't improved for a while. Here the best validation accuracy came at epoch twenty-one. For teaching, we let this run continue to epoch forty, so you can see the curves part, and the test score below comes from those final weights.
The epoch-twenty-one checkpoint scores about ninety-six point four percent on the same test set, a difference of just two digits out of three hundred and sixty: well within the noise. Either way, the test set stays locked until training is completely finished.
5:03The final exam and the mistakes
The final exam: three hundred and sixty test digits the network has never seen. Ninety-six point nine percent correct. Eleven mistakes. Let's look at them, in a confusion matrix from episode twelve. Almost everything sits on the diagonal. The most common mistake: an eight read as a one, twice. Look at the images. These eights are thin and squashed, and at eight by eight pixels, they really do look like ones. A nine with an open loop was read as a five. Studying a model's mistakes like this is one of the most valuable habits in machine learning.
It tells you whether the errors are understandable, whether you need more examples of certain cases, or whether something is genuinely wrong.
5:52What did the neurons learn?
What has the network actually learned? We can look. Each hidden neuron has sixty-four incoming weights, one per pixel. Arrange them back into an eight-by-eight square, colour positive weights blue and negative ones red, and you can see what each neuron is looking for. Some look like fragments of strokes. Some respond to a loop, others to a bar through the middle. Each one is a kind of template, and its activation is the dot product between the template and the image: high when the ink lands on the blue parts. Nobody designed these templates.
They emerged from gradient descent and backpropagation, simply because they helped reduce the loss. In Module Eight, we'll see convolutional networks learn far richer visual features in exactly the same way.
6:43Practical habits
A few habits will save you countless hours. Check that the starting loss is near the log of the number of classes. Before training on everything, make sure the network can memorise one tiny batch. If it can't even do that, there's a bug. Always plot training and validation curves. Fix your random seeds, because the starting weights and the shuffle order are random, and you'll want to reproduce results. Change one thing at a time. And keep the test set locked until the very end.
7:18Epochs, batches and steps
Let's count what actually happened during training. With one thousand one hundred and forty-nine training images in batches of thirty-two, one epoch is thirty-six steps. Forty epochs make one thousand four hundred and forty optimiser steps. Each step is one forward pass on thirty-two images, one backward pass, and one update. And because the data is reshuffled every epoch, each image was seen forty times, in a different company each time. That's what "training for forty epochs" really means.
7:53The batch-size trade-off
Why batches of thirty-two? Batch size is a trade-off. Tiny batches give many noisy, cheap steps, which can help generalisation but make poor use of a graphics card. Huge batches give smooth, accurate gradients and process an epoch quickly, but take fewer steps, and often need a larger learning rate with a gentle warm-up. Somewhere in the tens to hundreds is a common sweet spot. Two practical rules: if you change the batch size a lot, retune the learning rate; and remember that batch size is limited by memory, because every example's activations must be stored for the backward pass.
8:34Debugging a network that won't learn
When a network refuses to learn, work through a checklist. Is the starting loss near the log of the number of classes? If not, suspect the labels, the loss function, or the input scaling. Can the network drive the loss to almost zero on a single batch? If it can't even memorise thirty-two examples, there's a bug. Training loss falling but validation stuck? That's overfitting. Loss suddenly exploding into "not a number"? The learning rate is probably too high, or something needs normalising. Loss flat from the start?
The learning rate may be too low, or the gradients are vanishing, which is the subject of episode twenty-eight. This checklist solves a surprising share of real problems.
9:24Look at the hardest examples
One of the most useful habits in deep learning: sort your validation examples by loss and look at the worst ones. They usually fall into three groups. Some are genuinely ambiguous, digits even a person would hesitate over. Some are mistakes in the labels themselves, which you should fix. And some reveal a systematic weakness, like thin, squashed strokes, which you can address with more examples of that kind, or with data augmentation, coming in Module Eight.
9:57Checkpoints and reproducibility
Real training runs save checkpoints: the model's weights, the optimiser's state, the epoch number, the random seeds and the code version. If training is interrupted, it resumes from the last checkpoint. In a real project, when training ends, you load the checkpoint with the best validation score, the early-stopping idea, and only then evaluate on the test set. Record everything, and anyone can rerun your experiment and get the same numbers, give or take tiny differences between hardware. The companion notebook reproduces this exact run, ninety-six point nine percent and all.
10:37From 2,410 parameters to billions
What we just did with two thousand four hundred and ten parameters is, in essence, what happens at enormous scale. An image classifier with millions of parameters, trained on millions of photos, runs the same loop for hours or days on graphics cards. A language model with billions of parameters runs it for weeks on thousands of chips. The loop, the loss, backpropagation and the optimiser stay the same. What changes is the engineering around them: data pipelines, splitting work across many machines, lower-precision numbers, and careful monitoring, all of which we'll meet in later seasons.
11:18Why validation loss rose while accuracy held
Here's a puzzle from our real training run. Between epoch ten and epoch forty, the validation loss got worse, from zero point one five eight to zero point one nine five, yet the validation accuracy got slightly better. How can both be true? Accuracy only asks whether the top choice is right. The loss also asks how confident the model was. As training continues, the network becomes more and more certain about everything, including the few digits it gets wrong. A wrong answer given ninety-eight percent confidence costs minus the log of the two percent left for the right answer, about three point nine, a heavy penalty.
A handful of such confidently wrong answers can push the average loss up, even while the overall accuracy creeps up. So a rising validation loss is a warning of growing overconfidence. If you plan to use the model's probabilities, not just its top choice, check its calibration, and prefer the checkpoint where the validation loss was lowest.
12:24Reading loss curves like a doctor
Loss curves are like X-rays: learn to read them and you can diagnose most training problems at a glance. A flat line from the very start means the learning rate is too small, or there's a bug, like shuffled labels. A curve that shoots upwards, or turns into "not a number", means the learning rate is too large. Training loss falling while validation rises is overfitting. And both curves stuck high, close together, means the model is too simple for the task.
12:58Myth vs Fact
Myth: keep training until the training loss reaches zero. Fact: by then, the network is usually memorising. Our training loss fell to almost nothing while the validation loss got worse. Let the validation set tell you when to stop.
13:14Recap
Let's recap. Training repeats a simple loop: take a batch, run forward, measure the loss, backpropagate, and step. Softmax with cross-entropy gives the simple gradient, probability minus truth. A starting loss near the log of the number of classes is a quick sanity check. Training and validation curves reveal overfitting, and early stopping keeps the best weights. Then we test once, and study the mistakes. One sentence to remember: train on batches, judge on validation, report on test.
13:48Next: optimisers (Adam)
Mission complete. Brightcart's digit reader went from guessing at random to reading ninety-seven percent of unseen handwritten digits, and we watched every step of how it learned. We used an optimiser called Adam without explaining it. Next episode, we open it up: momentum, RMSProp and Adam, the smarter cousins of gradient descent that train almost every modern network. See you on the map.