← back

Detect Overfitting or Underfitting

#86 · Machine Learning · Easy

⊣ Solve on deep-ml.com

Problem

Given training loss, training accuracy, validation loss, and validation accuracy, determine whether the model is overfitting, underfitting, or performing well (good fit).

Solution

1
2
3
4
5
6
7
8
9
10
11
12
def detect_fitting(train_loss, train_acc, val_loss, val_acc,
                   loss_threshold=0.1, acc_threshold=0.05):
    loss_gap = val_loss - train_loss
    acc_gap = train_acc - val_acc

    if train_acc < 0.6 and val_acc < 0.6:
        return "underfitting"

    if loss_gap > loss_threshold or acc_gap > acc_threshold:
        return "overfitting"

    return "good fit"

Explanation

  1. Underfitting: both training and validation performance are poor. The model is too simple.
  2. Overfitting: training performance is good but validation performance is significantly worse. Large gap between training and validation metrics.
  3. Good fit: both training and validation metrics are close and reasonably high.
  4. The loss_threshold and acc_threshold control how much gap is tolerable.
  5. Remedies: underfitting -> increase model complexity; overfitting -> add regularization, more data, or reduce complexity.

Complexity

  • Time: O(1)
  • Space: O(1)