Deep learning and neural networksLoading progress…

Self-supervision

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

Labels from the data itself

Labelled data is expensive; raw text, images and audio are plentiful. Self-supervised learning invents the training task from the data: hide something and predict it. There is still an objective and a target; the target just comes from the data itself instead of a person.

input : the cat [MASK] on the mat target: sat (comes from the data itself, nobody labelled it)

The way the target is built decides what the model must learn: masking forces use of both sides of the context, next-word prediction forces it to anticipate.

Two ways to build targets from one sentence.

The choice matters. Predicting a masked word uses context on both sides; predicting the next word only the left. For images, building two augmented views of one picture teaches invariance to the augmentations (crops, colour shifts).

Predict first. Does self-supervision mean training without any objective or target?

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.