Train Your Own GPT: The Training Loop Explained in PyTorch | LLM Course #24
How does an LLM actually learn? In Lecture 24 of the LLM Masterclass we write the training loop for ChotuGPT in PyTorch: random batches where the target is the next token, the forward pass and loss, loss.backward(), and an AdamW step with warm-up, a cosine learning-rate schedule and gradient clipping. We watch the training and validation loss fall, spot overfitting, and finally ChotuGPT completes "After my morning chai, I feel ___" with "fresh". 💻 Code for this lecture (branch lecture-24): https://github.com/programmingline/chotugpt/tree/lecture-24 Lecture 24 of 32 — LLM Masterclass · Module 5: Training an LLM ⚠️ ChotuGPT is a simplified teaching model (891,648 parameters, trained for about 3.5 minutes on 2 CPU cores on our machine). The losses shown are its real printed output; your training times will differ. In this lecture: • The training loop: batch → forward pass → loss → backward pass → optimizer step, repeated 1,500 times • Batches: random 256-token windows; the targets are the same window shifted by one token; 8 × 256 = 2,048 tokens per step • AdamW: momentum, a step size per weight, and weight decay 0.1 • Learning rate 0.001 with 50 warm-up steps, then a cosine schedule down to 10%; gradient clipping at 1.0 • Loss curve: 6.30 at step 0 (close to ln 512 = 6.24, random guessing) → 1.53 → 0.65 → 0.4765 • Validation loss on text the model never trains on: best 0.5402 at step 1200, then it rises — overfitting • 3,072,000 tokens seen = 31.8 passes over the training text; early stopping / keeping the best checkpoint • Result: "After my morning chai, I feel" → fresh (0.874); work → tired, coffee → awake, lunch → sleepy Chapters: 00:00 Lecture 23 quiz answer 00:13 The training loop 00:27 One small loop 00:54 Before we start 01:00 Making a batch 01:26 AdamW and the learning rate 02:03 Watching the loss fall 02:37 Validation loss and overfitting 03:15 The loop in PyTorch 03:47 Quiz 04:09 ChotuGPT says fresh 04:38 Recap & next lecture ✅ Lecture 23 quiz answer: A, repeats waste compute and make the model memorise text instead of learning general patterns. ❓ Quiz: Training loss keeps falling but validation loss starts rising. This is… A) Underfitting B) Tokenization C) Overfitting. Comment your answer. I reveal it in Lecture 25! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 23 — Pretraining data ⏭ Next: Lecture 25 — Scaling laws, GPUs and parallelism Sources: D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", ICLR 2015 (arXiv:1412.6980). I. Loshchilov and F. Hutter, "Decoupled Weight Decay Regularization" (AdamW), ICLR 2019 (arXiv:1711.05101). I. Loshchilov and F. Hutter, "SGDR: Stochastic Gradient Descent with Warm Restarts" (cosine schedule), ICLR 2017 (arXiv:1608.03983). PyTorch documentation: torch.optim.AdamW, torch.nn.utils.clip_grad_norm_. #LLM #MachineLearning #PyTorch…more
How does an LLM actually learn? In Lecture 24 of the LLM Masterclass we write the training loop for ChotuGPT in PyTorch: random batches where the target is the next token, the forward pass and loss, loss.backward(), and an AdamW step with warm-up, a cosine learning-rate schedule and gradient clipping. We watch the training and validation loss fall, spot overfitting, and finally ChotuGPT completes "After my morning chai, I feel ___" with "fresh". 💻 Code for this lecture (branch lecture-24): https://github.com/programmingline/chotugpt/tree/lecture-24 Lecture 24 of 32 — LLM Masterclass · Module 5: Training an LLM ⚠️ ChotuGPT is a simplified teaching model (891,648 parameters, trained for about 3.5 minutes on 2 CPU cores on our machine). The losses shown are its real printed output; your training times will differ. In this lecture: • The training loop: batch → forward pass → loss → backward pass → optimizer step, repeated 1,500 times • Batches: random 256-token windows; the targets are the same window shifted by one token; 8 × 256 = 2,048 tokens per step • AdamW: momentum, a step size per weight, and weight decay 0.1 • Learning rate 0.001 with 50 warm-up steps, then a cosine schedule down to 10%; gradient clipping at 1.0 • Loss curve: 6.30 at step 0 (close to ln 512 = 6.24, random guessing) → 1.53 → 0.65 → 0.4765 • Validation loss on text the model never trains on: best 0.5402 at step 1200, then it rises — overfitting • 3,072,000 tokens seen = 31.8 passes over the training text; early stopping / keeping the best checkpoint • Result: "After my morning chai, I feel" → fresh (0.874); work → tired, coffee → awake, lunch → sleepy Chapters: 00:00 Lecture 23 quiz answer 00:13 The training loop 00:27 One small loop 00:54 Before we start 01:00 Making a batch 01:26 AdamW and the learning rate 02:03 Watching the loss fall 02:37 Validation loss and overfitting 03:15 The loop in PyTorch 03:47 Quiz 04:09 ChotuGPT says fresh 04:38 Recap & next lecture ✅ Lecture 23 quiz answer: A, repeats waste compute and make the model memorise text instead of learning general patterns. ❓ Quiz: Training loss keeps falling but validation loss starts rising. This is… A) Underfitting B) Tokenization C) Overfitting. Comment your answer. I reveal it in Lecture 25! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 23 — Pretraining data ⏭ Next: Lecture 25 — Scaling laws, GPUs and parallelism Sources: D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", ICLR 2015 (arXiv:1412.6980). I. Loshchilov and F. Hutter, "Decoupled Weight Decay Regularization" (AdamW), ICLR 2019 (arXiv:1711.05101). I. Loshchilov and F. Hutter, "SGDR: Stochastic Gradient Descent with Warm Restarts" (cosine schedule), ICLR 2017 (arXiv:1608.03983). PyTorch documentation: torch.optim.AdamW, torch.nn.utils.clip_grad_norm_. #LLM #MachineLearning #PyTorch