Contrastive learning is a machine learning method that trains models by comparing similar and dissimilar examples.Contrastive learning is an approach in machine learning that allows building high-quality data representations without labels (self-supervised learning). It relies on the concept of contrastive loss: the model attracts representations (embeddings) of semantically close objects (positive pairs) and repels representations of unrelated ones (negative pairs). For example, in images, positive pairs are different views (augmentations) of the same photo, while negatives are all other images in the mini-batch. This way, the neural network learns to extract invariant features, ignoring irrelevant variations in lighting, rotation, scale. The method underpins many modern recognition systems, where it often outperforms supervised learning when labels are scarce.
The roots trace back to metric learning: Siamese networks (Bromley et al., 1993) for signature comparison. In 2006, Hadsell and LeCun proposed contrastive loss for learning invariant representations. Revival of interest is linked to progress in self-supervised learning for images: in 2018, the InstDisc model (Wu et al.) used a memory bank; breakthroughs came with MoCo (He et al., 2020) with a queue of negative examples and SimCLR (Chen et al., 2020) with large batch sizes and strong augmentation. These works showed that contrastive pretraining achieves accuracy close to supervised learning, and sometimes surpasses it when fine-tuning on small datasets. The approach was later adapted for video, audio, graphs, and multimodal data (CLIP, 2021).
The algorithm includes three stages: (1) stochastic data augmentation creates two independent representations of the same input (e.g., random cropping, color distortion); (2) an encoder (ResNet, ViT) maps them into vectors in a latent space; (3) a projection head (MLP) transforms them into a lower-dimensional space where the contrastive InfoNCE loss is computed: L = -log[ exp(sim(z_i, z_j)/τ) / ∑_{k=1}^{2N} 1_{k≠i} exp(sim(z_i, z_k)/τ) ], where sim is cosine similarity, τ is a temperature parameter. Optimization reduces the cross-entropy between the distributions of positive and negative pairs. To prevent representational collapse (all vectors degenerating to a single point), mechanisms are employed: negative pairs from a queue (MoCo), stop-gradient and predictor (BYOL), clustering (SwAV). As a result, the latent space acquires semantic structure invariant to the specified transformations.
Face recognition without labels, searching for similar photos in large archives, improving diagnoses from medical images when labeled data is scarce.
OpenAI's CLIP system, trained contrastively on image-text pairs, can generate images from descriptions even though it was never directly taught artistic style — it simply captured the correlations.





















