Abstract
Softmax classifiers with a very large number of classes naturally occur in many applications such as natural language processing and information retrieval. The calculation of full softmax is costly from the computational and energy perspective. There have been various sampling approaches to overcome this challenge, popularly known as negative sampling (NS). Ideally, NS should sample negative classes from a distribution that is dependent on the input data, the current parameters, and the correct positive class. Unfortunately, due to the dynamically updated parameters and data samples, there is no sampling scheme that is provably adaptive and samples the negative classes efficiently. Therefore, alternative heuristics like random sampling, static frequency-based sampling, or learning-based biased sampling, which primarily trade either the sampling cost or the adaptivity of samples per iteration are adopted. In this paper, we show two classes of distributions where the sampling scheme is truly adaptive and provably generates negative samples in near-constant time. Our implementation in C++ on CPU is significantly superior, both in terms of wall-clock time and accuracy, compared to the most optimized TensorFlow implementations of other popular negative sampling approaches on powerful NVIDIA V100 GPU.
Introduction
In "Extreme Classification" tasks—such as predicting one product out of millions—the final Softmax layer of a neural network becomes a massive computational bottleneck. Calculating probabilities for every single possible class to update the model is prohibitively expensive. To mitigate this, researchers use Negative Sampling (NS), selecting a small subset of incorrect classes. However, standard NS methods often rely on static distributions (e.g., global word frequency), which tend to pick "easy" negatives that provide little learning signal. Effective learning requires "hard" negatives—classes similar to the target but incorrect. Traditionally, finding these hard negatives requires $O(N)$ computation, negating the benefits of sampling.
Locality Sensitive Negative Sampling (LNS)
We propose Locality Sensitive Negative Sampling (LNS), which utilizes Locality Sensitive Hashing (LSH) to identify hard negatives in near-constant time. LSH hashes similar items into the same buckets with high probability, allowing for efficient retrieval of relevant candidates.
LNS introduces two primary schemes:
- LSH Label: Queries the hash table using the weight vector of the true class to find similar class weights.
- LSH Embedding: Queries the hash table using the input embedding, identifying classes the model currently confuses with the input.
Unlike traditional Top-K search which requires expensive sorting, LNS samples directly from hash buckets. This allows the sampling distribution to adapt dynamically as model weights evolve, maintaining high-quality "hard" negatives throughout training in $O(1)$ time.
Experimental Evaluation
The method was evaluated on several large-scale datasets: Amazon-670K (670,000 labels), Wiki-325K (325,000 labels), and ODP-105K (105,000 labels). Additionally, a custom "Amazon-Uniform" dataset was used to test performance in the absence of power-law label distributions.
Results and Discussion
LNS demonstrates significant improvements in both speed and accuracy. On the Amazon-670K dataset, LNS achieved a 10.3x to 11x speedup per epoch compared to Full Softmax on a GPU.
Crucially, LNS maintains accuracy levels comparable to the "gold standard" Full Softmax, outperforming other sampling heuristics like Sampled Softmax or Noise Contrastive Estimation (NCE). A notable finding is the "Hardware Disadvantage" Paradox: our C++ CPU implementation outperformed a V100 GPU running TensorFlow because the algorithmic efficiency of sparse LSH lookups outweighed the raw throughput of dense matrix multiplications on the GPU.
Conclusion
LNS provides a truly adaptive, near-constant time negative sampling scheme that bridges the gap between computational efficiency and model accuracy in extreme classification. While implementation complexity and memory overhead remain considerations, the ability to perform adaptive sampling in constant time represents a significant step forward for large-scale machine learning.
Summary updated