Theory

Watch: the gap opens

You write nothing here. This is a run of the finished program of this lesson — so you can see what you will be doing before you have done anything. An MLP with two hidden layers of 256 neurons (269 322 numbers), 30 epochs, trained on the first 10 000 images and validated on 5 000 others the model never sees.

$ python train.py
mlp: 269,322 parameters
epoch  1  loss 0.5513  train acc 79.7%  |  val loss 0.5891  val acc 78.6%
epoch  2  loss 0.4429  train acc 84.1%  |  val loss 0.5091  val acc 82.2%
...
epoch  8  loss 0.2865  train acc 89.1%  |  val loss 0.4237  val acc 85.1%
epoch  9  loss 0.2554  train acc 90.7%  |  val loss 0.3956  val acc 85.8%
epoch 10  loss 0.2708  train acc 90.0%  |  val loss 0.4386  val acc 85.0%
epoch 11  loss 0.3196  train acc 88.7%  |  val loss 0.5238  val acc 83.3%
epoch 12  loss 0.2321  train acc 91.5%  |  val loss 0.4069  val acc 86.1%
...
epoch 20  loss 0.1395  train acc 94.7%  |  val loss 0.4271  val acc 86.9%
...
epoch 28  loss 0.0835  train acc 97.0%  |  val loss 0.4812  val acc 87.0%
epoch 29  loss 0.0916  train acc 96.7%  |  val loss 0.4977  val acc 86.6%
epoch 30  loss 0.0867  train acc 97.1%  |  val loss 0.4913  val acc 86.7%
final: train 97.1%  val 86.7%  gap +10.4%
done in 29.1s

(... — omitted lines; the real output has them. Seed 42, my machine.)

What to notice

1. The two numbers start together and part. In the first epoch the difference is 1.1 points (79.7 vs 78.6). At the ninth — 4.9 (90.7 vs 85.8). At the thirtieth — 10.4 (97.1 vs 86.7). Training accuracy climbs and climbs; validation accuracy nearly stops after epoch ten. The same network, the same data — only different images.

2. Validation loss hits bottom at epoch 9 and starts rising. The bottom is 0.3956. At epoch 30 — 0.4913. Meanwhile training loss keeps falling (0.2554 → 0.0867). The network becomes more and more sure of its answers, and on new images more and more often sure and wrong.

3. Validation accuracy does not fall. It wanders around 86–87 % and is even a little higher in the last epoch than at the ninth (86.7 vs 85.8). So loss and accuracy disagree here: the loss says "getting worse", the accuracy says "about the same". Loss is the more sensitive instrument, because it also punishes a correct but unconfident answer. Keep this difference in mind.

"97.1 %" can no longer be called "our model's accuracy". The real number is 86.7 %. And even that is not final (in step 7 you will see why).