Practice in Lab: Train paired embeddings with symmetric InfoNCE →
Train paired embeddings with symmetric InfoNCE
A contrastive batch declares which two observations belong together and treats the other in-batch candidates as negatives. Normalizing embeddings makes scale distinct from similarity, while temperature controls how strongly difficult candidates contribute. A symmetric objective trains both retrieval directions. Reordering one side alone changes pair identities; reordering both sides together must preserve the loss. This small CPU problem uses actual autograd and local negatives. It does not execute a distributed gather, establish that every negative is valid, or remove false-negative bias.
import torch
x = torch.eye(2)
print(x @ x.T)Review the design
Audit pair identity, false negatives, temperature and batch composition. Distributed negatives additionally need correct cross-worker gradient and identity handling.
Your task
For finite nonzero aligned [N,D] embeddings, N>=2, L2-normalize each row, form a@b.T / temperature and average row-wise and column-wise cross entropy against diagonal pair identities. Return a differentiable scalar; gradients must reach both encoders. Reject zero vectors, bad shapes and nonpositive/nonfinite temperature.
The explanation opens here once every check passes.