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 (NeurIPS 2018), by Yuntian Deng, Yoon Kim, Justin Chiu, Demi Guo, and Alexander M. Rush, develops attention as an explicit latent variable with variational inference. The general idea is to model a distribution over where a network attends and reason over that distribution, rather than pass one deterministic weighted average into the prediction network.

Original research

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

Work through an example

An average is not a marginal

A numerical example of nonlinear marginalization; this is not a trained attention model.

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

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. The paper’s inference network uses target information during training; this is distinct from supplying future target words to a left-to-right predictor at generation time.

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. This example defines an inference question independently of the visualization used to display the weights.

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.

These choices determine the meaning of the model. They also make comparisons useful: two systems both called “variational attention” need not represent the same latent quantity or solve the same inference problem.

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

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.