Attention as a Latent Variable: Modeling a Distribution over Attention
Latent Alignment and Variational Attention, by Yuntian Deng, Yoon Kim, Justin Chiu, Demi Guo, and Alexander M. Rush, develops attention as an explicit latent variable with variational inference. First released on July 10, 2018 and published at NeurIPS 2018, it models a distribution over where a network attends and integrates predictions over that distribution. Categorical and Dirichlet alignments make this a general approach to modeling attention uncertainty.
Change the probability of the latent state to compare f(E[z]) with E[f(z)] for a sigmoid predictor.
Two possible latent values: z = 0 and z = 4. The nonlinear prediction is f(z) = sigmoid(z).
P(z = 0)
50.0%
P(z = 4)
50.0%
Average, then predictf(E[z]) =
Predict, then marginalizeE[f(z)] =
Difference: 0.140
Normalized attention weights and a probabilistic model
An attention vector contains nonnegative weights that sum to one. That numerical property does not specify how uncertainty enters the prediction. A deterministic mechanism constructs one context vector from those weights and applies the downstream network once. A latent-variable mechanism defines alternative attention states, predicts under them, and integrates their contributions.
Write the attention state as z and the downstream computation as f. In general, f(E[z]) ≠ E[f(z)]. For a simple illustration, let z be 0 or 2 with equal probability and let f(z) = z². Applying f to the mean gives 1; averaging the two outputs gives 2. Neural prediction contains nonlinear operations, so moving the expectation across the network changes the model. This is the distinction between an average representation and an average over predictions.
Let x include the observed input and the query, y be the output, and z be a categorical attention state. The latent model predicts p(y | x) = Σ_z p(z | x) p(y | x, z). Each attention state supplies an output distribution; its attention probability weights that distribution. A continuous attention state uses an integral in place of the sum.
During training, the observed answer y lets an inference network learn q(z | x, y), a posterior approximation used by the variational objective. At prediction time, the model combines outputs using p(z | x). For translation, the training inference network sees the source and target sentence; generation uses the source and the target prefix already produced.
The 2018 paper makes alignment an explicit variable in the generative model. It studies categorical alignments, which select one source element, and Dirichlet alignments, which distribute attention across a mixture of elements. Amortized variational inference learns a posterior conditioned on observed inputs and outputs. Variance-reduction methods make latent inference practical, with experiments in machine translation and visual question answering.
Prior attention asks where to attend before the answer is known. Posterior attention asks which alignment explains an observed answer. These are different conditional distributions.
Imagine an image containing two people and a question asking who is holding a cup. Before seeing the answer, the model assigns attention probability to both people. Once the answer is observed, posterior inference redistributes probability toward alignments that explain it.
The distinction also matters when attention is composed with another probabilistic component. Explicit variables expose which quantities are conditioned on, which alternatives are integrated out, and what an approximation replaces. A sampled attention vector, its expectation, and its most probable value answer different questions; naming the distribution makes those choices inspectable.
Show, Attend and Tell (ICML 2015) studies deterministic soft attention and sampled hard attention for image captioning. Its stochastic formulation uses a variational lower bound and a sampling-based learning rule. It provides an early reference for probabilistic selection; Latent Alignment and Variational Attention develops learned posterior inference for alignment and tighter variational training.
Structured Attention Networks (ICLR 2017) defines distributions over structured selections, including subsequences and syntactic trees. Differentiable graphical-model inference supplies expected annotations to the surrounding network. This enriches the structure of attention. Computing an expected annotation remains a separate operation from marginalizing the final nonlinear prediction over every attention structure.
Variational Attention for Sequence-to-Sequence Models (COLING 2018) models the context vector with Gaussian random variables in a variational encoder-decoder. Its purpose is to prevent deterministic attention from bypassing the model’s latent space. The latent object is the context vector, distinct from the categorical or simplex-valued alignment variable in Latent Alignment and Variational Attention.
Bayesian Attention Modules (NeurIPS 2020) constructs stochastic attention weights by normalizing Weibull or lognormal variables. Reparameterized training and data-dependent priors make the modules usable across attention architectures. This develops the broad distribution-over-attention perspective with a different distribution family and training construction.
Four questions for a probabilistic attention model
Start with the random object: a selected position, a distribution over positions, a context vector, or an entire structured selection. Next specify the prior and the information available to the posterior. Then identify what the prediction integrates: an intermediate representation or the output likelihood. Finally state whether training and prediction enumerate states, sample them, use an expectation, or optimize a variational approximation.
Latent Alignment and Variational Attention uses categorical and simplex-valued alignments; Variational Attention for Sequence-to-Sequence Models uses Gaussian context vectors. The latent quantity and inference problem distinguish these two models.
Method
Random or structured object
Role of inference
Latent Alignment and Variational Attention
Categorical or Dirichlet alignment
Learn posterior alignment and approximate marginal likelihood
Structured Attention Networks
Subsequence or tree selection
Compute expected annotations
Variational Attention for Seq2Seq
Gaussian context vector
Preserve a stochastic path through the encoder-decoder
Download the self-contained attention notebook or Python script. It compares averaging source features before a nonlinear predictor with averaging output probabilities over categorical attention states.
The example computes the posterior for an observed label and optimizes a categorical variational distribution. The difference between exact log likelihood and the ELBO equals KL divergence to the exact posterior. It uses fixed generative parameters for one observation; the paper learns an amortized inference network across examples and also studies Dirichlet attention.
Chinese media coverage · 机器之心 · 2018-07-26.Variational attention and latent alignment (republished by the outlet on Tencent Cloud on 2018-07-30). Explains attention as a latent variable, the source-and-target-conditioned posterior, and a German–English alignment example.