Abstract
In deep learning, the softmax function is the standard method for converting raw logits into probability distributions. However, its dense nature—assigning non-zero values to every output—introduces noise and necessitates manual thresholding in multi-label tasks. This paper presents r-softmax, a novel probability mapping function that allows for controllable sparsity. By utilizing a sparsity rate parameter $r$, r-softmax can assign zero probability to irrelevant classes or tokens. Our evaluation across multi-label classification and NLP benchmarks shows that r-softmax improves model performance and interpretability compared to standard softmax and existing sparse alternatives.
1. Introduction
The softmax function is ubiquitous in neural network architectures, particularly in the final layers of classifiers and within the attention mechanisms of Transformers. Despite its success, softmax is inherently "dense," meaning it distributes probability mass across all possible outcomes. In scenarios where only a subset of labels is relevant, this density forces the model to account for noise, complicating decision-making and reducing the clarity of attention weights.
To address this, researchers have proposed sparse alternatives, but these often lack an intuitive mechanism for controlling the degree of sparsity or fail to match the performance of standard softmax. This paper introduces r-softmax, which provides a principled and controllable way to induce sparsity in neural network outputs.
2. The Problem: The Cost of Density
The density of softmax creates two primary issues in modern deep learning:
- Multi-label Classification: In tasks where multiple objects may be present (e.g., an image containing both a dog and a car), softmax still assigns tiny probabilities to thousands of irrelevant classes. Practitioners must manually tune thresholds to decide which labels are "active," a process that is often suboptimal and dataset-specific.
- Attention Mechanisms: In Transformers, softmax-based attention forces every token to attend to every other token. This inclusion of irrelevant or "distracting" tokens can degrade the signal-to-noise ratio, potentially harming the model's ability to focus on critical contextual information.
3. Methodology
The authors propose a transition from standard softmax to a sparse version through two intermediate steps.
3.1 t-softmax: The Learnable Threshold
The foundation of the approach is t-softmax, a weighted version of the softmax function. The weights are determined by a threshold $t$ relative to the maximum logit: $$w_i^t = \text{ReLU}(x_i + t - \max(x))$$ In this formulation, if the difference between a logit $x_i$ and the maximum logit $\max(x)$ exceeds the threshold $t$, the weight becomes zero. This allows for a smooth, differentiable transition: as $t \to \infty$, the function approaches standard softmax; as $t \to 0$, it converges to a one-hot vector.
3.2 r-softmax: Controllable Sparsity
While $t$ is mathematically useful, it is not intuitive for users. The authors introduce r-softmax, where the user specifies a desired sparsity rate $r \in [0, 1]$. For instance, $r=0.8$ indicates that 80% of the output values should be zero. The model then employs a quantile function to calculate the specific threshold $t$ required to achieve that exact sparsity level.
3.3 Optimization and Loss Function
Standard cross-entropy loss relies on the logarithm of probabilities. Since r-softmax can produce exact zeros, $\log(0)$ would lead to undefined gradients. To circumvent this, the authors developed a specialized loss function combining Mean Squared Error (MSE) for positive labels and a hinge-loss component. This hinge loss effectively pushes the logits of irrelevant labels far enough away from the maximum logit to ensure they are zeroed out by the r-softmax function.
4. Experimental Evaluation
The effectiveness of r-softmax was tested across three domains:
- Synthetic Data: Used to validate the function's behavior across varying label densities and document lengths.
- Multi-label Computer Vision: ResNet models were evaluated on the VOC 2007 and MS COCO datasets.
- Natural Language Processing: BERT was fine-tuned on the GLUE benchmark (MRPC, RTE, SST-2, QNLI, and QQP) with r-softmax replacing standard softmax in the self-attention layers.
5. Key Results and Insights
5.1 Superiority in Multi-label Tasks
On the MS COCO dataset, r-softmax achieved an F1 score of 72.56, significantly outperforming other sparse variants such as sparsemax (65.67) and sparsehourglass (64.85). Unlike standard softmax, which requires extensive post-hoc threshold tuning, r-softmax naturally identifies the relevant labels.
5.2 Performance Gains in Transformers
Replacing softmax with r-softmax in BERT's attention mechanism led to consistent improvements across the GLUE benchmark. Notably, on the RTE (Recognizing Textual Entailment) task, accuracy increased from 68.95% to 71.84%. These results suggest that filtering out noise in the attention matrix allows the model to capture more relevant dependencies.
5.3 Optimal Sparsity Levels
The research indicates that "a little sparsity goes a long way." In NLP tasks, the highest performance was typically observed at low sparsity rates ($r \approx 0.1$ to $0.2$). Excessive sparsity can lead to the loss of necessary context, suggesting that r-softmax's controllability is vital for balancing noise reduction with information retention.
6. Conclusion and Future Work
r-softmax offers a robust and interpretable alternative to the standard softmax function, providing users with direct control over output sparsity. While it demonstrates clear performance benefits in multi-label classification and attention-based models, it introduces a computational overhead due to the requirement of sorting logits for the quantile calculation. Future research may explore optimized implementations or the application of r-softmax to large-scale generative models like GPT-style architectures, where efficient sparsity could significantly impact inference performance.
Summary updated