Overfitting, Underfitting & Validation

Deep Learning with Keras and TensorFlow 10 min min read Updated: Nov 16, 2026 Beginner
Overfitting, Underfitting & Validation
Beginner Topic 7 of 9

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.
ProblemTraining scoreValidation scoreMeaning
UnderfittingLowLowModel is too simple or trained too little
Good fitHighHigh and closeModel generalizes
OverfittingVery highMuch lowerModel 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?

DatasetPurpose
Training setThe model learns from it
Validation setUsed during training to tune settings and detect overfitting
Test setUsed 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators