Bayesian Attention Belief Networks

Shujian Zhang, Xinjie Fan, Bo Chen, Mingyuan Zhou

Introduction

Since the rise of the Transformer, attention mechanisms have become the "gold standard" for processing sequences in deep learning. However, most standard implementations are deterministic: they use fixed mappings to calculate attention weights. While computationally efficient, this approach has two major drawbacks:

  1. Limited Expressiveness: Deterministic mappings can struggle to capture the complex, multi-modal dependencies inherent in real-world data.
  2. Lack of Uncertainty Estimation: These models provide a single "best guess" without any measure of confidence. This is a critical flaw for high-stakes applications like medical diagnosis or autonomous systems where knowing when a model is uncertain is just as important as the prediction itself.

While stochastic (randomized) attention models exist, they are often notoriously difficult to optimize or require complex, custom architectures that cannot easily leverage the wealth of existing pretrained models like BERT.

To address this, researchers have introduced Bayesian Attention Belief Networks (BABN). BABN replaces standard deterministic attention with a deep stochastic framework using a hierarchy of Gamma and Weibull distributions. This "plug-and-play" upgrade improves model accuracy, provides reliable uncertainty estimation, and significantly enhances robustness against adversarial attacks and domain shifts.

The BABN Architecture

The core innovation of BABN is treating unnormalized attention weights as latent random variables rather than fixed computed values.

  • The Generative Model (Decoder): The framework models unnormalized weights using a hierarchy of Gamma distributions. The Gamma distribution is ideal here because it naturally handles positive values, sparsity, and skewness.
  • The Inference Model (Encoder): To approximate the posterior distribution, the authors use a stack of Weibull distributions. This choice is clever because the Weibull distribution is "reparameterizable" (allowing for standard backpropagation) and has an analytic Kullback–Leibler (KL) divergence with the Gamma distribution, making the training objective (the Evidence Lower Bound, or ELBO) easy to compute.
The probabilistic graphical model of BABN, showing the generative model (Gamma), Gibbs sampling, and the inference network (Weibull).

Inspired by Gibbs sampling, the encoder uses a "deterministic-upward" path to extract features and a "stochastic-downward" path to sample attention weights.

Seamless Integration

Crucially, BABN is designed to be a drop-in replacement for existing architectures. It uses the existing keys ($K$) and queries ($Q$) of a standard attention layer to parameterize its Gamma and Weibull distributions. This means practitioners can take a pretrained model (like ALBERT or RoBERTa) and "upgrade" it to a BABN during the finetuning stage without starting from scratch.

Comparison between a standard deterministic attention layer and a Bayesian attention belief network layer.

Key Insights and Results

The authors evaluated BABN across a massive array of benchmarks, including General Language Understanding (GLUE, SQuAD), Domain Generalization (SNLI/MNLI, QQP/TwitterPPDB), Neural Machine Translation (IWSLT), and Multi-modal Learning (VQA-v2).

1. Performance Boost and Calibration

BABN consistently outperformed deterministic baselines and state-of-the-art stochastic models (like BAM) on GLUE and SQuAD. For example, it improved ALBERT-base accuracy on the RTE task from 75.8% to 77.6%. Furthermore, BABN achieved a lower Expected Calibration Error (ECE), meaning its predicted probabilities are much closer to the actual likelihood of being correct.

2. Stochasticity as a Defense

One of the most striking findings is that stochasticity acts as a natural defense against adversarial attacks. Because the attention weights are sampled from a distribution rather than being fixed, it is much harder for an attacker to find a specific "perturbation" (like changing a single word) that consistently fools the model. In adversarial testing using tools like Textfooler, BABN showed a significantly higher failure rate for the attacker.

3. "Knowing What You Don't Know"

By visualizing the posterior distribution, the authors demonstrated that BABN learns sample-dependent uncertainty. If an input is ambiguous or noisy (such as in the VQA-v2 dataset injected with Gaussian noise), the model's internal variance increases, providing a reliable signal that the prediction might be untrustworthy.

Limitations and Future Work

While highly effective, BABN does introduce some computational overhead. The extra linear layers and sampling steps result in a small increase in parameter count (~12.4M vs 11.7M for ALBERT-base) and a slightly slower training step (0.41s vs 0.26s). Additionally, the model relies on balancing weights for the upward and downward paths, which may require hyperparameter tuning for entirely new architectures.

Currently, BABN has primarily been applied during the finetuning stage. A promising avenue for future work is incorporating this Bayesian framework into the massive initial pretraining phase of models like GPT or BERT, which could yield even more powerful and robust probabilistic representations.

Summary updated

BibTeX
@inproceedings{zhang2021bayesian,
  author = {Shujian Zhang and Xinjie Fan and Bo Chen and Mingyuan Zhou},
  title = {{Bayesian Attention Belief Networks}},
  booktitle = {International Conference on Machine Learning},
  year = {2021},
  url = {https://openreview.net/forum?id=efgYSVFstkn},
}
Figure