Skip to content
AI.info

Recommender systems

Negative Sampling and Retrieval Training

Design in-batch, random, popularity, hard, and cross-batch negative sampling while controlling false negatives and sampling bias.

By the end you can

Example

A production app recommender, and the negatives it needed

Two-tower app recommenders are not hypothetical. Google applied one to a large-scale production app recommendation system, and in 2020 published the sampler it took to make the training work. Mixed Negative Sampling has a stated purpose: “In particular, different from commonly used batch or unigram sampling methods, MNS uses a mixture of batch and uniformly sampled negatives to tackle the selection bias of implicit user feedback.” Online A/B testing showed improved retrieval quality through more high-quality app installs. The model did not change. What each positive got compared against did.

Why a batch-only contrast is not enough has been measured. Count how often locally drawn negatives contain informative ones and the answer is none. The overlap of NCE or random in-batch negatives with the top-100 highest-scored negatives is zero. BM25 negatives reach 15%, ANCE negatives 63%. Microsoft's ANCE paper reports it. Zero overlap is what easy-negative saturation looks like with a number on it.

The risk on the other side has a number too. The RocketQA team went and looked: “Specifically, we manually examine the top-retrieved passages of 100 questions, that were not labeled as true positives. We find that about 70% of them are actually positives or highly relevant.” Mine from the top of your own ranked list and much of what you mine may be the right answer.

  • Easy-negative saturation: the overlap of random in-batch negatives with the top-100 highest-scored negatives is zero, and the visible symptom is near-zero training loss.
  • Hard decision region: for offline mining the hardest examples are not the best choice — Facebook Search got its best model recall sampling between rank 101-500.
  • False negatives: MS MARCO has 8.8M passages but only about 1.1 annotated positives per question, and RocketQA's manual check of 100 questions found about 70% of the top-retrieved unlabelled passages were positive or highly relevant.
  • Batch dependence: in-batch negatives inherit the composition of each mini-batch, which is why Mixed Negative Sampling adds uniformly sampled items to tackle the selection bias of implicit user feedback.
  • Sampling correction: the in-batch skew can be corrected — Google's algorithm sketches and estimates item frequency from streaming data via gradient descent, without requiring a fixed item vocabulary.

Negative sampling is part of the retrieval objective

Large-catalog retrieval cannot compare every query with every item on every update. MS MARCO holds 8.8M passages. The neural retrieval system Google built for YouTube was deployed over a corpus with tens of millions of videos. Training therefore uses a subset of negatives, and the model learns the distinctions the sampler emphasises.

How much rides on that subset is said plainly in the Dense Passage Retrieval paper: “In practice, how to select negative examples is often overlooked but could be decisive for learning a high-quality encoder.” Table 3 of that paper is the demonstration, on the Natural Questions development set. Without in-batch reuse, the choice between random (64.3%), BM25 (63.3%) and gold (63.1%) negatives barely moved top-20 accuracy at all. Take the same seven gold negatives in-batch and it moves from 63.1% to 69.1%. The negatives were the same. Only how they were used changed.

Six samplers are in play: random, in-batch, popularity, mixed, cross-batch, and hard-negative. Each buys difficulty and coverage at a different price in compute and in false negatives. And when the sampled objective should approximate a full-corpus softmax, the sampling probability may need correcting as well.

Picking a sampler decides which distinctions the model never gets the chance to learn, which makes it a modeling choice rather than a training detail.

Case

In-batch negatives are convenient, and skewed by a power-law distribution

Batches are not neutral. Items follow a power-law distribution, so a loss computed inside a batch is skewed by whichever items happen to be in it. Google stated the problem in 2019: “However, in-batch loss is subject to sampling biases, potentially hurting model performance, particularly in the case of highly skewed distribution.” The paper's opening sentence names the power-law item distribution as the source of that skew, and its modelling section names the direction: “popular items are overly penalized as negatives due to the high probability of being included in a batch.”

The correction is an algorithm that sketches and estimates item frequency from streaming data via gradient descent, without requiring a fixed item vocabulary. It was built for a large-scale neural retrieval system for YouTube, deployed over a corpus with tens of millions of videos and validated in live A/B tests. Google's own publication page names that deployed system Neural Deep Retrieval (NDR). The camera-ready PDF does not use the name.

A deployed search system reports the mirror concern. Facebook Search diagnoses people search bluntly: “the negative training data were too easy as they were random samples which are usually with different names”. Online hard negative mining then “consistently improved embedding model quality significantly across all verticals: +8.38% recall for people search; +7% recall for groups search, and +5.33% recall for events search”. The optimal setting was “at most two hard negatives per positive”, before model quality regressed.

The sampler is part of the objective.

Visual

The negative-sampling design space

Every row answers one question differently: what should this positive be compared against?

The in-batch row has a price, measured on the Natural Questions development set. Seven gold negatives with no in-batch reuse give 63.1% top-20 accuracy. The same seven taken in-batch give 69.1%. Thirty-one in-batch give 70.8%, and 127 in-batch give 73.0%. That is contrast bought cheaply, by reusing what is already in the batch. Random catalogue draws keep the comparison broad; in that same table, without in-batch reuse, they sit at 64.3%.

The popularity-and-mixed row is a shipped sampler, not a category in a diagram. Mixed Negative Sampling blends batch negatives with uniformly sampled ones precisely to tackle the selection bias of implicit user feedback.

The hard-or-retrieved row is where the boundary gets sharpened, and it is priced too. A single BM25 hard negative lifts DPR's top-20 accuracy to 77.3% (31+32) and 78.0% (127+128). A second BM25 negative does not help (76.4%).

Cross-batch memory buys size rather than aim. RocketQA's ablation on MS MARCO passage ranking gives that row its own number: 32.39 MRR@10 for in-batch negatives, 33.32 for cross-batch.

FigureLayers · 5 layers
  1. 01

    Random catalog negatives

    Broad coverage with many easy comparisons.

  2. 02

    In-batch negatives

    Efficiently reuse positives from other examples as contrasts.

  3. 03

    Popularity or mixed negatives

    Expose common competitors and control selection bias.

  4. 04

    Hard or retrieved negatives

    Train near the current decision boundary.

  5. 05

    Cross-batch memory

    Increase contrast set size using recent embeddings.

Predict first

Harder negatives, uncleaned

RocketQA's authors trained a dense passage retriever with the cheap default, in-batch negatives, and recorded its score. Then they swapped in hard negatives: passages that a strong retriever ranked highly for the question but that were not labelled as answers. They did not filter those passages first.

Make a prediction before reading on. Against the in-batch baseline, did the uncleaned hard negatives raise the retriever's score a lot, a little, or do something else?

Settle on an answer, then read on — the next section is the reveal

Steps

Putting the negative pipeline under control

Against what, exactly, is each positive being contrasted? The universe of negatives is settled first, then mixed by difficulty, masked for known positives, and tracked while training runs.

Step 2 has published settings, not just an instruction to combine broad and hard. Blending random with hard negatives kept improving recall for Facebook Search, saturating at easy:hard = 100:1. For offline mining the hardest examples were not the best choice; sampling between rank 101-500 achieved the best model recall. Online mining worked best with at most two hard negatives per positive.

Step 4 has a price attached. In RocketQA's ablation, undenoised hard negatives collapse to 26.03 MRR@10, below the 32.39 in-batch baseline. Denoised hard negatives reach 36.38. Step 5 exists because Krichene and Rendle showed that a sampled evaluation cannot settle the question on its own.

FigureProcess · 5 steps
  1. 1. Define the contrast universe

    Specify eligible items and exclusions at each training cutoff.

  2. 2. Mix difficulty levels

    Combine broad negatives with realistic competitors.

  3. 3. Mask known positives

    Handle duplicates, multi-positive requests, and equivalent items.

  4. 4. Track sampler statistics

    Measure popularity, hardness, false-negative rate, and coverage.

  5. 5. Re-evaluate online

    Check retrieval recall, tail exposure, and mature outcomes after sampler changes.

Example

The app recommender’s negatives, under control

Random catalogue negatives trained a two-tower app recommender: it learned to beat easy negatives and failed on substitutes. The recommender is invented, and the quiz below asks about it. Here is the routine run on it. All numbers are invented for the exercise.

  • Define the contrast universe: negatives come from apps available in the user's country and on the user's device at the training cut-off; apps the user already has are excluded.
  • Mix difficulty levels: each positive is contrasted with 20 random apps and 2 hard ones drawn from ranks 101 to 500 of the current retriever in the same category, not from its very top, where the user's real alternatives sit.
  • Mask known positives: lite and pro editions of the same app count as one item, and a hard negative from the same developer as the positive is skipped, so the model is never taught that a user dislikes the app they installed under another name.
  • Track sampler statistics: of the hard negatives drawn in a week, 4.5% were installed by the same user within 30 days, against 0.05% of random ones. That rate is plotted every run; a rise means the sampler is mining wanted apps.
  • Re-evaluate online: for users who installed a fitness app, count how many of its 5 nearest competitors appear in the top 100 candidates. Under random negatives it was 1.2 of 5; with the mixed sampler 3.6 of 5. The install rate and tail exposure are then read in a live test before the sampler ships.

Analogy

Training a goalkeeper with selected shots

Slow shots from distance teach a goalkeeper very little about close, deceptive attempts. Harder practice helps, though some of the “opponents” turn out to be teammates.

A diet of only the hardest shots is not the answer either, and the Facebook Search team says so from measurement: “One finding that may first seem counterintuitive is that models trained simply using hard negatives cannot outperform models trained with random negatives.” What worked was a blend that kept improving recall until it saturated at easy:hard = 100:1. A session that is mostly ordinary shots.

Negative sampling carries the same curriculum and labeling tension. A coach chooses the drills. An adaptive sampler decides what counts as a hard shot from the model's current weaknesses, so the curriculum shifts every time the keeper improves.

The chosen contrasts determine which retrieval errors the model learns to correct.

Evidence should separate encoder and sampler effects

One variable at a time is the whole design, and the ANCE experiments ran it: the BERT-Siamese encoder and the NLL loss held fixed, only the negatives varied. On the MS MARCO passage development set, MRR@10 is 0.261 with random in-batch negatives, 0.256 with hardest-in-batch NCE negatives, 0.299 with BM25 negatives, 0.311 with DPR-style BM25-plus-random negatives, and 0.330 with globally retrieved ANCE negatives. The mechanism is in the preprint's own words: “As expected, the uninformative local negatives are trivial to separate, yielding near-zero training loss, while ANCE global negatives are much harder and maintain a high training loss.” One variable moved. The ordering of those five rows is attributable.

Report loss together with full-corpus or large-candidate recall. The sampled number is not simply a noisier version of the real one. Krichene and Rendle proved the point in 2020: “This paper investigates sampled metrics in more detail and shows that they are inconsistent with their exact version, in the sense that they do not persist relative statements, e.g., recommender A is better than B, not even in expectation.” The smaller the sampling size, the less difference there is between metrics, until for very small sampling sizes all metrics collapse to AUC. Their conclusion is that sampling should be avoided for metric calculation, and that a study that must sample can improve the estimate with a correction obtained by minimising bias or mean squared error. A sampled score can rank two systems the wrong way round.

Inspect negative examples by hand as well — head, tail, new items, multilingual content, near-duplicate variants. A sampler that looks “hard” numerically may simply be exploiting bad metadata or stale embeddings.

Without that separation you cannot tell whether the sampler or the encoder earned the gain, and the next change gets aimed at the wrong component.

Key idea

A stronger encoder cannot rescue a bad sampler

A better encoder cannot compensate for a sampler that teaches the wrong competition. In the ANCE experiments the encoder never changed — one BERT-Siamese encoder, one NLL loss — and MS MARCO MRR@10 still moved from 0.261 with random in-batch negatives to 0.330 with globally retrieved negatives. That whole range belonged to one decision: what each positive was compared against.

Capacity spent on the encoder buys nothing against a false negative the sampler keeps presenting as a wrong answer.

Key idea

The sampling gate

Approve a negative strategy only when its difficulty, coverage, false-negative risk, and correction assumptions are observable. RocketQA supplies both halves of why. About 70% of the top-retrieved unlabelled passages they examined for 100 questions were actually positives or highly relevant. And mining without denoising cost them the run: 26.03 MRR@10 against a 32.39 in-batch baseline, recovered to 36.38 only once the mined negatives were denoised.

Ask what a proposed sampler would count as a wrong answer before approving it; a strategy whose false-negative risk nobody can observe is not ready for traffic.

Key takeaways