Improving Domain Generalization in Contrastive Learning using Adaptive Temperature Control

Robert Lewis, Katie Matton, Rosalind W. Picard, John GuttagarXiv 2026

TL;DR

Self-supervised contrastive learning often fails when test data undergoes a distribution shift that standard augmentations cannot mimic. This paper introduces an adaptive temperature control mechanism for the InfoNCE loss that uses domain labels to dynamically upweight negative samples from the same domain. This approach successfully guides the model to learn domain-invariant representations, boosting both out-of-distribution (OOD) generalization and in-distribution (ID) performance.

What Problem Does This Paper Address?

Contrastive learning frameworks (like SimCLR) excel at learning representations from sparsely labeled data by ensuring that different augmentations of the same image stay close in embedding space, while different images are pushed apart. However, this relies on the assumption that your choice of augmentations accurately reflects the variations the model will see at test time.

In real-world scenarios—such as deploying a medical imaging model on data from a new hospital—the test data often undergoes a "covariate shift" (a change in the input distribution while the underlying label conditional distribution stays the same). If the pre-training augmentations don't simulate this specific shift, performance plummets. The authors tackle the challenge of achieving robust domain generalization when the training data comes from multiple distinct domains but labels are highly scarce.

Methods

The core innovation lies in modifying the standard InfoNCE loss function. Typically, this loss uses a fixed temperature parameter ($\tau$) to control how severely "hard negatives" (negative samples highly similar to the anchor) are penalized.

Instead of a fixed value, the authors propose an adaptive, domain-aware temperature ($\tau_{ij}$) for each negative pair. The process works as follows:

  1. A lightweight domain discriminator is trained on top of the encoder's embedding space to predict which domain a sample belongs to. Crucially, its gradients are not backpropagated to the encoder to avoid adversarial competition.
  2. This discriminator estimates the probability ($w_{ij}$) that a negative sample shares the same domain as the anchor instance.
  3. If a negative sample is likely from the same domain, the method assigns it a smaller temperature ($\tau_{ij}$). Because temperature sits in the denominator of an exponent, a smaller temperature exponentially increases the penalty for that sample.

This forces the encoder to aggressively push apart negative samples from the same domain, preventing the network from using domain-specific shortcuts (like background style or lighting) to differentiate images. The authors introduce two variants: Domain-Weighted Negatives (using hard domain labels for the anchor) and Domain-Weighted Pairs (using soft probabilistic labels for both).

Datasets

The method was evaluated on a custom-designed variant of the Colored-MNIST dataset.

  • Task: Binary classification of digits 3 vs. 5.

  • Domain Heterogeneity: Simulated by shifting the digit color distributions. Training domains featured predominantly red and blue digits, the validation domain used purple, and the completely unseen out-of-distribution (OOD) test domain contained green digits.

  • Data Splits: 23,100 total samples (60% Train, 10% Validation, 10% Test-ID, 20% Test-OOD), evaluated primarily in a label-scarce environment using only 1% of available digit labels for downstream linear evaluation.

Key Insights and Ideas

  • The Power of the Exponent: Adjusting the temperature inside the exponential term of the InfoNCE loss is mathematically far more potent than simply multiplying the loss by a weight outside the exponent. The exponential scaling ensures that domain-matching negatives dominate the gradient updates.
  • Self-Normalizing Training Dynamics: As pre-training successfully removes domain-specific information from the embeddings, the domain discriminator naturally becomes highly uncertain. As a result, the adaptive temperatures automatically converge back to the baseline uniform temperature ($\tau_\alpha$). This elegant transition is visualized in the paper's supplemental training plots:
Distribution of effective temperatures converging over epochs
  • Breaking the Robustness Trade-off: Traditional domain generalization techniques (like Maximum Mean Discrepancy (MMD) or Domain-Adversarial Neural Networks (DANN)) often suffer from a trade-off where forcing domain invariance degrades in-distribution accuracy. By contrast, this adaptive temperature approach improves both ID and OOD performance simultaneously.

Key Results

When evaluated with a downstream linear classifier trained on just 1% of digit labels, the proposed methods significantly outperformed the baselines:

  • Standard Contrastive Learning: Achieved 78.4% Test-OOD and 88.0% Test-ID accuracy.
  • Domain-Weighted Pairs (Ours): Achieved 81.9% Test-OOD and 94.5% Test-ID accuracy.
  • Traditional Baselines (MMD & DANN): Performed worse than standard contrastive learning on OOD data (68.8% and 70.0% respectively), demonstrating the pitfalls of rigid domain penalties.
Figure 1: Accuracy of contrastive learning methods as color variance (σ) within each domain is increased. Downstream linear classifiers are trained using 1% of digit labels. We see that the proposed domain-weighted methods are frequently among the best performing for Test-OOD Accuracy at σ > 25. Furthermore, they display higher robustness on Test-ID Accuracy as σ is increased.

As shown in Figure 1, the domain-weighted methods consistently maintain superior OOD robustness and ID accuracy across a wide range of within-domain color variances ($\sigma$).

Limitations and Future Work

  • Synthetic Evaluation: The primary limitation is that the method was only validated on a synthetic variant of MNIST where the domain feature (color) is simple and explicit. Testing on complex, real-world distribution shifts (e.g., natural images or clinical datasets) remains essential future work.
  • Model Selection Dilemma: Like many domain generalization methods, hyperparameter tuning ($\tau_\alpha$ and $\tau_\beta$) is tricky because peak validation performance does not perfectly correlate with peak OOD test performance.
  • Discriminator Complexity: The authors utilized a simple linear classifier for the domain discriminator; exploring non-linear architectures or better-calibrated probability outputs could further refine the temperature adjustments.

Summary updated

BibTeX
@misc{lewis2026improving,
  author = {Robert Lewis and Katie Matton and Rosalind W. Picard and John Guttag},
  title = {{Improving Domain Generalization in Contrastive Learning using Adaptive Temperature Control}},
  year = {2026},
  eprint = {2601.07748},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2601.07748},
}
Figure