Model Pipeline - Validation loop
The validation loop checks how well the trained model performs on new, unseen data. It helps us see if the model is learning the right patterns without memorizing the training data.
The validation loop checks how well the trained model performs on new, unseen data. It helps us see if the model is learning the right patterns without memorizing the training data.
Loss
0.8 |*
0.7 | *
0.6 | *
0.5 | *
0.4 | **
0.3 |
+--------
1 2 3 4 5 Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.75 | 0.60 | Validation loss is high, accuracy is low, model is just starting. |
| 2 | 0.55 | 0.72 | Loss decreased, accuracy improved, model is learning. |
| 3 | 0.45 | 0.78 | Validation metrics improving steadily. |
| 4 | 0.40 | 0.81 | Model continues to generalize better. |
| 5 | 0.38 | 0.83 | Validation loss stabilizes, accuracy plateaus. |
model.eval() sets the model to evaluation mode, disabling dropout and batch norm updates.model.eval() is correct; others do not exist or are for training.model.eval()
correct = 0
with torch.no_grad():
for inputs, labels in val_loader:
outputs = model(inputs)
predicted = outputs.argmax(dim=1)
correct += (predicted == labels).sum().item()
accuracy = correct / len(val_loader.dataset)
print(f"Accuracy: {accuracy:.2f}")model.eval()
for inputs, labels in val_loader:
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
total_loss += loss.item()model.eval()
total_correct = 0
total_samples = 0
with torch.no_grad():
for inputs, labels in val_loader:
outputs = model(inputs)
predicted = outputs.argmax(dim=1)
# What goes here?
accuracy = total_correct / total_samples
print(f"Validation Accuracy: {accuracy:.2f}")Pass the test and this pattern moves unknown