A Tale of Two Efficient and Informative Negative Sampling Distributions

Shabnam Daghaghi, Tharun Medini, Nicholas Meisburger, Beidi Chen, Mengnan Zhao, Anshumali Shrivastava

TL;DR

Researchers have developed a way to train massive neural networks—those with millions of output classes—using a "near-constant time" sampling method. By leveraging Locality Sensitive Hashing (LSH), their approach adaptively identifies the most informative "hard" negative samples. Remarkably, this allows a C++ implementation running on a standard CPU to outperform highly optimized, GPU-based TensorFlow baselines by over 10x.

The Softmax Bottleneck

In "Extreme Classification" tasks, such as predicting the next word in a massive vocabulary or recommending one product out of millions, the final "softmax" layer of a neural network becomes a severe computational bottleneck. Calculating the probability for every single possible class (the "full softmax") requires $O(N)$ operations, where $N$ is the total number of classes.

To accelerate this process, practitioners rely on Negative Sampling (NS). Instead of evaluating all $N$ classes, the model updates weights for the "true" class and a small subset of "negative" (incorrect) classes. However, current NS methods face a difficult trade-off:

  1. Static Sampling (Uniform/Frequency-based): These methods are fast but tend to pick "easy" negatives (e.g., suggesting a "baby toy" as a negative for "running shoes"). This fails to help the model learn fine-grained, discriminative differences.
  2. Learned/Adaptive Sampling: These methods attempt to find "hard" negatives (e.g., distinguishing "Adidas running shoes" from "Nike running shoes"). Unfortunately, the search for these hard negatives is often so computationally expensive that it negates the speed benefits of sampling in the first place.

Locality Sensitive Negative Sampling (LNS)

To resolve this trade-off, the authors propose Locality Sensitive Negative Sampling (LNS). They utilize Locality Sensitive Hashing (LSH)—an algorithmic technique that places similar vectors into the same "buckets" with high probability—to find informative negatives in $O(1)$ (constant) time.

The paper introduces two specific LSH-based sampling schemes:

  • LSH Label: The system queries the hash tables using the weight vector of the true label. This retrieves other classes whose weights are currently similar to the correct one, providing excellent hard negatives based on the model's current state.
  • LSH Embedding: The system queries the hash tables using the input embedding (the activations from the penultimate layer). This identifies classes that the model is currently "confused" by (i.e., classes yielding high activation despite being incorrect).
Architecture of Locality Sensitive Negative Sampling showing LSH Label and LSH Embedding queries.

The LNS Workflow: 1. Preprocessing: Hash all class weight vectors into LSH tables. 2. Sampling: For a given input, hash the query (either the true label vector or the input embedding) and retrieve candidate negatives from the resulting buckets. 3. Update: As the model learns and its weights change, the LSH tables are periodically updated in the background to maintain accurate similarity buckets.

Key Insights

  • Search vs. Sampling: LSH is traditionally used for exact nearest-neighbor search, which can become expensive if you strictly need the top-K results. The authors realized that for negative sampling, you don't need the absolute best negatives; you just need good, informative ones. By treating LSH as a sampler rather than a strict search engine, they achieved near-constant time complexity.
  • True Adaptivity: Unlike static methods, LNS naturally changes its sampling distribution as the model trains. As the weight vectors for two similar classes move closer together in the embedding space, LSH naturally starts picking one as a hard negative for the other.
  • Robustness Beyond Power Laws: Most common NS methods (like those used in Word2Vec) rely heavily on the assumption that class frequencies follow a power law. The authors demonstrated that when this power law is absent, standard methods fail drastically, whereas LNS remains robust and highly accurate.

Empirical Results

The authors evaluated LNS on several large-scale datasets from the Extreme Classification Repository, including Amazon-670K (product recommendation), Wiki-325K (document classification), and ODP-105K.

Convergence plot on the Wiki-325K dataset demonstrating LSH methods rapidly approaching Full Softmax accuracy.
  • Unprecedented Speed: On the Amazon-670K dataset, LNS was 10.3x to 11x faster per epoch than TensorFlow's Full Softmax running on a powerful NVIDIA V100 GPU.
  • Hardware Efficiency: In a striking demonstration of algorithmic efficiency, the authors' C++ CPU implementation of LNS outperformed GPU-based TensorFlow implementations of Sampled Softmax and Noise Contrastive Estimation (NCE) in both wall-clock time and final accuracy.
  • Rapid Convergence: LNS reached 90% of the "Full Softmax" accuracy significantly faster than any other baseline. On the ODP dataset, it achieved a 14x to 15x speedup while preserving or even slightly improving overall accuracy.

Limitations and Future Directions

While highly effective, the current LNS implementation is built in C++ as part of the SLIDE framework. Integrating this into standard Python-heavy workflows (like PyTorch or standard TensorFlow) requires custom kernels, which may present a barrier to immediate adoption for some practitioners. Additionally, maintaining LSH hash tables for millions of classes introduces a memory overhead, though the authors note this is generally manageable compared to the massive weight matrices already required for extreme classification.

Looking forward, the authors suggest exploring how this adaptive, constant-time sampling could be applied to other deep learning bottlenecks, such as large-scale attention mechanisms or generative modeling with massive vocabularies.

Summary updated

BibTeX
@inproceedings{daghaghi2021tale,
  author = {Shabnam Daghaghi and Tharun Medini and Nicholas Meisburger and Beidi Chen and Mengnan Zhao and Anshumali Shrivastava},
  title = {{A Tale of Two Efficient and Informative Negative Sampling Distributions}},
  booktitle = {International Conference on Machine Learning},
  year = {2021},
  url = {https://openreview.net/forum?id=XJgxWzd0Y9},
}
Figure