Deep learning and neural networksLoading progress…

Contrastive learning

Compare learned embeddings, transfer learning, fine-tuning and contrastive training objectives.

Pull matching pairs together, push the rest away

Contrastive learning trains an encoder so that two views of the same item (a positive pair) get a high similarity, while views of different items (the negatives) get a lower one. For one anchor, softmax turns all the candidate similarities into probabilities, and the loss is −ln of the positive's probability.

The worked example: positive logit ln 2 and one alternative with logit 0 give weights 2 and 1, so the positive has probability 2/3 and the loss is −ln(2/3) ≈ 0.405. Real batches have many more alternatives.

positive logit0.69number of alternatives3
candidatelogitweight e^logitprobability
positive (the other view of the same item)0.6920.4
alternative 1010.2
alternative 2010.2
alternative 3010.2
loss = −ln P(positive) = −ln 0.4 = 0.9163 lower loss = the positive is pulled toward its anchor RELATIVE to the alternatives
Change the positive logit and the number of alternatives.

Predict first. Could an augmentation accidentally erase the feature needed for the downstream task?

Chapter context

A representation is useful when its structure supports a downstream task. Learning a representation changes which examples appear similar, but those similarities inherit assumptions from training data and objectives.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

One contrastive loss for an anchor a and positive p is

L=−log⁡exp⁡(s(a,p)/τ)∑j∈Cexp⁡(s(a,j)/τ).L=-\log\frac{\exp(s(a,p)/\tau)}{\sum_{j\in\mathcal C}\exp(s(a,j)/\tau)}.

The candidate set includes the designated positive; other candidate definitions depend on the method. Temperature tau is positive and controls concentration.

Worked example

For a candidate set with positive logit ln 2 and one alternative logit 0, exponentiated weights are 2 and 1. The positive probability is 2/3 and loss is −ln(2/3)≈0.405465. This demonstrates the normalization; actual contrastive batches usually contain many more candidates.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import log, exp, isclose
logits=[log(2),0]
prob=exp(logits[0])/sum(exp(v) for v in logits)
assert isclose(prob,2/3)
assert isclose(-log(prob),.40546510810816444)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Embedding proximity is task-dependent. Frozen parameters can coexist with a trainable head. Parameter-efficient tuning still requires evaluation for forgetting and task failures.