HomeMachine Learning & Neural NetworksMomentum Contrast (MoCo) in 2D: The Negative-Pair Queue

Momentum Contrast (MoCo) in 2D: The Negative-Pair Queue

Interactive 2D contrastive-learning simulator: train a query encoder against a momentum-updated key encoder and a growing FIFO negative queue on a unit circle, watching the real InfoNCE loss, cosine similarities and a live loss curve respond to temperature, momentum and queue size — the mechanism behind MoCo.

Machine Learning & Neural Networks2DAdvanced60 FPS📱 Mobile-adapted⇄ 3D version
2d-ds-topic-55 ↗ Open standalone

Self-supervised contrastive learning trains an encoder without any labels by pulling together embeddings of two augmented views of the same example (a positive pair) while pushing apart embeddings of everything else (negative pairs) — the InfoNCE objective at the heart of SimCLR, MoCo and CLIP-style pretraining. This simulator visualizes MoCo's specific answer to the "where do the negatives come from" problem: instead of needing an enormous training batch like SimCLR, a slow momentum-averaged key encoder feeds a running FIFO queue of past embeddings that any later training step can draw negatives from. Six colour-coded classes live as unit vectors on a 2D circle; each training step samples one class, computes the real InfoNCE loss and its tangent-line gradient against the queue, nudges that class's query embedding along the gradient, drifts its momentum key encoder toward the new query position, and enqueues the fresh key — all driven by the temperature, momentum, queue size and step-size controls, with the loss curve and negative-similarity histogram updating live alongside the main view.

⚙ Under the hood

Interactive 2D contrastive-learning simulator: train a query encoder against a momentum-updated key encoder and a growing FIFO negative queue on a unit circle, watching the real InfoNCE loss, cosine similarities and a live loss curve respond to temperature, momentum and queue size — the mechanism behind MoCo.

contrastive-learningmocoinfonceself-supervised-learningembedding-spacemomentum-encoder

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)