---
title: "What a full training loop looks like"
lesson_id: "04"
---

# What a full training loop looks like

- **Lesson ID:** 04
- **Goal:** Show the shape of a GPT training loop in nanoGPT, mark the repo as deprecated, and point current training work at nanochat.
- **Human lesson:** [04-a-full-training-loop.html](04-a-full-training-loop.html)

## Prerequisites

- Lessons 01 to 03, or enough comfort with autograd, tensors, and tokens to read a short trainer.

## Inputs, outputs, and artifacts

- **Inputs:** The question of what a full training loop looks like.
- **Outputs:** A clear reading of `train.py` and `model.py`, plus a laptop-scale Shakespeare path.
- **Artifacts:** A short teaching `train_step` sketch, and three progressive mermaid diagrams of the loop.

## Agent build steps

1. Point at [nanoGPT](https://github.com/karpathy/nanoGPT). Quote the README's roughly 300-line loop and 300-line model, and the current counts 336 and 330.
2. Put the November 2025 deprecation and the pointer to nanochat near the top.
3. Teach the loop: batch, forward, loss, backward, optimizer step. Use progressive diagrams.
4. Show a short, original `train_step` sketch with a MPS-or-CPU-or-CUDA device line. This is not a useful model and not a copy of nanoGPT.
5. Give the Shakespeare character path, including `uv` and `--compile=False`.
6. Link [Attention Is All You Need](https://arxiv.org/abs/1706.03762), the [GPT-2 paper](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf), and lecture 7.
7. Tell the reader to use nanochat for a current training run.

## Constraints

Keep the language simple, friendly, and literal. No quizzes, no em dashes. Brand Edge FDE only. Do not encourage a new OpenWebText or 8×A100 run in this deprecated repo.

## Key concepts

- nanoGPT's README describes a roughly 300-line training loop and a roughly 300-line GPT model. The current files are 336 and 330 lines.
- The loop is batch, forward, loss, backward, optimizer step, repeated.
- The model predicts the next token. The loss is cross-entropy on that prediction.
- A November 2025 README update marks nanoGPT deprecated and points to nanochat. Read this repo for clarity, not to start a new large training run.
- The laptop-scale path in the README is character-level Shakespeare.

## Takeaways

- Open train.py and model.py and point to where each step of the loop lives.
- If you want to feel a run, use the Shakespeare character example, and disable torch.compile if your machine needs that.
- When you are ready to train a current GPT-2-level chat model, go to nanochat, not to this repo.

## Acceptance checks

- Deprecation and the nanochat pointer are explicit.
- The loop is taught with progressive diagrams and a short sketch.
- Agent brief link is present.
- [ ] Proceed to [lesson 05 brief](05-what-pytorch-hides.llms.md).
