Diagnose high bias or high variance from training and validation loss curves
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `diagnose_fit(train_losses, val_losses, baseline_loss, bias_tol=0.1, gap_tol=0.1, window=3)`. Summarize each trajectory by the mean of its last `min(window, len)` epochs, classify the fit, and return a dict with keys `label`, `final_train`, `final_val`, `bias_gap`, `gap`, `remedies` (all numbers as `float`).
The classification rules:
```python
bias_gap = final_train - baseline_loss
gap = final_val - final_train
if bias_gap > bias_tol: label = "high_bias" # checked first
elif gap > gap_tol: label = "high_variance"
else: label = "well_generalized"
remedies = {
"high_bias": ["increase model capacity", "add features",
"reduce regularization", "train longer"],
"high_variance": ["get more training data", "increase regularization",
"reduce model capacity", "use early stopping"],
"well_generalized": ["keep current configuration"],
}[label] # return a list in this exact order
```
Example:
```python
diagnose_fit([1.0, 0.75, 0.5, 0.25, 0.0],
[1.0, 0.875, 0.75, 0.625, 0.5], 0.125)
# final_train=0.25, final_val=0.625, bias_gap=0.125, gap=0.375
# label="high_bias" (0.125 > 0.1)
```
```hint Watch the edges
Two inputs trip up most first drafts: a trajectory shorter than `window`, and values such as `window=2.5` or a NaN loss that must raise `ValueError` before any comparison runs.
```
### Constraints & Assumptions
- Inputs are lists or 1-D arrays, and `baseline_loss` is an estimate of the achievable loss on the task.
- Comparisons are strict: a value exactly at tolerance is not flagged.
- Raise `ValueError` for empty or unequal-length trajectories, any non-finite loss or `baseline_loss`, or `window` that is not a positive integer.
### What a Strong Answer Covers
- Input validation that raises `ValueError` for every listed bad input, including each kind of invalid `window`.
- Windowed averages that stay correct when a trajectory is shorter than `window`.
- The bias check running before the gap check, with strict comparisons at the tolerances.
- Numbers returned as floats and a fresh copy of the remedy list in the given order.
- Why averaging the last epochs makes the diagnosis less sensitive to noise.
### Follow-up Questions
- Why check high bias before the train/val gap?
- How would you detect overfitting onset from the trajectories alone?
- What would you conclude when the final training loss ends up below `baseline_loss`?
Overview: Write a function that reads training and validation loss curves, averages the last few epochs, and labels a model as high bias, high variance or well generalized against a baseline loss. It tests the order of the bias and variance checks, strict tolerance comparisons, input validation and matching remedies to each failure mode.