Inferensys

Glossary

In-Batch Negative Sampling

A training efficiency technique that reuses other positive items within the same mini-batch as negative samples for a given query, leveraging the random composition of batches to approximate the full softmax distribution.
Developer working on RAG retrieval system, document chunks visible on screen, technical workspace with code editor.
TRAINING EFFICIENCY

What is In-Batch Negative Sampling?

A technique that reuses other positive items in a mini-batch as negative samples, approximating the full softmax distribution without an exhaustive search of the entire item catalog.

In-Batch Negative Sampling is a training efficiency technique for dual-encoder and sequence models where the positive items for other queries within the same mini-batch are treated as negative samples for a given query. This leverages the random composition of batches to approximate the global softmax distribution, eliminating the need to compute scores against the entire item corpus during each training step.

The method increases the effective number of negative samples to batch_size - 1 per positive pair at virtually no additional computational cost, since the item embeddings are already computed. However, it introduces a sampling bias toward popular items that appear more frequently in batches, which is often corrected using log-Q correction or mixed negative sampling strategies.

MECHANISM

Key Characteristics of In-Batch Negative Sampling

A training efficiency technique that reuses other positive items within the same mini-batch as negative samples, leveraging the random composition of batches to approximate the full softmax distribution.

01

The Core Mechanism

In a standard Two-Tower Model, a batch contains B user-item pairs. For each user, the correct item is the positive sample. The other B-1 items in the batch are treated as negative samples. The model computes the dot-product similarity for all B x B pairs simultaneously using a single matrix multiplication, making it extremely compute-efficient. The InfoNCE loss (or sampled softmax) then maximizes the score for the correct pair while minimizing scores for the in-batch negatives.

02

The Popularity Bias Problem

Because negatives are drawn from the empirical distribution of items in the batch, popular items appear more frequently as negatives. This causes the model to unfairly penalize popular items, pushing their embeddings away from users even when they might be relevant. The result is an embedding space where popular items are artificially depressed in ranking. Mitigation strategies include:

  • LogQ correction: Adjusting logits by subtracting the log of the item's sampling probability.
  • Mixed negative sampling: Combining in-batch negatives with uniformly sampled negatives.
03

Batch Size as a Hyperparameter

The number of negatives per positive is B-1, making batch size a critical hyperparameter. Larger batches provide more diverse negatives, better approximating the full softmax over the entire item catalog. However, GPU memory constraints limit batch size. Typical strategies:

  • Gradient accumulation across multiple micro-batches to simulate a larger effective batch.
  • Cross-device batch sharing where negatives from other GPU workers are incorporated.
  • Batch sizes of 2048–8192 are common in production two-tower recommenders.
04

Accidental Positive Collisions

A significant failure mode occurs when the batch contains multiple positive pairs for the same item. If item i is the ground-truth positive for user u, but also appears as a positive for user v in the same batch, it will be treated as a false negative for user u. This confuses the training signal. Solutions include:

  • Deduplication: Ensuring each item appears only once per batch.
  • Label-aware masking: Masking out in-batch negatives that are actually positives for the query user.
05

Temperature Scaling

The temperature parameter τ controls the concentration of the similarity distribution. A low temperature (τ < 1) sharpens the distribution, making the model focus intensely on hard negatives. A high temperature (τ > 1) smooths the distribution, treating all negatives more uniformly. Temperature is typically tuned as a hyperparameter, with values between 0.05 and 0.2 being common in practice. It directly impacts the gradient magnitude flowing to hard versus easy negatives.

06

Comparison to Uniform Negative Sampling

In-batch negative sampling is orders of magnitude faster than maintaining a separate negative sampling pool because it reuses the forward pass computations already performed for positive items. However, uniform negative sampling from the full corpus provides an unbiased estimate of the partition function. The trade-off:

  • In-batch: Fast, biased (popularity skew), batch-size-dependent.
  • Uniform: Slower, unbiased, requires a separate sampling infrastructure.
  • Hybrid approaches combine both, using in-batch for efficiency and a small set of uniformly sampled negatives for debiasing.
TRAINING EFFICIENCY

Frequently Asked Questions

Clear, technical answers to the most common questions about in-batch negative sampling, its mechanisms, and its role in modern recommender system training.

In-batch negative sampling is a training efficiency technique that reuses other positive items within the same mini-batch as negative samples for a given query, eliminating the need to explicitly sample negatives from the full item corpus. During each training step, a batch of (user, positive_item) pairs is processed through a two-tower model. For each user in the batch, the positive items of all other users in that same batch are treated as negatives. This leverages the random composition of mini-batches to approximate the full softmax distribution. The technique is foundational to deep learning recommender systems because it computes the loss using only the in-batch items, reducing the computational complexity from O(|I|)—where |I| is the catalog size—to O(B), where B is the batch size. The InfoNCE loss is then calculated by contrasting the true positive pair against all in-batch negatives, pulling the user and positive item embeddings closer while pushing apart the user and sampled negative embeddings.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.