Sub topic 12
Behavioral Tests with Input Perturbations
Prerequisites and working model
This chapter assumes that you can run a trained NLP classifier, understand train, validation, and test splits, and use basic Python. It also assumes that you know accuracy, precision, recall, and F1.
A behavioral test checks how a model responds when an input changes in a controlled way. The test does not ask only whether the original prediction is correct. It asks whether the prediction changes, or stays the same, as the meaning of the input changes.
For example, changing a person's name should usually not change the sentiment of this sentence:
- “Alice said the service was terrible.”
- “Mohammed said the service was terrible.”
Changing “terrible” to “excellent” should change the sentiment. These tests are often called metamorphic tests because they check an expected relationship between related inputs.
Designing useful perturbations
Start by writing the expected behavior before running the model. Common test families include:
- Invariance: An irrelevant change should preserve the prediction. Examples include replacing a name, changing extra whitespace, or changing capitalization when case should not matter.
- Directional sensitivity: A meaningful change should cause a known change. Replacing “helpful” with “unhelpful” should often alter sentiment.
- Consistency: Equivalent forms should produce equivalent outputs. A question and a polite version of the same question may need the same intent label.
- Robustness: Small input noise, such as a spelling variation or punctuation change, should not cause an unexpected failure.
Use templates and several values for each perturbation. A single example can hide a coincidence. Record the source text, modified text, predictions, expected relation, and pass or fail result.
def predict(text):
"""A small stand-in for a trained sentiment model."""
words = set(text.lower().replace(".", "").split())
if "terrible" in words or "awful" in words:
return "neg"
if "excellent" in words or "helpful" in words:
return "pos"
return "neu"
names = ["Alice", "Mohammed", "Wei", "Olga"]
template = "{} said the service was terrible."
results = []
for name in names:
source = template.format("Alice")
modified = template.format(name)
source_pred = predict(source)
modified_pred = predict(modified)
results.append((name, source_pred == modified_pred))
failures = [name for name, passed in results if not passed]
print(f"tested={len(results)}, failures={len(failures)}")Reading failures and avoiding false conclusions
A failed behavioral test is evidence for investigation, not automatic proof of bias. Check whether the perturbation really is irrelevant for the task. A name may matter in a task about identity, authorship, or demographic references.
Common errors include using unrealistic sentences, treating an uncertain prediction as a hard label, and testing only one template. Include natural examples and multiple templates. Keep behavioral cases separate from training data when possible. Otherwise, memorization can make the test look stronger than generalization.
Track failure rates by test family and model version. Keep every discovered failure as a regression test after fixing the model. A useful next iteration targets failures that are frequent, harmful to users, and practical to reduce.
Exercise
A sentiment model predicts “pos” for “The delivery was late” and “neg” after changing only the word “delivery” to “service.” What should you check first?
Show answer
Check whether the two terms carry different sentiment in the task and whether the examples are realistic. If they are equivalent for the intended behavior, add more templates and inspect the model's features before calling it a systematic failure.