Sub topic 15
Closing the Loop: Fix, Re-Evaluate, and Guard Against Regressions
Assume you know basic Python, pandas, NumPy, and the usual train, validation, and test split. You have already trained an NLP classifier or sequence tagger and measured accuracy, precision, recall, and F1.
Error analysis only helps when it changes what you do next. The practical loop is:
- Find and categorize important errors.
- Choose one targeted change.
- Re-train the model.
- Re-run the same evaluation.
- Check whether the target errors improved without creating worse failures.
- Add a regression test for the behavior you fixed.
Choose a fix that tests a hypothesis
A useful fix has a clear hypothesis. For example:
- Error category: the classifier misses short negations such as “not useful.”
- Hypothesis: the training data contains too few short examples with negation.
- Fix: add labeled examples that cover short negated phrases.
- Success measure: improve recall on a held-out negation slice without reducing overall precision beyond an agreed limit.
Change one major factor at a time. If you change the tokenizer, model, labels, and training data together, you will not know which change caused the result.
Keep a small experiment log. Record the dataset version, random seed, code version, configuration, baseline metrics, target slice, and decision rule.
| Experiment | Target | Baseline | New result | Decision |
|---|---|---|---|---|
| Add short negations | Negation recall | 0.62 | 0.78 | Keep if precision stays above 0.80 |
Re-evaluate the whole system
Always evaluate the complete test set again. A fix that improves one slice can damage another slice. Compare:
- Overall metrics.
- Per-class precision, recall, and F1.
- Important slices, such as short texts or rare labels.
- Confusion-matrix entries connected to the error category.
- Calibration if users rely on confidence scores.
- Latency and memory when the model runs in production.
Use the same test examples and metric definitions for the baseline and new model. If the test set changes, a score difference may reflect different data rather than a better model.
A small metric change may be noise. For important decisions, use a paired bootstrap or another repeated resampling method. Report the size of the change and its uncertainty, not only whether the new score is larger.
Add regression guards
A regression test turns a discovered behavior into a lasting requirement. For a classifier, store a small set of examples and expected labels. For a tagger, store expected spans or token labels. Run these tests whenever the model or preprocessing changes.
def predict(text):
# Replace this stand-in with the trained model.
return "neg" if "not useful" in text.lower() else "pos"
regression_cases = [
("not useful", "neg"),
("very useful", "pos"),
]
failures = [
(text, expected, predict(text))
for text, expected in regression_cases
if predict(text) != expected
]
if failures:
raise AssertionError(f"Regression failures: {failures}")
print("All regression cases passed")Regression cases should represent behaviors, not just famous individual examples. Include paraphrases, boundary cases, and nearby cases that should receive a different prediction. Review them when the product definition or annotation policy changes.
Prioritize the next iteration
Rank error categories using three questions:
- Frequency: how many production or test examples have this error?
- Impact: how harmful is the error?
- Fixability: can a realistic data, modeling, or product change address it?
Start with categories that are common, consequential, and actionable. After each iteration, preserve the old evaluation, inspect new errors, and decide whether the next problem is different.
Exercise
A fix raises recall for a rare class from 0.40 to 0.70, but overall accuracy falls from 0.92 to 0.89. What should you check before keeping the fix?
Show answer
Check the rare-class support, uncertainty in both measurements, precision and confusion-matrix changes, the cost of each error, and performance on important slices. Keep the fix only if the trade-off matches the task’s decision criteria.