Not All Semantics are Created Equal: Contrastive Self-supervised Learning with Automatic Temperature Individualization

Zi-Hao Qiu, Quanqi Hu, Zhuoning Yuan, Denny Zhou, Lijun Zhang, Tianbao Yang

Abstract

In this paper, we aim to optimize a contrastive loss with individualized temperatures in a principled and systematic manner for self-supervised learning. The common practice of using a global temperature parameter $\tau$ ignores the fact that "not all semantics are created equal", meaning that different anchor data may have different numbers of samples with similar semantics, especially when data exhibits long-tails. First, we propose a new robust contrastive loss inspired by distributionally robust optimization (DRO), providing us an intuition about the effect of $\tau$ and a mechanism for automatic temperature individualization. Then, we propose an efficient stochastic algorithm for optimizing the robust contrastive loss with a provable convergence guarantee without using large mini-batch sizes. Theoretical and experimental results show that our algorithm automatically learns a suitable $\tau$ for each sample. Specifically, samples with frequent semantics use large temperatures to keep local semantic structures, while samples with rare semantics use small temperatures to induce more separable features. Our method not only outperforms prior strong baselines (e.g., SimCLR, CLIP) on unimodal and bimodal datasets with larger improvements on imbalanced data but also is less sensitive to hyper-parameters. To our best knowledge, this is the first methodical approach to optimizing a contrastive loss with individualized temperatures.

Introduction

Contrastive Learning (CL) has emerged as a cornerstone of self-supervised representation learning. Frameworks like SimCLR and CLIP operate by pulling "positive" pairs (different views of the same instance) closer in the embedding space while pushing "negative" pairs (different instances) apart. Central to this process is the temperature parameter ($\tau$), a hyperparameter that scales the similarity scores in the softmax-based contrastive loss (e.g., InfoNCE).

Despite its importance, the standard practice is to use a single, global temperature for all data points. This approach assumes a semantic uniformity that rarely exists in real-world datasets. This paper argues that because semantic density varies—some concepts are common while others are rare—a global temperature is inherently suboptimal. We introduce a principled framework for individualizing temperatures, ensuring that the model treats every semantic concept according to its unique distribution.

The Limitation of Global Temperature

In standard contrastive losses, the temperature $\tau$ dictates the "hardness" of the negative samples.

  • Small $\tau$: Forces the model to focus on the most difficult negative samples. While this creates highly separable features, it risks "semantic shattering"—pushing away samples that are semantically similar but technically distinct (e.g., two different images of the same breed of dog).
  • Large $\tau$: Results in a more uniform distribution over negatives. This preserves local semantic structures but may fail to create distinct boundaries for rare or isolated concepts.

In long-tailed datasets, a global temperature forces a compromise. Frequent semantics (the "head" of the distribution) require higher temperatures to maintain cluster cohesion, while rare semantics (the "tail") require lower temperatures to ensure they are sufficiently discriminative.

Methodology: iSogCLR

1. Robust Global Contrastive Loss (RGCL)

To address the limitations of global temperature, we derive a new objective inspired by Distributionally Robust Optimization (DRO). Instead of a standard average loss, we consider a distribution over negative samples for each anchor. By applying a KL-divergence constraint, we prevent the model from over-focusing on any single negative sample, providing a robust foundation for feature learning.

2. Individualized Temperatures via Duality

Using Lagrangian duality, we demonstrate that solving the DRO-based contrastive problem is mathematically equivalent to a contrastive loss where each anchor $i$ possesses its own learnable temperature $\tau_i$. This theoretical bridge transforms a complex optimization problem into a manageable task of learning sample-specific parameters.

3. The iSogCLR Algorithm

Optimizing a unique temperature for every sample in a massive dataset (e.g., millions of images) presents a significant memory and computational challenge. We propose iSogCLR, an efficient stochastic algorithm that utilizes moving average estimators to track loss components. This allows for the optimization of individualized temperatures on the fly without requiring massive mini-batch sizes or excessive GPU memory. For a dataset of one million samples, the additional memory overhead is approximately 7.6MB, which is negligible in the context of modern deep learning.

Experimental Results

Unimodal Performance

We evaluated iSogCLR on both balanced (CIFAR-100, ImageNet-100) and long-tailed datasets (CIFAR-100-LT, iNaturalist 2018). The benefits were most pronounced in imbalanced scenarios. On CIFAR-100-LT, iSogCLR achieved an accuracy of 53.06%, significantly outperforming the SimCLR baseline of 49.33%.

Bimodal Performance (Vision-Language)

When integrated into a CLIP-style architecture and pre-trained on the Conceptual Captions 3M (CC3M) dataset, iSogCLR showed substantial gains in zero-shot retrieval. On the MSCOCO text-retrieval task (TR@1), iSogCLR reached 32.72%, a 5.7% improvement over the standard CLIP baseline (26.98%).

Hyperparameter Robustness

Standard contrastive learning is notoriously sensitive to the initial choice of $\tau$. iSogCLR exhibits much higher stability. Regardless of the initial temperature setting, the algorithm converges to a similar, high-performing distribution of learned temperatures, reducing the need for extensive hyperparameter sweeping.

Conclusion

iSogCLR represents a shift from heuristic-based temperature tuning to a principled, automated approach. By recognizing that "not all semantics are created equal," our method allows models to adapt to the inherent density of the data distribution. This leads to more robust representations, particularly in the long-tailed distributions that characterize real-world data. Future work may extend this individualized optimization logic to other modalities, such as audio or reinforcement learning states.

Summary updated

BibTeX
@inproceedings{qiu2023not,
  author = {Zi-Hao Qiu and Quanqi Hu and Zhuoning Yuan and Denny Zhou and Lijun Zhang and Tianbao Yang},
  title = {{Not All Semantics are Created Equal: Contrastive Self-supervised Learning with Automatic Temperature Individualization}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=l0I4S6x1Nxt},
}
Figure