An Epoch Loop with Validation
Evaluate after each pass through the data.
An Epoch Loop with Validation is a free Deep Learning Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What an Epoch Means
One epoch is a single full pass over your entire training set. Real training repeats this pass many times so the model keeps improving.
The Outer Loop
Wrap your whole routine in a loop over epochs. Each turn of this outer loop trains once and then checks progress on validation.
for epoch in range(num_epochs):
train_one_epoch()
validate()The Inner Training Loop
Inside an epoch you loop over batches from the DataLoader. Each batch runs the familiar forward, loss, backward, step rhythm.
for x, y in train_loader:
optimizer.zero_grad()
loss = loss_fn(model(x), y)
loss.backward()
optimizer.step()Switch to Train Mode
Before training a pass, call model.train(). It turns on layers like dropout and batch norm that should behave differently while learning.
model.train()Switch to Eval Mode
Before validating, call model.eval(). This freezes dropout and uses running batch-norm stats so your scores are steady and fair.
model.eval()No Gradients While Validating
Validation only reads the model, so wrap it in torch.no_grad(). Skipping the graph saves memory and runs noticeably faster.
with torch.no_grad():
for x, y in val_loader:
out = model(x)Track the Running Loss
Add up each batch's loss across the epoch, then divide by the count. This average loss is one clean number to compare epoch to epoch.
total += loss.item() * x.size(0)
epoch_loss = total / len(loader.dataset)Measure Validation Loss
Compute the same average on the validation set. This validation loss reveals how well the model generalizes beyond what it trained on.
Print Progress Each Epoch
Log both losses every epoch so you can watch the curves. Seeing the trend live makes overfitting easy to catch the moment it starts.
print(epoch, train_loss, val_loss)Read the Two Curves
Healthy training shows both losses falling. When validation loss starts climbing while training keeps dropping, the model is beginning to overfit.
Don't Backward on Validation
A frequent bug is calling backward() during validation. Never update weights from val data, or you contaminate your honest measurement.
Quick Check
What should you call right before running the validation pass?
Recap
Each epoch trains in train mode, then validates in eval mode under no_grad. Track both losses and watch the gap to catch overfitting early. 📈
Frequently asked questions
Is the “An Epoch Loop with Validation” lesson free?
Yes — the full text of “An Epoch Loop with Validation” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “An Epoch Loop with Validation”?
Evaluate after each pass through the data. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “An Epoch Loop with Validation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Split Train, Validation & Test
- An Epoch Loop with Validation
- Save & Load with state_dict
- Early Stopping on Val Loss