Source-linked AI summary

Contrastive Learning with Hard Negative Samples

Joshua Robinson, Ching-Yao Chuang, Suvrit Sra, Stefanie Jegelka

arXiv:2010.04592v2cs.LGstat.ML

TL;DR

Unsupervised contrastive learning needs informative hard negatives but lacks the true similarity information used by supervised negative mining. The paper introduces a tunable hard-negative sampling distribution with an efficient implementation, and reports improved downstream performance across modalities alongside favorable theoretical representations. Its analysis assumes a discrete latent-class structure and an unknown class prior that must be treated as a hyperparameter or estimated.

  • Problem

    Unsupervised contrastive learning commonly samples negatives from the marginal distribution, while informative hard-negative selection requires similarity information unavailable without supervision.

  • Method

    The method uses a tunable distribution that favors currently similar negatives, combines positive-unlabeled learning with importance sampling, and incurs no computational overhead.

  • Results

    The method improves downstream performance on image, graph, and text data, while its optimal representations cluster similar inputs and separate different classes.

  • Takeaways & Limitations

    Hard-negative sampling provides a simple unsupervised bridge between contrastive learning and metric-learning negative mining, with user-controlled hardness.

  • Takeaways & Limitations

    The setup assumes discrete latent classes and an unknown class prior that must be treated as a hyperparameter or estimated.

Abstract

from arXiv · show

How can you sample good negative examples for contrastive learning? We argue that, as with metric learning, contrastive learning of representations benefits from hard negative samples (i.e., points that are difficult to distinguish from an anchor point). The key challenge toward using hard negatives is that contrastive methods must remain unsupervised, making it infeasible to adopt existing negative sampling strategies that use true similarity information. In response, we develop a new family of unsupervised sampling methods for selecting hard negative samples where the user can control the hardness. A limiting case of this sampling results in a representation that tightly clusters each class, and pushes different classes as far apart as possible. The proposed method improves downstream performance across multiple modalities, requires only few additional lines of code to implement, and introduces no computational overhead.

1 INTRODUCTION

Contrastive learning depends on informative positive and negative pairs, yet unsupervised methods commonly sample negatives uniformly without similarity information. The paper proposes tunable unsupervised hard-negative sampling and reports theoretical and empirical benefits across modalities.

  • Uniform negative sampling often selects points unlike the anchor and can even include examples from the same class.Informative negatives are instead points that are close in the learned representation but should be far apart.
  • The method constructs a tunable distribution favoring negatives whose representations are currently similar to the anchor.It uses positive-unlabeled learning to address missing similarity labels and importance sampling for efficient implementation.
  • The proposed importance-sampling strategy requires only a few additional code lines and introduces no computational overhead.
  • Theoretical analysis shows that optimal representations form tight clusters for similar inputs while separating different classes as far as possible.
  • Hard-negative sampling improves downstream performance on image, graph, and text data.

2 CONTRASTIVE LEARNING SETUP

The setup learns hyperspherical embeddings from same-class positives and sampled negatives, with the negative distribution treated as a central design choice. The paper asks whether choosing a distribution better than the marginal can improve contrastive learning.

  • The representation maps observations to points on a hypersphere with radius 1/t, where t is the temperature scaling hyperparameter.
  • The latent-class model treats positive pairs as inputs sharing a class and defines class-conditioned distributions for same-label and different-label points.
  • The NCE objective uses a same-label positive and negative examples sampled from a distribution q.
  • In common practice, q is the marginal distribution p or an empirical approximation, motivating the question of whether a better q exists.

3 HARD NEGATIVE SAMPLING

The hard-negative method favors different-class points that the current embedding places near the anchor, while tuning hardness to balance learning signal against false-negative damage. It implements this reweighting efficiently without changing the sampling pipeline.

  • Useful negatives should differ in class from the anchor while appearing similar under the current embedding.This combines approximate different-class selection with hard-negative selection.
  • Unsupervised learning cannot enforce different-class selection exactly, so the method approximates it while exposing a smoothly adjustable hardness level.
  • Greater hardness increases the learning signal but also raises the risk that approximate filtering leaves false negatives.
  • The negative distribution conditions on a different latent class and exponentially up-weights points with high inner product to the anchor.Because embeddings lie on a hypersphere, high inner product is equivalent to small squared Euclidean distance.
  • Positive-unlabeled learning rewrites the target distribution using the marginal and same-class positive distributions, which can be approximated from available samples and semantic-preserving transformations.
  • Importance sampling approximates the required expectations using samples from the marginal and positive distributions instead of explicitly sampling the hard-negative distribution.
  • The implementation reweights the objective with only two extra lines of code and no additional computational overhead.

4 ANALYSIS OF HARD NEGATIVE SAMPLING

The analysis characterizes hard-negative sampling as interpolating toward worst-case negatives and shows that optimal embeddings form tight within-class clusters with separated class centers. Experiments then report improved downstream accuracy across image, graph, and sentence representations, with performance depending on the hardness parameter.

  • 4.1 HARD SAMPLING INTERPOLATES BETWEEN MARGINAL AND WORST-CASE NEGATIVES: β controls negative hardness by increasing probability for negative points with high representation similarity to the anchor.The sampling distribution approaches a lower bound that becomes tight as β →∞.
  • 4.2 OPTIMAL EMBEDDINGS ON THE HYPERSPHERE FOR WORST-CASE NEGATIVE SAMPLES: In the worst-case limit, optimal embeddings are invariant across similar inputs and characterize one vector per class through a hypersphere ball-packing problem.For uniform class distributions, the class vectors maximize average squared distance to their nearest other class vectors.
  • 4.2 OPTIMAL EMBEDDINGS ON THE HYPERSPHERE FOR WORST-CASE NEGATIVE SAMPLES: The resulting class centers solve a maximum-margin clustering problem, with inter-class separation determined by the geometry of sphere packing.The minimum separation constant is a geometric property of spheres and corresponds to the Tammes ball-packing problem.
  • 5.1 IMAGE REPRESENTATIONS: 3% and 7.3% absolute accuracy gains over SimCLR occur on CIFAR100 and STL10, respectively, with additional gains over the best debiased baseline.The improvements over the best debiased baseline are 1.9% and 3.2% on CIFAR100 and STL10; tinyImageNet improves by 3.6% over SimCLR.
  • 5.2 GRAPH REPRESENTATIONS: Hard sampling matches InfoGraph on all graph tasks and outperforms it in 6 of 8 cases, including 3.2% and 2.4% gains on ENZYMES and REDDIT.Across all eight datasets, 17 of 24 tested β > 0 values achieve accuracy at least as high as InfoGraph.
  • 5.3 SENTENCE REPRESENTATIONS: Hard sampling equals or outperforms the quick-thoughts baseline on 5 of 6 sentence tasks and the debiased baseline on 4 of 6.Hard sampling with τ+ > 0 encountered numerical optimization issues.

6 A CLOSER LOOK AT HARD SAMPLING

Hard sampling is most effective when paired with debiasing, because extreme hardness can select false negatives; annealing β improves robustness to this choice. On STL10, the combined approach achieves the highest linear readout accuracy and reduces positive–negative similarity overlap.

  • Larger β does not necessarily improve vision representations because imperfect debiasing leaves same-label false negatives among the most similar negatives.
  • Annealing β toward 0 during training makes performance more robust to the choice of β.
  • Hard sampling with debiasing obtains the highest linear readout accuracy on STL10, while using either component alone gives similar accuracy and all improve over SimCLR.
  • Hard sampling with debiasing produces lower similarity for negative pairs and less overlap between positive- and negative-pair histograms than SimCLR.
  • Hard sampling with β = 1 reaches STL10’s SimCLR performance in 60 epochs, compared with 400 epochs for SimCLR.

7 CONCLUSION

The paper introduces unsupervised hard-negative sampling that connects contrastive learning with metric-learning negative mining without computational overhead. Its method improves downstream performance across image, graph, and text data.

  • The method provides a simple distribution over hard negative pairs and a practical importance-sampling strategy with zero computational overhead.
  • Theoretical analysis shows that the hard-negatives objective has desirable generalization properties.
  • The proposed sampling method improves downstream task performance on image, graph, and text data.

A ANALYSIS OF HARD SAMPLING

The analysis studies the concentration parameter’s limiting behavior and bounds the hard-negative sampling distribution as β becomes large.

  • As β approaches infinity, the hard-negative objective converges to its worst-case form for fixed measurable embeddings on the hypersphere.
  • The convergence argument uses pointwise decay of the bounded error term together with dominated convergence.
  • The proof bounds the probability of sampling negatives outside a near-maximum-similarity event using the positive mass of that event.

A.2 OPTIMAL EMBEDDINGS ON THE HYPERSPHERE FOR WORST-CASE NEGATIVE SAMPLES

For classification, optimal worst-case-negative embeddings are invariant across similar inputs and place class representations according to a hypersphere ball-packing problem.

  • Any global minimizer maps similar input pairs to the same representation almost surely.
  • The class-level representations are characterized as a solution to a ball-packing problem on the hypersphere.
  • The proof constructs embeddings that are constant on each class while preserving or lowering the uniformity loss.

A.3 DOWNSTREAM GENERALIZATION

The analysis establishes bounds connecting the learned embedding’s class separation to nearest-neighbor classification risk. It constructs class-specific hypersphere vectors and derives a misclassification bound from their separation.

  • The classifier’s misclassification risk is bounded using the minimum distance between distinct class vectors.
  • The derivation combines tail-probability bounds to obtain a high-probability nearest-neighbor classification guarantee.
  • The analysis represents each class by a vector on a scaled hypersphere and uses these vectors to define a nearest-neighbor classifier.
  • The proof converts inner-product bounds into Euclidean separation through the identity relating x⊤y to squared distance for equal-length vectors.

C ADDITIONAL EXPERIMENTS

Additional experiments test hard-negative sampling with MoCo-v2 and a large negative memory bank. The reported setup uses N = 65536 stored negatives.

  • Hard-negative sampling is evaluated with MoCo-v2 using a negative memory bank of N = 65536.Embeddings are trained for 200 epochs with batch size 128.

C.2 ABLATIONS

The ablations examine memory-bank scale, hardness, embedding similarity distributions, training speed, and implementation complexity. They show that hardness must be tuned rather than maximized indiscriminately.

  • Hard negatives remain useful with a very large MoCo-v2 negative memory bank containing N = 65536 representations.
  • β = 0.5 provides the best balance between positive and negative similarity distributions in the reported CIFAR100 ablation.Increasing β from 0 through 0.5 to 2 shifts both distributions left, while downstream performance depends on their relative separation.
  • The selected hard negatives have higher semantic similarity to their anchors than uniformly sampled negatives, including shared texture and object characteristics.
  • For STL10, β = 1 reaches the accuracy achieved by SimCLR in 400 epochs after 60 epochs, while CIFAR100 reaches it after 125 epochs.
  • The hard-sample objective requires only two additional lines of code compared with the standard objective.

D.1 VISUAL REPRESENTATIONS

The visual-representation experiments describe the training setup, hard-negative implementation, and practical stabilization choices. They also motivate annealing hardness and compare hard negatives with random negatives.

  • The method reduces β during training to use difficult negatives early, then lower hardness later to limit the effect of false negatives.
  • Hard negatives are selected by high inner product with the anchor in the learned latent space, producing semantically similar examples.
  • The hard-sample implementation adds only two lines of code to the standard contrastive objective.
  • When embeddings are not hypersphere-normalized, clipping inner products to [−2, 2] stabilizes optimization.
  • The graph experiments adapt InfoGraph by modifying its loss computation with hard-sampling reweighting.
Loading 2010.04592v2…