PythiloLesson 4 of 9 · progress kept in this browser

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)
This example uses torch. Run it in the Pythilo app, where these features are available.
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.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.