Background
Sections
IntroductionModule 01 β€” Tensors 🧊01 Β· Creating Tensors 🧊02 Β· Indexing & Reshaping πŸ”ͺ03 Β· Tensor Math βž—04 Β· Device Placement πŸ–₯️⚑Module 02 β€” Autograd βš™οΈ01 Β· The Computational Graph πŸ•ΈοΈ02 Β· Backward Pass & Gradients ⬅️03 Β· Turning Autograd Off πŸ›‘04 Β· Gradient Gotchas πŸͺ€Module 03 β€” Neural Networks01 Β· The `nn.Module` Basics 🧱02 Β· Common Layers 🧩03 Β· Building a Network πŸ—οΈ04 Β· Inspecting Models πŸ”Module 04 β€” Data Handling πŸ—‚οΈ01 Β· Dataset Basics πŸ“‡02 Β· The DataLoader 🚚03 Β· Transforms 🎨04 Β· Splits & Built-in Datasets βœ‚οΈModule 05 β€” The Training Loop πŸ”01 Β· Loss Functions 🎯02 Β· Optimizers βš™οΈ03 Β· The Training Loop πŸ”04 Β· Evaluation & Metrics πŸ“ŠModule 06 β€” Saving & Loading πŸ’Ύ01 Β· `state_dict` Basics πŸ’Ύ02 Β· Checkpoints & Resuming ⏸️03 Β· Loading for Inference πŸš€04 Β· Best Model & Early Stopping πŸ…Module 07 β€” Computer Vision πŸ‘οΈ01 Β· Convolutions πŸ”²02 Β· CNN Architecture πŸ›οΈ03 Β· Transfer Learning πŸ”04 Β· Image Classification Project πŸ§ͺModule 08 β€” NLP & Transformers πŸ’¬01 Β· Text Data & Tokenization πŸ”€02 Β· Embeddings 🧭03 Β· Recurrent Layers & LSTMs πŸ”„04 Β· Intro to Transformers ⚑Module 09 β€” Ecosystem: PyTorch Lightning ⚑01 Β· Why Lightning? πŸ€”02 Β· The LightningModule 🧩03 Β· The Trainer πŸŽ›οΈ04 Β· DataModules & Callbacks 🧰Module 10 β€” Deployment & Optimization πŸš€01 Β· Exporting Models πŸ“¦02 Β· `torch.compile` ⚑03 Β· Inference Optimization πŸͺΆ04 Β· Serving & Next Steps πŸŽ“

Module 05 β€” The Training Loop πŸ”

3 min read

This is the moment everything converges. Tensors, autograd, models, and data all come together into the loop that actually makes a network learn.

Every module so far has been building toward this one. You have tensors (01) and the autograd engine that differentiates them (02); you can build a model out of layers (03); and you can serve it batched, shuffled data (04). The training loop is the conductor that brings them together: for each batch, it runs a forward pass to get predictions, measures how wrong they are with a loss function, uses autograd to compute gradients, and asks an optimizer to nudge every weight in the direction that reduces the loss. Repeat that a few thousand times and a random pile of numbers becomes a working model.

The loop itself is short β€” often a dozen lines β€” but every line earns its place, and the order matters. Zero the old gradients, forward, compute loss, backward, step: get the sequence wrong and training silently breaks (remember gradient accumulation from Module 02?). This module also covers the two modes a model can be in β€” train() and eval() β€” and why every serious training run alternates between learning on the training set and measuring honestly on a validation set. Master this loop once and you'll reuse the exact same skeleton for CNNs, Transformers, and everything else in Phase 3.

This module is split into three sub-modules β€” work through them in order.

πŸ“š Sub-Modules

# Sub-Module What you'll learn
01 Loss Functions What a loss measures, and choosing MSELoss vs. CrossEntropyLoss for your task
02 Optimizers SGD vs. Adam, the learning rate, and the zero_grad() β†’ step() rhythm
03 The Training Loop Assembling the full loop, backprop, and model.train() vs. model.eval()
04 Evaluation & Metrics A proper validation loop, computing accuracy, and reading curves to spot overfitting

🎯 By the end of this module, you'll be able to...

  • Pick and apply the right loss function for regression or classification.
  • Configure an optimizer and explain what the learning rate controls.
  • Write a complete, correct training loop with a validation pass β€” from scratch.
  • Evaluate a model with accuracy metrics and diagnose overfitting from train/val curves.

βœ… Prerequisites

All of Phase 1, plus the data pipeline from Module 04. The gradient-accumulation and train()/eval() ideas from Module 02 and Module 03 become concrete here.


⬅️ Prev module: 04 Β· Data Handling Β· ➑️ Next module: 06 Β· Saving & Loading