This article appears to incorporate text from a large language model, which is prohibited in Wikipedia articles. (September 2026) |

Hard negative mining, also referred to as hard negative sampling, is a family of techniques in machine learning for selecting or constructing negative samples more difficult to distinguish from a corresponding positive example with respect to uniformly-sampled negatives, leading to a more informative training signal for discriminative classifiers and similarity models.
The technique is used when the pool of possible negative examples is far larger than the set of positives and most randomly chosen negatives are easy to classify by the model, contributing little to learning. Concentrating training on the small fraction of difficult negatives is intended to provide stronger gradient signal and faster convergence than uniform sampling.
Hard negative mining originated in object detection in the 1990s, where it was referred to as bootstrapping (not to be confused with statistical bootstrap resampling), and was later formalized for support vector machine models. Closely related ideas appear in deep metric learning, information retrieval, recommender systems, self-supervised contrastive learning, and vision-language models. Surveys distinguish hard negative mining from the broader practice of negative sampling, in which negatives are drawn at random or in proportion to frequency rather than by difficulty.
In many learning problems the number of negative examples vastly exceeds the number of positives. In sliding-window object detection, a single image yields on the order of 105 candidate windows, almost all of which are background. Online hard example mining work reports that the imbalance can reach roughly 100,000 background regions per object for sliding-window detectors and remain about 70:1 even for proposal-based detectors. Using all negative examples is both computationally infeasible and statistically harmful, because the large number of easy negatives dominates the loss and gradient.
A parallel motivation arises in similarity learning. When training examples are grouped into pairs or triplets, the number of candidate tuples grows quadratically or cubically with the number of images, and once a model has converged most such tuples already satisfy the training objective and produce little or no gradient. In high-dimensional embedding spaces, randomly drawn negatives also tend to be uninformative because a significant percentage of them might already be distant from positive samples in the latent space. Selecting the most informative ("hard") examples is therefore presented as a way to keep the training signal steadier and informative throughout the whole training process.
Across application areas, a hard negative is consistently defined relative to the model being trained: it is a negative (an example whose label differs from that of the anchor or query), that the current model nonetheless classifies as a positive. For instance, in a margin-based detector, like a SVM a hard negative is a background example that is misclassified or that falls within the classifier's margin. In embedding-based methods, it is a differently-labelled example that lies close to the anchor in the learned feature space.
Several finer distinctions are commonly utilized:
In classical detectors, hard negative mining is an iterative, cache-based procedure layered on top of an SVM or other classifier: the model is trained on the positives and a small set of negatives, the trained model is run over background data to collect new false positives, easy examples are discarded, and the model is retrained, repeating a few times. A negative is treated as hard when its margin score indicates misclassification or a within-margin position.
Online hard example mining (OHEM) adapts this idea to the training of deep region-based detectors. Rather than alternating offline mining and retraining, OHEM forms each SGD mini-batch by scoring all candidate regions of interest with a read-only forward pass, and then back-propagating only through the highest-loss regions. An efficient implementation keeps two copies of the region network: one read-only copy that scores all regions and one trainable copy that processes only the selected hard examples. Hard negative mining has likewise been applied to convolutional face detectors by harvesting false positives from a first-round Faster R-CNN face detector and retraining on them, labelling a detected region a hard negative when its maximum intersection-over-union with any ground-truth face falls below a certain threshold.

In deep metric learning, models are trained with pairwise or triplet losses so that examples of the same class map to nearby points and examples of different classes map to distant points. Because most randomly sampled triplets quickly satisfy the loss margin and stop contributing gradient, the selection of hard pairs or triplets is widely regarded as essential. The triplet loss, in fact, penalizes triplets in which the anchor–positive distance is not smaller than the anchor–negative distance by at least a margin .
The FaceNet system introduced semi-hard negative mining, in which the chosen negative is farther from the anchor than the matching positive yet still falls within the margin, after finding that always selecting the hardest negatives could drive the embedding toward a collapsed (degenerate) solution; FaceNet also computed hard positives and negatives online from within each training batch rather than searching the whole dataset.
An early example of in-batch top-K selection appears in unsupervised video representation learning, where the negatives incurring the highest triplet loss within a mini-batch are selected. A widely cited variant is batch-hard mining, where a batch is built by sampling a fixed number of classes and a fixed number of examples per class, and for each anchor only the hardest positive and hardest negative within that batch are used; because these are the hardest within a small random subset rather than the whole dataset, the authors argued they act as "moderate" triplets and avoid the cost and instability of offline mining.
Several methods reduce the cost of mining or change what is mined:
In recommender systems and neural information retrieval, training pairs each observed (relevant) item against non-observed items as implicit negatives. Bayesian Personalized Ranking constructs training triples in which an observed item is treated as a positive and a non-observed item as an implicit negative, and trains a pairwise objective by drawing such triples uniformly at random; it does not use hard or dynamic negative selection, and reviews describe it as the uniform-sampling baseline that later hard-negative methods improved upon. In neural dense retrieval, a dual encoder is trained so that relevant query-document pairs have higher similarity than irrelevant ones, and the choice of negatives is decisive.
Several strategies are in common use:
Contrastive self-supervised learning methods learn representations by pulling together different views of the same instance (positives) and pushing apart unrelated ones (negatives). The widely used InfoNCE objective, introduced in contrastive predictive coding, scores one positive against negatives drawn from a proposal distribution:
In its original form, contrastive predictive coding draws its negatives at random and recommends using more negatives because the objective bounds a mutual information quantity that tightens as grows. Several influential methods similarly increase the supply of negatives rather than their difficulty: SimCLR treats all other augmented examples in a large training batch as negatives and argues that its temperature-scaled cross-entropy loss implicitly weights negatives by hardness, whereas margin- and triplet-style losses require explicit semi-hard mining to remain competitive; and Momentum Contrast (MoCo) maintains negatives in a queue decoupled from the batch size, encoded by a slowly updated momentum encoder to keep them consistent.
Explicit hard-negative methods have nonetheless been adapted to the unsupervised setting.

In CLIP-style vision-language models, hard negatives are used to address the "bag of words" behavior of such models, ignoring word order and object-attribute binding. Methods in this area differ in whether they select, construct, or reweight negatives:
For SVM detectors, hard negative mining has a convergence guarantee. An algorithm which alternately mines margin-violating examples into a cache and discards easy ones converges to the model that training on the entire dataset would produce. A complementary line of analysis treats the choice of negatives as an importance sampling problem in which the optimal negative-sampling distribution is proportional to the per-example gradient norm, so that harder (higher-loss) negatives should be sampled more often; this motivates mining the model's current top-ranked negatives.
At the same time, analyses of deep embedding training caution that the hardest negatives can be counterproductive. As the anchor-negative distance shrinks, the gradient direction becomes dominated by noise, yielding high-variance updates that can collapse the embedding. A 2023 theoretical study modelled hard-negative sampling as tilting the negative distribution toward more anchor-aligned negatives and proved that such hardening never decreases the contrastive loss while leaving the globally optimal representation geometry unchanged in the supervised case, while, in some settings harming the unsupervised case.
Several authors report that mining only the single hardest negative destabilizes embedding training. FaceNet found that always selecting the hardest negatives could drive the embedding to a collapsed solution, and adopted semi-hard mining instead. Lifted structured embedding similarly observed that selecting only the hardest negative converges to a poor local optimum, motivating a smoothed objective over all in-margin negatives. Distance-weighted sampling diagnoses the mechanism as noise-dominated, high-variance gradients, and an empirical comparison in hierarchical-triplet-loss work found naive hardest-negative mining giving little improvement over random sampling, with a semi-hard variant performing better, partly because mining within a mini-batch cannot capture the global data distribution.

When labels are unavailable or incomplete, the hardest negatives are also the most likely to be false negatives: examples that are mined as negatives but actually share the anchor's class or relevance. The problem has been documented in unsupervised contrastive learning, where it is called sampling bias (not to be confused with sampling bias in statistics), and in dense retrieval, where one study estimated that a large fraction of top-retrieved but unlabelled passages were in fact relevant, so that mining them as negatives without denoising degraded retrieval quality. Proposed remedies include debiasing corrections, cross-encoder denoising, uncertainty- and diversity-aware selection, and, in multi-modal settings, using a third modality to filter likely false negatives before reweighting.
mine_hard_negatives() function used to build hard-negative training sets for dense retrieval and semantic similarity models.