Temperature as Uncertainty in Contrastive Learning

Oliver Zhang, Mike Wu, Jasmine Bayrooti, Noah Goodman

TL;DR

This paper introduces Temperature as Uncertainty (TaU), a simple yet effective method to equip contrastive learning models with a measure of confidence. By transforming the standard temperature hyperparameter into a learned, input-dependent variable, the authors enable models to flag out-of-distribution (OOD) data while maintaining high-quality representations.

What Problem Does This Paper Address?

Contrastive learning has revolutionized unsupervised representation learning, often matching or exceeding supervised performance. However, these models typically produce "point embeddings"—they map an input to a single location in a vector space without any indication of how certain they are about that mapping.

In real-world deployments, knowing when a model is confused is critical. For example, an autonomous system needs to recognize when it encounters an "out-of-distribution" (OOD) image that doesn't look like its training data. Existing uncertainty methods, like Bayesian neural networks or ensembles, are often computationally expensive or fail to scale to the high-dimensional spaces used in modern contrastive frameworks.

Methods

The authors' key insight lies in the temperature parameter ($\tau$) found in the standard contrastive loss function (InfoNCE). Traditionally, $\tau$ is a fixed hyperparameter that scales the similarity between embeddings.

The authors observe that:

  • Low temperature makes the loss highly sensitive to small changes in embedding location (high confidence).
  • High temperature makes the loss more invariant or "forgiving" of displacements (low confidence).

Instead of using a fixed value, TaU makes temperature a function of the input. They modify the encoder network to output an additional scalar value representing the uncertainty for each specific image.

To ensure training stability, they employ a few clever tricks:

  1. Reciprocal Mapping: The network predicts $\alpha(x) = 1/\tau(x)$ rather than $\tau$ directly.
  2. Sigmoid Bounding: They use a sigmoid function to keep the learned values within a stable range.
  3. Post-hoc Training: They demonstrate that a small "uncertainty head" can be trained on top of frozen, pre-trained models (like CLIP or BYOL), allowing practitioners to add uncertainty to existing models without a full retrain.

Datasets

The researchers evaluated TaU using several standard benchmarks:

  • In-distribution (Training): CIFAR10 and ImageNet.
  • Out-of-Distribution (Evaluation): CIFAR100, SVHN (Street View House Numbers), TinyImageNet, LSUN, COCO, and CelebA.

Key Insights and Ideas

  • Temperature as a Sensitivity Knob: The most elegant part of this work is realizing that a "mysterious" hyperparameter already used for scaling can be re-purposed as a direct proxy for model confidence.
  • Visualizing Confidence: The model's uncertainty aligns well with human intuition. As seen in Figure 1, images with high TaU certainty are clear and prototypical, while low-certainty images are often cluttered, blurry, or contain unusual perspectives.
Figure 1: CIFAR10 Images on the left have high TaU cer- tainty while images on the right have low TaU certainty.
  • Superiority over Baselines: Traditional uncertainty methods like Monte Carlo (MC) Dropout or Ensembles performed surprisingly poorly in the contrastive setting, often barely doing better than random chance at OOD detection. TaU significantly outperformed them.

Key Results

  • OOD Detection: TaU excels at identifying very different distributions. For a model trained on CIFAR10, TaU achieved an AUROC of 0.968 when detecting SVHN images, outperforming the k-Nearest Neighbors (kNN) baseline (0.800) and MC Dropout (0.684).
  • Minimal Performance Hit: Adding uncertainty doesn't ruin the representations. Linear evaluation accuracy on CIFAR10 only dropped by about 2-3% compared to standard deterministic models.
Table 1: Downstream Out-of-Distribution Detection: comparison of TaU to several popular base- lines for uncertainty on deep neural networks. Out-of-distribution AUROC is reported.
  • Versatility: When applied post-hoc to a supervised ImageNet model, TaU achieved a massive 0.978 AUROC on SVHN detection, proving it is a viable "bolt-on" feature for existing infrastructure.

Limitations and Future Work

  • NCE Dependency: The current formulation of TaU is tied to contrastive objectives that use negative samples (like SimCLR or MoCo). The authors noted limited success when applying it to "negative-free" architectures like SimSiam or Barlow Twins during the training phase.
  • Optimization Sensitivity: Learning temperature can be unstable. Without the specific architectural constraints (like the sigmoid and reciprocal mapping) described in the paper, the values can collapse to zero or explode to infinity.
  • Future Directions: The authors suggest exploring TaU for detecting adversarial attacks or data corruption, and extending the logic to a wider variety of self-supervised objectives.

Summary updated

BibTeX
@misc{zhang2021temperature,
  author = {Oliver Zhang and Mike Wu and Jasmine Bayrooti and Noah Goodman},
  title = {{Temperature as Uncertainty in Contrastive Learning}},
  year = {2021},
  eprint = {2110.04403},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2110.04403},
}
Figure