Contrastive Learning

Contrastive learning trains representations by pulling related views (positives) together in embedding space and pushing unrelated views (negatives) apart. Positives are usually augmented views of the same instance; negatives are other instances.

The common InfoNCE form is:

\mathcal{L}_i = -\log \frac{\exp(\operatorname{sim}(z_i,z_i^+)/\tau)} \sum_{j} \exp(\operatorname{sim}(z_i,z_j)/\tau)}

Here is a positive view, are candidate positives/negatives, is a similarity function, and is temperature.

With normalized embeddings, cosine similarity is:

The temperature controls how sharply the model distinguishes close and far examples.

Batch Objective View

For a batch of anchors and candidates, InfoNCE is a classification problem over the positive index:

where and is the candidate set. This makes the negative set part of the objective, not an implementation detail.

Why It Matters

  • Learns useful representations without labels, from the structure of views alone.
  • The augmentation policy encodes the invariances you want the representation to have.
  • Applies to sequences, graphs, molecules, and proteins where defining a meaningful “same entity, different view” is natural.

Design Questions

  • What defines a positive pair?
  • What defines a negative pair?
  • Which augmentations preserve the meaning needed for downstream tasks?
  • Is the objective instance-discrimination, supervised contrastive learning, cross-modal alignment, or retrieval training?

Positive and Negative Semantics

For an anchor , a positive should preserve the relevant identity:

for the intended downstream meaning . A false negative occurs when is treated as negative even though it shares the relevant class, scaffold, family, target, or semantic state.

Design Matrix

ChoiceQuestionFailure Mode
view functionwhat transformations produce ?augmentation removes task-relevant signal
positive rulewhat counts as same identity?positives too easy or semantically wrong
negative setbatch, memory bank, queue, corpus, supervised labels?false negatives or train/test contamination
similaritydot product, cosine, learned score?scale and normalization change temperature meaning
projection headtrain-only or downstream representation?evaluation uses a different representation than training
temperaturefixed, learned, scheduled?overly sharp distribution and unstable gradients

Chem-Bio Examples

ObjectPossible PositiveRisk
moleculetwo SMILES views of same standardized moleculetautomer/protonation changes may alter label
molecular graphgraph augmentation preserving scaffold or pharmacophoreatom/bond deletion may change activity
protein sequencemasked/cropped homologous viewshomolog positives can leak family-level labels
protein structurecoordinate/noise views of same foldalignment or template source may leak
complexligand-pocket views of same interactionknown pose can leak deployment-unavailable geometry

Checks

  • Do augmentations preserve label-relevant content, or do they destroy it (e.g. a molecular edit that changes activity)?
  • Are negatives actually negative, or do they include near-duplicates and same-family members (false negatives)?
  • Does the split avoid leakage through near-duplicates, shared scaffolds, or protein families?
  • Is representation collapse occurring (all embeddings converging to one point)?
  • Does the batch or memory bank contain enough hard negatives without contaminating the split?
  • Is the downstream evaluation linear probe, kNN, retrieval, or full fine-tuning?
  • Are false negatives likely under scaffold, family, or target-based semantics?