Bayesian Attention Modules

Xinjie Fan, Shujian Zhang, Bo Chen, Mingyuan Zhou

Attention modules have become the backbone of modern deep learning, enabling state-of-the-art results across natural language processing, computer vision, and graph learning. However, the vast majority of these models rely on deterministic attention mechanisms—typically calculated via a standard softmax function. While simple to optimize, deterministic attention limits a model's ability to capture complex dependencies and provide reliable uncertainty estimates.

In this paper, the authors introduce Bayesian Attention Modules (BAM), a scalable framework that transforms standard deterministic attention into a stochastic, Bayesian component. By treating attention weights as latent random variables, BAM injects principled noise into the learning process, leading to improved performance, enhanced robustness, and better uncertainty quantification across a wide variety of tasks.

The Problem with Deterministic Attention

Most modern neural networks, from Transformers to Graph Attention Networks (GATs), use deterministic attention to aggregate features. Relying on a single deterministic weight vector can lead to overconfidence and poor generalization.

While "stochastic" or "hard" attention mechanisms exist (e.g., discretely sampling a specific region to focus on), they are typically non-differentiable. Training such models requires high-variance gradient estimators like REINFORCE, making them notoriously unstable and difficult to optimize. BAM bridges this gap by offering a "soft," stochastic attention mechanism that remains fully differentiable, easy to implement, and stable to train.

The BAM Framework

The core innovation of BAM lies in treating attention weights as data-dependent local latent variables. The framework is built on four key pillars:

  1. Reparameterizable Distributions: Instead of computing a deterministic score and applying softmax, BAM samples unnormalized weights from continuous distributions—specifically, Weibull or Lognormal distributions. These are chosen because they generate non-negative values and support the "reparameterization trick," allowing gradients to flow smoothly through the sampling process during backpropagation.
  2. Simplex Constraint: To ensure the sampled weights behave like traditional attention (i.e., summing to 1), the samples are normalized across the key dimension.
  3. Contextual Prior: BAM introduces a "key-based contextual prior." This is a lightweight secondary network that evaluates the input keys to suggest a global importance distribution before the specific query is even considered.
  4. Variational Inference: The model is trained by maximizing the Evidence Lower Bound (ELBO). To maintain training efficiency, the authors derive a semi-analytic Kullback-Leibler (KL) divergence term, which drastically reduces the variance of gradient estimates compared to standard Monte Carlo sampling.

Key Insights and Advantages

Treating attention as a probabilistic distribution yields several distinct advantages:

  • Stochasticity as Regularization: The inherent "noise" introduced during training prevents the model from over-relying on specific features. This acts as a powerful regularizer, leading to better generalization on unseen data.
  • The Power of the Prior: The contextual prior allows the model to learn an inherent hierarchy of importance in the data.
Attention maps generated by BAM during image captioning, showing the contextual prior and word-specific attention.
As seen in the image captioning example above, the prior learns that the man and the bicycle are generally important regions to focus on. The specific query then refines this attention as the model generates each word (e.g., shifting focus when generating "riding" or "street").
  • Robustness to Noise: BAM demonstrates significant resilience to input noise. In Visual Question Answering (VQA) tasks where Gaussian noise was added to image features, BAM's performance degraded much less than standard deterministic attention.
  • Uncertainty Quantification: Because BAM is probabilistic, it can be sampled multiple times during inference. High variance across these samples indicates model uncertainty—a critical feature for AI safety, interpretability, and deployment in high-stakes environments.

Empirical Performance

The authors evaluated BAM across an impressively broad range of benchmarks, demonstrating consistent improvements over strong baselines:

  • Graph Networks: BAM-WC (Weibull with Contextual Prior) outperformed standard Graph Attention Networks (GAT) on node classification datasets, increasing accuracy on Pubmed from 77.26% to 78.82%.
  • Visual Question Answering (VQA): On noisy VQA-v2 data, BAM achieved 62.89% accuracy compared to the baseline's 61.25%, while simultaneously providing superior uncertainty metrics.
  • Image Captioning: On MS-COCO, BAM-WC achieved a CIDEr score of 104.5, outperforming both standard soft attention (104.0) and traditional hard attention (84.4).
  • Language Models: By replacing deterministic self-attention in ALBERT-base with BAM during fine-tuning, the model saw consistent gains across the GLUE benchmark (e.g., MRPC accuracy rose from 86.5 to 88.5) and SQuAD datasets.

Limitations and Future Directions

While highly effective, BAM does introduce some computational overhead. Sampling and calculating KL divergence terms add minor memory and compute costs compared to vanilla softmax, though the authors' complexity analysis shows this overhead is generally inconsiderable.

Future work offers exciting possibilities. In the NLP experiments, BAM was only applied during the fine-tuning stage of ALBERT; applying it during the massive pre-training phase of large language models could yield even more significant benefits. Additionally, combining BAM with Reinforcement Learning (RL)-based fine-tuning—a common practice in tasks like image captioning—remains a promising avenue for future research.

Summary updated

BibTeX
@inproceedings{fan2020bayesian,
  author = {Xinjie Fan and Shujian Zhang and Bo Chen and Mingyuan Zhou},
  title = {{Bayesian Attention Modules}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2020},
  url = {https://openreview.net/forum?id=w2vSTUuj9_0},
}
Figure