Research notesLanguage models & structured learning
← All guides

Probabilistic attention and latent inference

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.

Original research

Latent Alignment and Variational Attention
Yuntian Deng, Yoon Kim, Justin Chiu, Demi Guo, Alexander M. Rush · NeurIPS 2018
Record and citation

Work through an example

An average is not a marginal

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]) = 0.881
Predict, then marginalizeE[f(z)] = 0.741

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.

Sources: Latent Alignment and Variational AttentionStructured Attention Networks

Marginalizing attention at the output

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.

Sources: Latent Alignment and Variational Attention

Latent alignment and variational attention

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.

Sources: Latent Alignment and Variational Attention

What a posterior over attention tells you

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.

Sources: Latent Alignment and Variational Attention

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.

MethodRandom or structured objectRole of inference
Latent Alignment and Variational AttentionCategorical or Dirichlet alignmentLearn posterior alignment and approximate marginal likelihood
Structured Attention NetworksSubsequence or tree selectionCompute expected annotations
Variational Attention for Seq2SeqGaussian context vectorPreserve a stochastic path through the encoder-decoder
Bayesian Attention ModulesDistribution over normalized attention weightsReparameterized variational training

Sources: Latent Alignment and Variational AttentionStructured Attention NetworksVariational Attention for Sequence-to-Sequence ModelsBayesian Attention Modules

Run exact and variational attention

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.

The code runs with Python 3.9+ and its standard library, without model weights, API keys or GPU resources. Download all three examples and numerical checks.

Sources: Latent Alignment and Variational Attention

Coverage and discussion

  • 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.

Papers & implementations

NeurIPS 2018

Latent Alignment and Variational Attention

Yuntian Deng, Yoon Kim, Justin Chiu, Demi Guo, Alexander M. Rush

Models alignment as a latent variable and learns approximate posterior inference through variational attention.

A general framework for distributions over attention, beyond a deterministic attention vector.

ICLR 2017

Structured Attention Networks

Yoon Kim, Carl Denton, Luong Hoang, Alexander M. Rush

Uses differentiable graphical-model inference to attend over subsequences and syntactic trees.

Models structured selections and computes expected annotations; full prediction marginalization is a separate operation.

NeurIPS 2020

Bayesian Attention Modules

Xinjie Fan, Shujian Zhang, Bo Chen, Mingyuan Zhou

Builds distributions over attention weights from normalized reparameterizable variables and learns them with Bayesian regularization.

Extends probabilistic attention to modular stochastic weights with data-dependent priors.