When training a model in epochs, the key metrics to watch are training loss and validation loss. Loss tells us how well the model is learning to predict the right answers. We want the loss to go down as epochs increase. Also, validation accuracy helps us see if the model is improving on new data it hasn't seen before. These metrics show if the model is learning well or just memorizing the training data.
Epoch-based training in PyTorch - Model Metrics & Evaluation
Suppose after training for 5 epochs, the model predicts on a test set of 100 samples. The confusion matrix might look like this:
| Predicted Positive | Predicted Negative |
|--------------------|--------------------|
| True Positive (TP): 40 | False Positive (FP): 5 |
| False Negative (FN): 10 | True Negative (TN): 45 |
From this, we can calculate precision, recall, and accuracy to understand model performance after training.
As epochs increase, the model may become better at finding positive cases (higher recall) but might also make more mistakes (lower precision). For example, in a spam filter, if the model catches almost all spam (high recall) but marks many good emails as spam (low precision), users get annoyed. So, during epoch training, we watch these metrics to find a balance that fits our goal.
Good: Training and validation loss both decrease and stabilize. Validation accuracy improves and stays close to training accuracy. Precision and recall are balanced according to the task needs.
Bad: Training loss keeps going down but validation loss goes up (overfitting). Large gap between training and validation accuracy. Precision or recall very low, meaning the model misses many cases or makes many mistakes.
- Accuracy paradox: High accuracy can be misleading if classes are imbalanced.
- Overfitting: Training loss decreases but validation loss increases.
- Data leakage: Validation data accidentally used in training can give false good metrics.
- Early stopping ignored: Not stopping training when validation loss stops improving wastes time and causes overfitting.
Your model has 98% accuracy but only 12% recall on fraud cases after 10 epochs. Is it good for production? Why or why not?
Answer: No, it is not good. The model misses 88% of fraud cases (low recall), which is dangerous. High accuracy is misleading because fraud cases are rare. We need to improve recall to catch more fraud.
Practice
Solution
Step 1: Understand the definition of an epoch
An epoch means the model sees every example in the training dataset once.Step 2: Differentiate epoch from batch and update
A batch is a subset of data; multiple batches make an epoch. Weight updates happen after each batch, not the whole dataset.Final Answer:
One full pass through the entire training dataset -> Option AQuick Check:
Epoch = full dataset pass [OK]
- Confusing batch with epoch
- Thinking epoch is one weight update
- Mixing test evaluation with training epoch
Solution
Step 1: Recall Python range behavior
range(5) generates 0,1,2,3,4 which is 5 iterations.Step 2: Check other options for iteration count
range(1,5) gives 4 iterations; range(0,6) gives 6; range(5,10) gives 5 but starts at 5, not zero.Final Answer:
for epoch in range(5): -> Option DQuick Check:
range(5) = 5 loops [OK]
- Using range(1,5) which loops 4 times
- Using range(0,6) which loops 6 times
- Starting loop at 5 instead of 0
for epoch in range(2):
total_loss = 0
for batch in dataloader:
output = model(batch)
loss = loss_fn(output, batch.labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
print(f"Epoch {epoch+1} loss: {total_loss}")Solution
Step 1: Analyze the outer loop and print statement
The outer loop runs 2 times (epochs 0 and 1). After each epoch, it prints total_loss.Step 2: Understand total_loss accumulation and printing
total_loss sums batch losses per epoch, so print shows loss after each epoch.Final Answer:
Loss values for epoch 1 and epoch 2 printed -> Option BQuick Check:
Print inside epoch loop prints each epoch loss [OK]
- Thinking only last epoch loss prints
- Confusing batch loss with epoch loss
- Assuming print causes error
for epoch in range(3):
for batch in dataloader:
output = model(batch)
loss = loss_fn(output, batch.labels)
loss.backward()
optimizer.step()
optimizer.zero_grad()
print(f"Epoch {epoch+1} done")Solution
Step 1: Recall correct order of optimizer calls
Zero gradients before backward pass to avoid accumulating gradients.Step 2: Check code order
Code calls loss.backward() before optimizer.zero_grad(), which is wrong.Final Answer:
optimizer.zero_grad() should be called before loss.backward() -> Option CQuick Check:
Zero grads before backward [OK]
- Zeroing gradients after backward
- Swapping optimizer.step() and zero_grad() order
- Thinking no zero_grad needed
Solution
Step 1: Understand early stopping concept
Early stopping monitors validation loss after each epoch to decide if training should continue.Step 2: Match early stopping with epoch-based training
Checking validation loss after each epoch and stopping if no improvement for 3 epochs fits epoch-based training.Final Answer:
After each epoch, check validation loss and stop if no improvement for 3 epochs -> Option AQuick Check:
Early stopping checks validation loss per epoch [OK]
- Checking after batches instead of epochs
- Ignoring validation loss during training
- Waiting until all epochs finish to check
Ready to prove it?
Pass the test and this pattern moves unknown
