Code

300 steps: the first real accuracy

The tests pass — your training loop works. Now run it in full: 300 steps and a number that is finally no longer 10 %.

The panel shows the complete files

On the right now are the complete versions of your files:

  • neuron.py — cross_entropy filled in; nothing else changed;
  • train_scratch.py — train written, and main() added with two imports (time, load_fashion).

main() runs train with steps=300, lr=0.1, prints selected steps, and then tries the same weights on the official test set — 10,000 pictures that training never saw. test_train_scratch.py got one test for the same.

Run it

python train_scratch.py
step   0  loss 2.3207  accuracy 10.2%
step   1  loss 2.0832  accuracy 28.5%
step   2  loss 1.9216  accuracy 58.5%
step  50  loss 0.8289  accuracy 74.1%
step 100  loss 0.7066  accuracy 77.7%
step 150  loss 0.6480  accuracy 79.5%
step 200  loss 0.6110  accuracy 80.6%
step 250  loss 0.5847  accuracy 81.2%
step 300  loss 0.5646  accuracy 81.7%
accuracy on the official test set: 80.0%
done in 5.6s

(Seconds depend on the machine; ours took from 2 to 6 s.)

What these numbers say

The answers to "Guess":

  1. After one step: 28.5 %.
  2. After 300: 81.7 % (on the training pictures).
  3. On the test set: 80.0 %, i.e. 1.7 points lower.

The loss fell 2.3207 → 0.5646 — to a quarter of where it started. Accuracy from 10.2 % to 81.7 %. That is not chance: in the last lesson chance moved within 3 % – 15 %.

The first jump is the biggest, afterwards — small change. From step 0 to 2 accuracy rose 10.2 → 58.5; from 50 to 300 — 74.1 → 81.7, i.e. 250 steps gave less than the second step did. Here you see for the first time diminishing returns, which will accompany you through the course.

The training-versus-test gap is small (1.7 points). This model has 7,850 numbers and 10,000 pictures; it has nothing to memorise with. That gap will become a problem a few lessons from now.

Does it repeat?

With seed = 42 the first step gives 28.5 %. Five different seeds (0–4), the same program:

seed   step 0   step 1   step 300 (training)   test
 0      3.3 %   32.7 %        81.8 %           79.9 %
 1     11.4 %   35.2 %        81.8 %           80.0 %
 2      8.0 %   27.8 %        81.9 %           80.0 %
 3     10.9 %   28.9 %        81.8 %           80.0 %
 4     11.3 %   30.5 %        81.7 %           80.0 %

Two different stories:

  • The start is widely scattered. The initial accuracy is from 3.3 % to 11.4 % (remember lesson 3's 3.3 – 15.2 %), after one step — from 27.8 % to 35.2 %. "One step gives 28.5 %" is this seed's answer, not a law of nature.
  • The end hardly changes. After 300 steps — 81.7 – 81.9 % on training and 79.9 – 80.0 % on test, the loss 0.5642 – 0.5646. This model (one layer + softmax) has one "lowest point", and from any start it arrives there.

Your numbers may differ by a point or two with another PyTorch version or processor. What matters is the shape: the loss falls from ~2.3 to ~0.56, accuracy climbs from ~10 % to ~80 %.