Supervised Contrastive Learning of Environment-Invariant Representations
Authors: Taro Makino, Ji Won Park, Natasa Tagasovska, Takamasa Kudo, Paula Coelho, Jan-Christian Huetter, Heming Yao, Burkhard Hoeckendorf, Ana Carolina Leote, Stephen Ra, David Richmond, Kyunghyun Cho, Aviv Regev, Romain Lopez
Organizations: Center for Data Science, New York University · Prescient Design, Genentech · Research and Early Development (gRED), Genentech · Biology Research — AI Development (BRAID), Genentech · Department of Genetics, Stanford University
Real-world datasets often combine data collected under different experimental conditions. This yields larger datasets, but can also introduce environment-associated correlations that make it difficult to model the phenomena of interest. We address this with Supervised Contrastive Invariance Learning (SCIL), which learns a task-supervised representation zc while regularizing its environment-related structure, together with an environment-supervised auxiliary representation zs. The contribution of the auxiliary representation to the reported results remains unresolved. SCIL is based purely on Supervised Contrastive Learning and uses a single hyperparameter α to control the strength of environment regularization. We study this regularization under distribution shift with Colored MNIST and Camelyon17-WILDS, where increasing α decreases in-distribution validation accuracy and increases held-out test accuracy. We also evaluate SCIL on batch correction for 26 million Optical Pooled Screening images, where zc preserves biological signal while becoming less predictive of inter-well variation.
Figures & tables
Figure 1: Spurious correlations emerge when collecting medical images from different hospitals, or conducting single-cell perturbation screens across multiple wells. zs models these spurious correlations, while zc models the environment-invariant correlations.
Figure 2: Colored MNIST. (a) There is an environment-dependent correlation between color and digit on the training set, which does not persist on the test set where all digits are white. (b) We can generate images counterfactually using SCBD. When we swap zc across examples, it changes the digit without affecting the color. In contrast, when we swap zs across examples, it changes the color without affecting the digit. By composing digit and color independently, we generate images outside of the support of the training distribution, such as a light red one (bottom middle) and a bright green five (bottom right).
Figure 3: Increasing α strengthens the degree that zc is invariant to e , and monotonically improves test accuracy at the expense of validation accuracy.
Algorithm
CMNIST
Camelyon17-WILDS
SCBD ( α=0 )
25.5±3.0
61.9±3.8
SCBD ( α=192 )
82.9±12.1
72.7±3.0
ERM
37.8±2.6
65.8±4.9
CORAL
37.6±3.6
59.5±7.7
DANN
39.0±4.5
55.2±6.7
IRM
37.0±4.2
66.3±2.1
Table 1: Test accuracy (%) for domain generalization for ten random seeds. SCBD with α=192 significantly outperforms all baselines on both CMNIST and Camelyon17-WILDS.
Figure 4: Comparison of SCBD to CellProfiler and VAE-based baselines on real-world batch correction. Left: Performance on predicting protein complex membership (biological content). Higher is better. Right: Performance of predicting the well label e . Lower is better. SCBD is unambiguously better than all baselines, as it preserves more biological signal, while being less sensitive to the inter-well batch effects.
Figure 5: In- and out-of-distribution performance are negatively correlated on CMNIST, which satisfies the assumptions made by SCBD.
Figure 6: Counterfactual generation with iVAE. When we swap zc , it changes the digit but not the color, and when we swap zs , it changes the color but not the digit. iVAE generates better-looking images than SCBD, since the decoder is trained jointly with the encoder for iVAE.
Figure 7: CMNIST results for an ablation in which we omit zs , and learn a single block of latent variables zc that are correlated with y and invariant to e . These results are similar to the model that learns zs . We use ResNet-18 encoders here, as we did in the main text.
Figure 8: CMNIST embedding size ( Dzc and Dzs ) ablation study for SCBD with ResNet-18 encoders and α=192 . The results are relatively consistent across different embedding sizes.
Figure 9: CMNIST batch size ablation study for SCBD with ResNet-18 encoders and α=192 . The results are generally better for larger batch sizes, which was also observed by the authors of SCL ( Khosla et al., 2020 ) .
Figure 10: CMNIST weight decay ablation study for SCBD with ResNet-18 encoders and α=192 . The results are relatively consistent across different degrees of weight decay.
Figure 11: In- and out-of-distribution performance are negatively correlated on Camelyon17-WILDS. This is consistent with Teney et al. (2024) , and therefore this dataset satisfies the assumptions made by SCBD.
Figure 12: Camelyon17-WILDS results for SCBD using ResNet-18 encoders. The conclusions are the same as with the DenseNet-121 encoders.
Figure 13: Camelyon17-WILDS results for an ablation in which we omit zs , and learn a single block of latent variables zc that are correlated with y and invariant to e . We observe a clean trade-off between validation and test accuracy with respect to α , but the test accuracy error bars are larger than those of the model that includes zs .
Figure 14: Camelyon17-WILDS embedding size ( Dzc and Dzs ) ablation study for SCBD with DenseNet-121 encoders and α=192 . The results are relatively consistent across different embedding sizes.
Figure 15: Camelyon17-WILDS batch size ablation study for SCBD with DenseNet-121 encoders and α=192 . The results are generally better for larger batch sizes, which was also observed by the authors of SCL ( Khosla et al., 2020 ) .
Figure 16: Camelyon17-WILDS weight decay ablation study for SCBD with DenseNet-121 encoders and α=192 . The results are relatively consistent across different degrees of weight decay.
Figure 17: PACS with art painting as the test domain.
Figure 18: PACS with cartoon as the test domain.
Figure 19: PACS with photo as the test domain.
Figure 20: PACS with sketch as the test domain.
Algorithm
Art painting
Cartoon
Photo
Sketch
Average
SCBD ( α=0 )
86.6±2.1
81.3±1.9
97.0±0.8
70.6±4.9
83.9
ERM
88.1±0.1
77.9±1.3
97.8±0.0
79.1±0.9
85.7
CORAL
87.7±0.6
79.2±1.1
97.6±0.0
79.4±0.7
86.0
DANN
85.9±0.5
79.9±1.4
97.6±0.2
75.2±2.8
84.6
IRM
85.0±1.6
77.6±0.9
96.7±0.3
78.5±2.6
84.4
Group DRO
86.4±0.3
79.9±0.8
98.0±0.3
72.1±0.7
84.1
Appendix
Table 6: Test accuracy (%) for PACS with three random seeds.
Figure 21: VLCS with Caltech101 as the test domain.
Figure 22: VLCS with LabelMe as the test domain.
Figure 23: VLCS with SUN09 as the test domain.
Figure 24: VLCS with VOC2007 as the test domain.
Algorithm
Caltech101
LabelMe
SUN09
VOC2007
Average
SCBD ( α=0 )
94.7±1.8
67.1±1.3
74.7±3.0
71.4±1.3
77.0
ERM
97.6±1.0
63.3±0.9
72.2±0.5
76.4±1.5
77.4
CORAL
98.8±0.1
64.6±0.8
71.7±1.4
75.8±0.4
77.7
DANN
98.5±0.2
64.9±1.1
73.1±0.7
78.3±0.3
78.7
IRM
97.6±0.3
65.0±0.9
72.2±0.5
76.4±1.5
78.1
Group DRO
97.7±0.4
62.5±1.1
70.1±0.7
78.4±0.9
77.2
Appendix
Table 7: Test accuracy (%) for VLCS with three random seeds.
We consider learning from labeled data collected across multiple environments, where the data distribution may vary across these environments. This problem is commonly approached from a causal perspective, seeking invariant representations that retain causal factors while discarding spurious ones. However, this framework assumes that the environment has no direct effect on the target. In contrast, we consider settings in which this assumption fails, but still aim to learn representations that support robust prediction on average across previously unseen environments. To this end, we study representations learned by explicitly modeling variation across environments and then marginalizing that variation out. We analyze the resulting representations and characterize when they are preferable to those learned by causal invariant-representation methods. We propose a concrete method based on generalized random-intercept models, a class of predictors in which such marginalization is possible, and study their generalization properties. Empirically, we show that these models outperform invariant-learning methods across a range of challenging settings.
Yuli Slavutsky, David M. Blei
Department of Statistics Columbia University · Departments of Statistics and Computer Science Columbia University
Deep neural networks trained with Empirical Risk Minimization (ERM) often fail under distribution shifts because they exploit spurious correlations between object labels and background context. Recent generative approaches address this issue by creating counterfactual images with altered contexts, but typically use these samples as standard data augmentation, leaving the model free to retain background-sensitive representations. We propose a two-stage framework that uses generative intervention to explicitly learn background-invariant visual representations. First, we isolate the foreground object using zero-shot segmentation and generate context-shifted variants with a structure-preserving diffusion model, preserving object identity while varying the surrounding environment. We then introduce Cross-Variant Self-Supervised Learning, where variants of the same object under different backgrounds form positive pairs in a contrastive objective. This encourages the encoder to align object-centric representations while suppressing background-specific cues. Then, we fine-tune the pretrained encoder using an ERM warm-up followed by GroupDRO with layer-wise learning rates. Experiments on distribution-shift benchmarks demonstrate best worst-group performance, achieving 92.5% on Waterbirds, 81.7% on MetaShift, and 87.4% on NICO++. Code: https://github.com/surajyadav-research/GRSSL
Suraj Yadav, Anjaneya Sharma, Siddharth Yadav
Indraprastha Institute of Information Technology Delhi
Self-supervised learning (SSL) has revolutionized representation learning, with Joint-Embedding Architectures (JEAs) emerging as an effective approach for capturing semantic features. Existing JEAs rely on implicit or explicit batch interaction -- via negative sampling or statistical regularization -- to prevent representation collapse. This reliance becomes problematic in regimes where batch sizes must be small, such as high-dimensional scientific data, where memory constraints and class imbalance make large, well-balanced batches infeasible. We introduce IConE (Instance-Contrasted Embeddings), a framework that decouples collapse prevention from the training batch size. Rather than enforcing diversity through batch statistics, IConE maintains a global set of learnable auxiliary instance embeddings regularized by an explicit diversity objective. This transfers the anti-collapse mechanism from the transient batch to a dataset-level embedding space, allowing stable training even when batch statistics are unreliable, down to batch size 1. Across diverse 2D and 3D biomedical modalities, IConE outperforms strong contrastive and non-contrastive baselines throughout the small-batch regime (from B=1 to B=64) and demonstrates marked robustness to severe class imbalance. Geometric analysis shows that IConE preserves high intrinsic dimensionality in the learned representations, preventing the collapse observed in existing JEAs as batch sizes shrink.
Konstantinos Almpanakis, Anna Kreshuk
European Molecular Biology Laboratory, Heidelberg, Germany