Back to paper library
arXiv 2020ICML 2020Contrastive learningPaper notes

SimCLR v1

Winning with Batch Size

Original paper

A Simple Framework for Contrastive Learning of Visual Representations

Paper authors

Ting Chen · Simon Kornblith · Mohammad Norouzi · Geoffrey Hinton

Publication
ICML 2020Conference
First submitted to arXiv
arXiv:2002.05709
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{contrastivelearningsimclrv12020,
  title = {{A Simple Framework for Contrastive Learning of Visual Representations}},
  author = {Ting Chen and Simon Kornblith and Mohammad Norouzi and Geoffrey Hinton},
  year = {2020},
  eprint = {2002.05709},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2002.05709}
}
On this page

By 2020, this line of visual contrastive learning had been developing for about five years. The scene had become quite complicated and a little chaotic: not just model architectures, but training recipes too.

The authors of this paper believe that the core of contrastive learning should be simpler.

Defining the Problem

Recall the essential elements of contrastive learning:

  • To achieve good results, is a memory bank really necessary?

We need a large number of negative samples to guide the model in learning better representations, and a memory bank can indeed serve this purpose. The original InstDisc did this, and later MoCo followed suit.

But it's annoying! Maintaining a memory bank during training is clearly an unfriendly design.

In this paper, the author decides to directly discard the memory bank design, opting for larger batch sizes instead. As long as we provide enough negative samples, the model can learn sufficiently good representations!

Solving the Problem

Model Architecture

This architecture is really simple: no multiple encoders, no memory bank.

The process starts by applying two random augmentations to the same image, producing two "seemingly different, but actually from the same source" images.

Then, both images are passed through "the same" encoder network to obtain two latent vectors.

Next, a small projection network maps these latent vectors to the contrastive learning space, and finally, a contrastive loss function ensures that the "same source" augmented images are close to each other in the representation space.

Huh? That's it?

Yes! That's it, and we've just finished reading a paper! (Not really)

Image Augmentations

In SimCLR, the authors argue that strong, diverse augmentations matter even more for this contrastive task than they do for supervised classification.

The figure above shows the augmentations studied in the paper; the key ones in SimCLR's default combination include:

  • Random crop
  • Color distortion
  • Gaussian blur

The two augmented images are treated as a "positive pair" because they both come from the same original image.

Detailed Design

First, for the encoder, ResNet-50 is used as the base encoder. The augmented images are input into the network to obtain a vector hih_i. The dimensionality of this vector is typically large, such as 2048 dimensions after average pooling for ResNet-50.

Next, for the projection head, the authors found that applying the contrastive loss directly on hih_i is less effective than adding a small MLP first. This projection network g(⋅)g(·) typically consists of one hidden layer, followed by ReLU, and then projecting onto a 128-dimensional vector ziz_i used for contrastive loss calculation. After training, they discard g(⋅)g(·) and use the encoder representation hih_i for downstream tasks.

The most critical part is how to define the contrastive loss.

The core concept is: for the same pair (i,j)(i, j) (which come from the same original image), they should be as close as possible in the vector space, while being as far apart as possible from unrelated samples. Here, the authors refer to the NT-Xent (Normalized Temperature-scaled Cross Entropy Loss) formulation.

NT-Xent Loss Function

NT-Xent is an InfoNCE-style contrastive cross-entropy with a specific two-view batch construction, cosine similarity, temperature scaling, and symmetric loss. L2-normalized features make their dot product equal to cosine similarity.

The NT-Xent loss function is computed as follows:

ℓi,j=−log⁡(exp⁡(sim(zi,zj)/τ)∑k=12N1[k≠i]exp⁡(sim(zi,zk)/τ)).ℓ_{i,j} = - \log \left( \frac{\exp(\text{sim}(z_i, z_j)/τ)}{\sum_{k=1}^{2N} \mathbf{1}[k \neq i] \exp(\text{sim}(z_i, z_k)/τ)} \right).

Where:

  • ziz_i is the 128-dimensional vector obtained from the projection head after processing the ii-th augmented image.
  • sim(u,v)=u⊤v∥u∥∥v∥\text{sim}(u, v) = \frac{u^\top v}{\|u\|\|v\|} represents cosine similarity.
  • ττ (tau) is the temperature hyperparameter, which controls the scaling of similarity scores.
  • The numerator, exp⁡(sim(zi,zj)/τ)\exp(\text{sim}(z_i, z_j)/τ), represents the exponentiated similarity score between ziz_i and the positive sample zjz_j.
  • The denominator is the sum of the exponentiated similarity scores between ziz_i and all other vectors in the batch (excluding itself).

In a batch, assume there are NN original images, each undergoing two different augmentations, resulting in a total of 2N2N augmented images. For a positive pair (i,j)(i, j) (i.e., two different augmentations of the same original image), the loss is computed while treating the remaining 2(N−1)2(N - 1) augmented images as negative samples.

The objective is to maximize the similarity between positive samples (zi,zj)(z_i, z_j) while minimizing the similarity of all other negative samples. That is, we aim for sim(zi,zj)\text{sim}(z_i, z_j) to be much greater than sim(zi,zk)\text{sim}(z_i, z_k) (for all k∉{i,j}k \notin \{i,j\}). This encourages the model to learn more discriminative representations, ensuring that similar samples cluster together while dissimilar samples are pushed apart.

Within a batch, for each positive pair (i,j)(i, j), both (i,j)(i, j) and (j,i)(j, i) losses are computed, then averaged over the 2N2N anchor views for backpropagation.

Discussion

Most Useful Augmentation Combinations

To observe the impact of different data augmentation methods (either individually or in combination) on the quality of representations learned by the model, the authors use linear evaluation. In this approach, the pre-trained encoder is frozen, and a linear classifier (usually a fully connected layer) is added on top, followed by top-1 accuracy evaluation on ImageNet.

In the experiment, the model input has two "parallel augmentation pipelines." Here, the authors intentionally apply the tested augmentation to "one of the pipelines," while the other branch applies only basic random crop + resize. This allows a clearer observation of the effects of individual augmentations or combinations, without their effects being mixed.

The interpretation of the table in the image is as follows:

  • Diagonal entries: Single transformations (e.g., Gaussian blur, color distortion), representing the application of only that augmentation to one branch.
  • Off-diagonal entries: Combinations of two augmentations (e.g., first Gaussian blur, then color distortion).
  • Last column: The average value of each row, which is the average performance under the augmentation combination for that row.

The experimental results show:

  • Single augmentations (diagonal entries) are usually not enough for the model to learn strong representations. With only one variation, the model can still rely on other invariant cues to identify "positive pairs."
  • Augmentation combinations (off-diagonal entries) vary in their results; crop plus color distortion is notably stronger than either one alone.

Suitable combinations make the contrastive task harder and improve linear evaluation in this experiment.

Impact of Augmentation Strength

We can adjust the strength of augmentations, such as increasing or decreasing the variation in brightness, contrast, saturation, and hue. The authors further explore the impact of "augmentation strength" on model performance.

When training supervised classification models on ImageNet, auto-augmentation strategies like AutoAugment are commonly used. However, the authors find that AutoAugment is not necessarily better than the "simple crop + strong color distortion" approach.

In the table, SimCLR rises from 59.6% at one-eighth color-distortion strength to 63.2% at full strength and 64.5% with blur. AutoAugment gives 61.1% here. The supervised row does not show the same gain from stronger color distortion, so the useful augmentation policy depends on the training objective.

Scaling Up the Model

The figure compares model depth and width under several training durations: blue dots are SimCLR after 100 epochs, red stars are SimCLR after 1000 epochs, and green crosses are supervised ResNets after 90 epochs. In these settings, greater depth or width improves SimCLR's linear evaluation.

This resembles our experience with supervised learning: more capacity can support richer representations. Within the plotted sizes, the accuracy gap between the unsupervised and supervised models also narrows as models grow.

Batch Size Impact

The figure above shows performance at different batch sizes and training durations, with each bar representing the result from a single training experiment.

Traditionally, batch size selection mainly considers computational efficiency and gradient stability. However, in contrastive learning, batch size also plays a crucial role: it impacts the number of available negative samples.

  • Larger batch sizes mean more negative samples available per training step, allowing the model to learn richer contrastive information and improve sample discrimination.
  • Advantage at fewer epochs: At 100 epochs, larger batches generally reach higher linear-evaluation accuracy in this figure. The plot compares epochs, not actual training time.

This differs from supervised learning. In supervised learning, large batches mainly aim to "stabilize gradient estimates and improve training efficiency." But in contrastive learning, more negative samples are the core advantage brought by large batches.

Another interesting finding is that more training epochs can partially offset the disadvantages of small batches:

  • As training epochs increase and batches are randomly resampled, the accuracy gap between smaller and larger batches shrinks; some settings nearly meet.
  • The figure does not compare equal wall-clock time or total computation, so it cannot by itself show which batch size uses fewer resources.

Comparison with Other Methods

The authors compare the linear evaluation results of various self-supervised learning methods (i.e., freezing the backbone and adding a linear classifier on top).

The results show that a standard ResNet-50 SimCLR reaches 69.3% top-1, meeting or exceeding several earlier methods that use specialized architectures. The 4×-wide version reaches 76.5%, close to the supervised ResNet-50 reference in the figure, though their model sizes differ: about 375M versus 24M parameters.

If the number of ImageNet labels is reduced to 1% or 10%, and fine-tuning is done using class-balancing, as shown in the table below:

For top-5 accuracy with 1% and 10% of labels, SimCLR 4× reaches 85.8% and 92.6%, the highest values in this table. Standard ResNet-50 SimCLR reaches 75.5% and 87.8%; it does not exceed every comparison method. The results show the value of these pretrained representations with few labels, while model size still matters when reading the table.

Conclusion

In this study, the authors propose a simple yet effective contrastive learning framework and thoroughly analyze the impact of various design choices on learning outcomes.

The experiments establish data augmentation, nonlinear projection heads, and NT-Xent as effective components for the reported self-supervised and semi-supervised settings. Transfer results vary by task; SimCLR does not beat every baseline on every downstream dataset.