Back to notes
PyTorch · ~1 month
A PyTorch train/val loop on toy moons
Can I run a standard PyTorch train/eval loop on small data, keep a checkpoint, and write the metrics down?
- Data
- sklearn.datasets.make_moons — 1,000 samples, 20% noise, stratified 80/20 train/val.
- Setup
- MLP, hidden sizes 32 and 16, ReLU. Adam (lr=0.01), cross-entropy, batch 32, 50 epochs on CPU. Checkpoint by validation accuracy.
- What PyTorch handled
- Autograd via loss.backward()
- torch.optim.Adam
- DataLoader
- nn.Sequential
- Also in the repo
- CONCEPTS.md on autograd and optimizers
- Optional: python scripts/train_fastai.py --epochs 50
- Numbers
- Final trainloss 0.071 · accuracy 97.1%Final validationloss 0.029 · accuracy 98.5%Best val accuracy1.00 at epoch 41
- Figures

1,000 points, 20% noise. Not linearly separable. 
Train vs validation loss and accuracy. Val beating train is a small-set quirk on this data. 
Validation-set confusion matrix. - What I take from it
- The loop and checkpointing work end to end. Val hitting 1.00 just means the moons are an easy shape to separate.
- Reproduce
make reproduce # or: python scripts/train_torch.py --epochs 50
The other note: Logistic regression vs a forest on one split