Sampled Softmax with Random Fourier Features

Ankit Singh Rawat, Jiecao Chen, Felix X. Yu, Ananda Theertha Suresh, Sanjiv KumararXiv 2019

Abstract

Training deep learning models with millions of output classes is computationally expensive due to the linear scaling of the softmax function. This paper introduces RF-softmax, a novel method utilizing Random Fourier Features to efficiently sample negative classes. RF-softmax provides a low-bias gradient estimate that scales logarithmically with the number of classes, significantly improving training speed without sacrificing accuracy.

Introduction

In multi-class classification tasks—such as predicting the next word in a sentence or recommending one item out of millions—the standard tool is the softmax function. However, softmax has a major flaw: to calculate the probability of a single class, you must sum over every possible class. When you have hundreds of thousands or millions of classes, this "partition function" becomes a massive computational bottleneck.

To speed up training, researchers often use Sampled Softmax, where they only consider the "true" class and a small random subset of "negative" classes. The challenge is that unless these negatives are sampled from the exact softmax distribution, the training becomes biased, leading to suboptimal models. However, sampling from the exact distribution is as expensive as calculating the full softmax. Most current approaches settle for simple uniform sampling, which is fast but highly inaccurate.

Methodology

The authors propose Random Fourier Softmax (RF-softmax), which bridges the gap between speed and accuracy using three key steps:

  1. Kernel Approximation: The authors observe that if input and class embeddings are normalized (a common practice in modern neural networks), the exponential kernel used in softmax can be mathematically rewritten as a Gaussian kernel.
  2. Random Fourier Features (RFF): They use RFF to "linearize" this Gaussian kernel. This transforms a complex, non-linear similarity measure into a simple dot product in a new, randomized feature space. As shown in the table below, this approximation is significantly more accurate than previous methods.
  3. Logarithmic Sampling: Once the kernel is linearized, the classes can be organized into a binary tree. This allows the model to sample a negative class in $O(D \log n)$ time, where $n$ is the number of classes and $D$ is the number of random features.
Comparison of approximation error (MSE) between RF-softmax and other kernel-based methods.

Crucially, the authors provide the first rigorous theoretical analysis of how the choice of sampling distribution affects the bias of the gradient, proving that RF-softmax maintains much lower bias than existing approximations.

Datasets and Evaluation

The authors evaluated their method on two types of tasks:

  • Language Modeling: Using the PennTreeBank (10k classes) and Bnews (64k classes) datasets.
  • Extreme Classification: Using three large-scale datasets from the Extreme Classification Repository: AmazonCat-13k, Delicious-200k, and WikiLSHTC (up to 325k classes).

Key Insights

  • The Bias-Variance Trade-off: The paper highlights a specific challenge: if the sampling temperature ($\nu$) is exactly equal to the model temperature ($\tau$), the approximation is unbiased but has high variance. They found that setting $\nu < \tau$ acts as a "sweet spot" that balances bias and variance for better empirical results.
  • Normalization as an Enabler: By requiring normalized embeddings, the authors turned a difficult non-shift-invariant problem (exponential kernel) into a well-studied shift-invariant one (Gaussian kernel), allowing them to use powerful tools from kernel theory.
  • Efficiency over Complexity: Previous kernel-based methods (like Quadratic-softmax) scaled with the square of the embedding dimension ($d^2$). RF-softmax scales with the number of random features ($D$), which can be significantly smaller than $d^2$ while providing a much tighter approximation of the true softmax.

Key Results

  • Speedup: In a test with 500,000 classes, RF-softmax was roughly 13x faster than exact sampling (2.4ms vs 32.3ms per batch).
  • Accuracy: On the Bnews dataset, RF-softmax with 8,192 random features achieved nearly the same validation perplexity as the full softmax, significantly outperforming uniform and quadratic sampling baselines.
  • Bias Reduction: Theoretical and empirical results confirmed that RF-softmax provides a much more accurate estimate of the full softmax gradient than previous sampling methods, leading to more stable and effective training.

Conclusion and Future Work

While RF-softmax offers significant improvements, it relies on $\ell_2$-normalized embeddings. While standard in many retrieval and recommendation architectures, it might require architectural changes for models that rely on unconstrained embedding magnitudes. Future directions include exploring structured orthogonal random features (SORF) to further reduce the computational complexity from $O(Dd)$ to $O(D \log d)$.

Summary updated

BibTeX
@misc{rawat2019sampled,
  author = {Ankit Singh Rawat and Jiecao Chen and Felix X. Yu and Ananda Theertha Suresh and Sanjiv Kumar},
  title = {{Sampled Softmax with Random Fourier Features}},
  year = {2019},
  eprint = {1907.10747},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1907.10747},
}
Figure