Notes

Sampled Softmax

Training a retrieval model as classification over the catalogue means a softmax with one output per item. At ten million items, computing that denominator once per training example is the entire cost of training. Sampled softmax is the standard way of not paying it.

The mechanism
A full softmax normalises the user-item score over every item. Sampled softmax replaces that denominator with the positive item plus a small set of sampled negatives, and corrects each sampled logit by subtracting the log of its sampling probability. That correction is what keeps the gradient an approximately unbiased estimate of the full-softmax gradient; drop it and frequent items are systematically over-penalised. In a two-tower model the negatives are commonly the other items already in the minibatch (in-batch negatives), which costs one extra matrix multiply plus a logQ correction, because batch membership is itself popularity-weighted.

Choices that decide whether it works

  • Negative distribution. Uniform negatives are easy and teach little; popularity-weighted negatives sharpen head items; hard negatives mined from an index sharpen the decision boundary at the cost of gradient noise.
  • Count. More negatives tighten the approximation and raise recall, which is why large-batch training matters far more for retrieval than for ranking.
  • False negatives. A sampled negative the user would have loved is a label error, and drawing one gets likelier as sampling tilts toward popularity.

Why it matters
This is what makes a retrieval tower trainable at catalogue scale, and the negatives shape the embedding space that FAISS or ScaNN later searches. Get it wrong and the index is fast, well-engineered, and full of the wrong neighbours.

Sampled softmax trains a retrieval model as classification over the catalogue while normalising over only the positive item and a small set of sampled negatives, correcting each logit by its log sampling probability so the gradient stays approximately unbiased. It is what makes two-tower training tractable at catalogue scale, usually via in-batch negatives. The negative distribution and count directly set retrieval recall and the geometry of the embedding space an ANN index later searches.

Teaching software to pick your next video sounds like a multiple-choice question with ten million options. Grading every option on every attempt would be impossibly slow.

Sampled softmax is the trick of grading against a short list instead: the thing the person actually chose, plus a few hundred random others. Learn to prefer the real one over those, repeat the exercise millions of times, and the model ends up with a usable sense of taste. The maths includes a small adjustment so that popular items, which get pulled into the short list more often, are not unfairly punished for being popular.