Gradient Matching for Domain Generalization

Yuge Shi, Jeffrey Seely, Philip Torr, Siddharth N, Awni Hannun, Nicolas Usunier, Gabriel Synnaeve

TL;DR

The authors propose Fish, a simple yet effective first-order algorithm that encourages models to ignore spurious correlations (like backgrounds) by matching gradient directions across different training domains. By maximizing the inner product of gradients from multiple sources, Fish effectively identifies invariant features that generalize better to unseen environments, achieving state-of-the-art results on major benchmarks like WILDS.

What Problem Does This Paper Address?

Machine learning models are notorious for taking the "path of least resistance." If you train a classifier to distinguish between cows and camels, and all the cows are on green grass while the camels are on yellow sand, the model will likely learn to classify the background rather than the animal. This is known as a spurious correlation.

When this model is deployed in a new "domain"—say, a cow on a beach—it fails. The challenge of Domain Generalization (DG) is to train models that ignore these domain-specific shortcuts and instead focus on invariant features (like the shape of a hump or a snout) that remain consistent across all environments.

Methods

The authors introduce a new objective called Inter-Domain Gradient Matching (IDGM). The core idea is intuitive: if a feature is truly invariant, the gradient (the direction to update the model to reduce error) should point in a similar direction regardless of which domain the data comes from. By maximizing the inner product of gradients from different domains, we force the model to find parameters where all domains "agree" on the direction of improvement.

Conceptual illustration of Inter-Domain Gradient Matching (IDGM) vs. ERM.

The "Fish" Algorithm

Directly optimizing the gradient inner product is computationally expensive because it requires second-order derivatives (Hessians). To solve this, the authors derive Fish, a first-order approximation inspired by the meta-learning algorithm Reptile.

The process is straightforward:

  1. Clone the current model.
  2. Inner Loop: Perform several steps of standard gradient descent on the clone, using a different domain for each step.
  3. Outer Loop: Update the original model by moving it slightly toward the final state of the clone.

While standard Empirical Risk Minimization (ERM) just averages the loss, Fish effectively "looks ahead" to see if an update for one domain will also help others.

Datasets

The authors evaluated Fish across a massive range of tasks:

  • Toy Datasets: A linear classification task and CDSprites-N (a newly introduced variant of dSprites), where color is a spurious correlation and shape is the invariant feature.
  • WILDS Benchmark: Real-world distribution shifts including satellite imagery (PovertyMap, FMoW), medical imaging (Camelyon17), and text toxicity detection (CivilComments).
  • DomainBed Benchmark: Seven classic DG datasets including PACS, OfficeHome, and DomainNet.

Key Insights and Ideas

  • Gradient Alignment = Invariance: The "aha moment" here is that gradient consistency is a proxy for causal invariance. If updating a feature helps in Domain A but hurts in Domain B, that feature is likely a spurious shortcut.
  • First-Order Efficiency: The authors prove that their first-order "Fish" update is mathematically equivalent to maximizing the gradient inner product as the learning rate approaches zero. This allows for a 10x speedup over direct second-order optimization.
  • Fish vs. Reptile: While they look similar, the difference is crucial. Reptile matches gradients within a single task to speed up adaptation; Fish matches gradients across domains to prevent the model from adapting to domain-specific noise.
  • Handling Subpopulation Shift: Surprisingly, Fish performs exceptionally well on datasets like CivilComments, where the "domains" are demographic groups. This suggests gradient matching is a powerful tool for algorithmic fairness and robustness against minority group neglect.

Key Results

  • WILDS: Fish outperformed all baselines (including IRM and GroupDRO) on 3 out of 6 datasets and achieved competitive results on the rest. Notably, on Camelyon17 (tumor detection), Fish achieved 74.7% accuracy compared to ERM’s 70.3%.
  • DomainBed: Fish ranked 2nd out of 10 algorithms when averaged across all seven datasets, proving its versatility across vision and language tasks.
  • Robustness to Domain Count: In the CDSprites-N experiment, Fish reached near 100% test accuracy with as few as 10 training domains, whereas ERM required 40 domains to stop over-relying on spurious color cues.

Limitations and Future Work

The authors identify one primary bottleneck: Scalability. When the number of domains is extremely large (e.g., the Amazon dataset with over 7,000 "reviewer" domains), Fish struggles to outperform simple ERM. The inner-loop logic becomes computationally heavy as the domain count grows.

Future research directions include finding more efficient ways to sample domains for the inner loop and investigating how to scale gradient matching to datasets with tens of thousands of distinct environments. Additionally, the authors noted that while Fish is faster than second-order methods, it still requires more compute than standard ERM due to the multiple inner-loop updates.

Summary updated

BibTeX
@inproceedings{shi2022gradient,
  author = {Yuge Shi and Jeffrey Seely and Philip Torr and Siddharth N and Awni Hannun and Nicolas Usunier and Gabriel Synnaeve},
  title = {{Gradient Matching for Domain Generalization}},
  booktitle = {International Conference on Learning Representations},
  year = {2022},
  url = {https://openreview.net/forum?id=vDwBW49HmO},
}
Figure