Overfitting, Underfitting & Validation
Two Common Problems
Simple example: A student memorizes the exact questions from last year's exam (overfitting). He gets perfect marks on old papers but fails on new ones. Another student barely studies (underfitting) and fails everything. A good student understands the ideas and can answer new questions.
| Problem | Training score | Validation score | Meaning |
|---|---|---|---|
| Underfitting | Low | Low | Model is too simple or trained too little |
| Good fit | High | High and close | Model generalizes |
| Overfitting | Very high | Much lower | Model memorized the training data |
How to See It: Learning Curves
python
import matplotlib.pyplot as plt
plt.plot(history.history["loss"], label="train loss")
plt.plot(history.history["val_loss"], label="validation loss")
plt.legend()
plt.show()
- Both curves high: underfitting
- Both curves go down together: healthy training
- Train loss goes down but validation loss goes up: overfitting
Why Do We Need Three Datasets?
| Dataset | Purpose |
|---|---|
| Training set | The model learns from it |
| Validation set | Used during training to tune settings and detect overfitting |
| Test set | Used once at the end for the final honest score |
python
# Keras can split validation data for you
model.fit(x_train, y_train, epochs=10, validation_split=0.2)
# Or provide your own
model.fit(x_train, y_train, epochs=10, validation_data=(x_val, y_val))
How to Fix Underfitting
- Add more neurons or layers
- Train for more epochs
- Use better features or a better architecture
- Reduce regularization
How to Fix Overfitting
- Get more training data, or use data augmentation (for images)
- Use a smaller model
- Add Dropout or L2 regularization
- Stop training early with
EarlyStopping - Use Batch Normalization and a good learning rate
Early Stopping Example
python
early = keras.callbacks.EarlyStopping(
monitor="val_loss",
patience=3, # wait 3 epochs without improvement
restore_best_weights=True
)
model.fit(x_train, y_train, epochs=50, validation_split=0.2, callbacks=[early])
Result
Training stops by itself when the validation loss stops improving, and the best weights are restored.
Rule of thumb: Always watch the validation curve, not just the training accuracy.
Conclusion
Every deep learning project fights between underfitting and overfitting. Use validation data, read the curves and apply the right fix.
Quick Summary: Compare training and validation scores. Fix overfitting with more data, dropout, regularization and early stopping.

