Contrastive and pretext objectives
Quick answer
Self-supervised learning constructs training targets from the input itself rather than requiring human labels. Contrastive objectives compare related and unrelated views; pretext tasks predict a withheld or transformed part of the input. Learned representations can then be transferred to a downstream task. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Compute stable single-anchor contrastive loss
import maths=[1.,0.,-1.];tau=.5a=[v/tau for v in s];m=max(a)print(round(m+math.log(sum(math.exp(v-m) for v in a))-a[0],4))Observed local output:
0.1429Higher positive similarity reduces the loss when the other scores are fixed.
How it works
An encoder f maps inputs to vectors. In a contrastive example, two augmentations of one observation form a positive pair; views from other observations can form negatives. With normalized vectors, dot product equals cosine similarity. For anchor similarity scores s+ and s_j, a single-anchor contrastive loss is −log(exp(s+/tau)/sum_j exp(s_j/tau)), where the denominator includes the positive and tau > 0 controls score sharpness. Subtract the maximum scaled score before exponentiation for numerical stability. This illustrates one anchor, not the complete symmetric SimCLR training recipe. Positive pairs should share meaningful content; false negatives may be semantically similar, and some self-supervised methods use no explicit negatives. Pretext objectives include predicting masked token content or reconstructing a held-out feature from the visible features. Do not let the target remain visible through another derived input. A downstream linear probe freezes the encoder and trains a small classifier on labeled training representations. Fine-tuning updates the encoder too. Use only permitted training inputs for pretraining, even though no human labels are needed. Otherwise test distribution leakage can invalidate inductive evaluation. Self-supervision is often grouped under unsupervised representation learning, but differs from semi-supervised learning's explicit combination of labeled and unlabeled examples; taxonomies vary. Source context: Chen et al., ICML 2020, SimCLR (https://proceedings.mlr.press/v119/chen20j.html); examples here are original simplified objectives.
Predict a masked feature without exposing it
import numpy as npfrom sklearn.linear_model import LinearRegressionX=np.array([[0,1],[1,3],[2,5],[3,7]],dtype=float)encoder=LinearRegression().fit(X[:3,:1],X[:3,1])print(encoder.predict(X[3:,:1]).round(6).tolist())Observed local output:
[7.0]A simple pretext prediction learns a relationship from visible features. This is not a trained contrastive encoder or evidence of useful transfer.
Common mistakes
Do not equate every self-supervised method with contrastive learning, use the test set for pretraining, or interpret a low pretext loss as a guarantee of transfer. Avoid leaking the masked target through input preprocessing.
Practice and next steps
Compare the single-anchor loss for similarities [1,0] versus [0,1], with the first entry positive and tau=1. Explain which encoder would be preferred by the contrastive objective. Complete the knowledge check and staged exercise. Next: Label-efficiency evaluation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Contrastive and pretext objectives — knowledge check
Apply contrastive and pretext objectives to new scenarios.
Where do self-supervised training targets come from?
What does a positive pair commonly represent?
Why subtract the maximum scaled score?
What is a frozen linear probe?
What is a pretext leakage example?