Learning Curves: Spot Bias & Variance Problems
In this article
Learning curves plot a model's training and validation error against the amount of training data used. They are a direct visual tool for diagnosing whether a machine learning model suffers from high bias (underfitting) or high variance (overfitting). By reading the shape and gap of these curves, you can decide the most effective next step: add more data, change model complexity, or add regularization. You only need a trained model and a way to evaluate it on training and validation splits at increasing dataset sizes.
What You'll Need
- A dataset you can split into training and validation (or test) sets.
- A model that can be trained multiple times (e.g., scikit-learn, TensorFlow, PyTorch).
- A way to compute a performance metric (e.g., accuracy, mean squared error, log loss) on both training and validation sets.
- Plotting capability (matplotlib, seaborn, or any charting tool).
Step-by-Step
- Split your data. Hold out a fixed validation set (e.g., 20–30% of the data) that will never be used for training. The remaining data will be used to create progressively larger training subsets.
- Define a list of training set sizes. Choose a sequence of increasing sizes, for example: 10%, 20%, 40%, 60%, 80%, 100% of the training portion. Ensure the smallest size still gives the model enough examples to learn something meaningful.
- For each size, repeat the following:
- Randomly sample a subset of the training data of that size (without replacement).
- Train the model on that subset.
- Evaluate the model on the training subset itself (to get training error) and on the fixed validation set (to get validation error).
- Record both scores.
- Repeat the sampling multiple times. To reduce noise, run the training/evaluation for each size 3–5 times with different random samples, then average the errors. This is especially important for small dataset sizes where results vary a lot.
- Plot the curves. On the x-axis, put the training set size (or percentage). On the y-axis, put the error (or loss). Draw one line for training error and one for validation error.
- Interpret the gap and the trend. Look at two things: the vertical gap between the curves and whether the validation error is still decreasing at the largest training sizes.
- Diagnose high variance (overfitting). If the training error is very low and the validation error is much higher, with a large gap that does not close as you add data, the model has high variance. It memorizes the training set but fails to generalize.
- Diagnose high bias (underfitting). If both training and validation errors are high and close together, the model is too simple. Adding more data will not help because the model cannot fit even the training data well.
- Diagnose the sweet spot. If the validation error is low and close to the training error, the model generalizes well. The curves should converge and plateau at a low error level.
- Act on the diagnosis. For high variance, try: more training data, regularization (e.g., L1/L2, dropout), simpler model (fewer parameters), or early stopping. For high bias, try: a more complex model, more features, less regularization, or longer training.
Common Mistakes
- Using the same validation set for all sizes is correct, but reusing it for model selection afterwards is not. Once you pick a model based on validation performance, you need a separate test set to report final performance.
- Plotting accuracy instead of error. Accuracy curves are inverted (higher is better) and can be confusing. Stick to error or loss so that "higher is worse" consistently.
- Not averaging multiple runs. A single run at a small training size can be noisy and mislead you into seeing a trend that isn't there.
- Ignoring the scale of the y-axis. If the error is already near zero, the gap may look huge in absolute terms but be negligible. Always consider the metric's range.
- Training on the full dataset and then evaluating on the same full dataset. The training curve must be evaluated on the subset actually used for training, not the whole training set.
Tips
- Start with a small model. Run the learning curve on a simple baseline first. It will usually show high bias, which tells you the problem isn't data quantity but model capacity.
- Use a logarithmic x-axis if your training sizes span orders of magnitude (e.g., 100, 1,000, 10,000). It makes the curve easier to read.
- Plot the standard deviation as a shaded region around each curve to see how stable the estimates are.
- For classification, use log loss or error rate. Accuracy is less sensitive to small improvements; log loss reflects confidence, not just correctness.
- Combine learning curves with validation curves. A validation curve (error vs. model complexity) complements the learning curve and can confirm whether increasing complexity helps.
Troubleshooting
- The curves are jagged and noisy. Increase the number of repeated runs per size (e.g., from 3 to 10) and use the average. Also ensure you're sampling without replacement and not accidentally leaking validation data into training.
- Validation error goes up as training size increases. This usually indicates a bug: you may be training on the validation set accidentally, or the validation set is too small and noisy. Check your split logic.
- Both curves are flat and high. The model is too simple or the features are not informative. Try a more complex model or add feature engineering before blaming data size.
- The training error is zero and validation error is high. This is classic overfitting. Add regularization, reduce model capacity, or get more data. Also check if the model is leaking target information through features.
- The curves cross each other. This can happen with very small training sets. It's not necessarily a problem; focus on the trend as the training size increases, not on the crossing point.
- You cannot see any pattern because the error scale is too compressed. Try plotting the log of the error, or use a different metric that is more sensitive (e.g., mean squared error instead of mean absolute error for regression).
Frequently Asked Questions
Q: How many training sizes should I use?
A: 5–10 distinct sizes is usually enough. Fewer than 5 makes the trend hard to see; more than 10 adds little value unless the dataset is huge.
Q: Can I use the test set instead of a validation set for the curve?
A: Technically yes, but you should not. The test set must be reserved for final evaluation only. Using it to decide what to do next leaks information and makes your final test score optimistic.
Q: What if my training curve is high but validation curve is even higher?
A: That is normal. Training error is usually lower than validation error. The key is the gap and the slope. If both are high and close, it's bias. If training is low and validation is high, it's variance.
Q: Does a learning curve work for deep learning?
A: Yes, but training is expensive. You can train on a few subset sizes (e.g., 25%, 50%, 75%, 100%) and use fewer epochs for the smaller subsets to save time, though this slightly changes the comparison.
Q: My curves show high variance. Should I always get more data?
A: More data helps, but it is often the most expensive fix. Try regularization or a simpler model first; they are cheaper and may close the gap without needing new data.
Q: What is the difference between a learning curve and a validation curve?
A: A learning curve varies the training set size. A validation curve varies a hyperparameter (like model complexity or regularization strength) while keeping the training set fixed. They answer different questions.