In contrastive learning the model is not told what each thing is, but what resembles what. It is shown positive pairs (a question and its answer, a photo and its caption) and negative ones (random combinations), and the embeddings are adjusted so that positives end up close and negatives far apart in vector space, under some measure such as cosine similarity (Alammar & Grootendorst, 2024, pp. 291–293).
One of the earliest examples in language was word2vec: a word and its neighbors in the text form positive pairs, and randomly chosen words serve as negatives (negative sampling) (Alammar & Grootendorst, 2024, p. 293). Sentence-BERT trains sentence embeddings this way for semantic search (Reimers & Gurevych, 2019), and CLIP aligns images and texts using 400 million image–caption pairs (Radford et al., 2021). CLIP uses batches of 32,768 pairs: in each, every image has one correct caption and more than 32,000 incorrect ones, which serve as free negatives (see epoch).
Alammar & Grootendorst (2024) connect the technique to an idea from philosophy of science: contrastive explanation. When a robber was asked why he robbed banks, he answered "because that is where the money is": he explained why banks rather than somewhere else, when the question was why rob rather than obey the law. To explain is always to explain why P rather than Q (2024, p. 292). Likewise, a model learns what makes something a dog when it is asked, in effect, why this is a dog and not a cat. For a linguist, the resonance is Saussurean: in language, Saussure (1916) said, there are only differences, and a sign's value depends on its opposition to other signs. Contrastive learning builds exactly that: a space in which each point is worth its distances to the others.