Course contentsShow
← AI Engineering
Lesson 1581 of 1,88638. Bias, Fairness, and AlignmentFree lesson

Counterfactual Data Augmentation

Generate counterfactual examples by swapping sensitive attributes to train models that are invariant to protected characteristics.

Counterfactual Data Augmentation

You'll learn how to synthetically create training examples that flip sensitive attributes to teach models fairness through balanced exposure.

The Core Idea

Counterfactual data augmentation fights bias by creating "what-if" versions of your training data. You take real examples and systematically swap protected characteristics—like gender pronouns, names associated with specific ethnicities, or age indicators—while keeping everything else constant.

Why this works: If your model sees "Sarah is a software engineer" and "Michael is a software engineer" with equal frequency and identical contexts, it learns that engineering competence has nothing to do with gender. The model becomes invariant to these protected attributes because the training signal is balanced.

How It Works in Practice

  1. Identify sensitive attributes in your data (names, pronouns, demographic descriptors)
  2. Create mappings between equivalent terms (he/she, common names across ethnic groups)
  3. Generate counterfactual pairs by swapping these attributes while preserving semantic meaning
  4. Train on the augmented dataset where protected characteristics appear uniformly across all contexts

Think of it like teaching fairness through repetition: if a child only sees doctors depicted as men, they develop bias. Show them equal examples of male and female doctors, and they learn the profession is independent of gender.

Connection to Earlier Mitigation Strategies

This is a pre-processing technique (lesson 1575) that balances training data before model training begins. Unlike prompt-based mitigation (1578) or output calibration (1577), counterfactual augmentation bakes fairness directly into the model's learned representations.

Key Takeaway: Counterfactual data augmentation creates synthetic training examples by swapping protected attributes, forcing models to learn patterns that are invariant to characteristics like gender, race, or age—effectively debiasing through balanced exposure during training.