AI Glossary / Probabilistic machine learning / Intermediate
Hard attention
Definition
Hard attention is an attention mechanism that makes a discrete selection of input information, such as choosing one image region or a small set of positions to process. Unlike a soft weighted combination, the selected context comes from particular choices. Stochastic hard attention samples those choices from a learned distribution.
What it means
An attention model decides which information should influence its next computation. Soft attention commonly forms a weighted average of candidate representations. Hard attention instead selects an item or subset, making the decision itself discrete. The distinction concerns how information is selected, not whether the resulting prediction is difficult or confident.
The terminology covers multiple designs. This entry emphasizes the stochastic location-selection mechanism used in early neural visual-attention research. A deterministic top-choice mechanism is related but is not identical to sampling from attention probabilities. Neither should automatically be equated with every sparse attention pattern used in modern models.
How it works
A scoring network evaluates candidate locations or representations using the current model state. Scores become selection probabilities, often through softmax. A stochastic mechanism samples a location and supplies the selected representation to the next processing stage. Subsequent selections can depend on earlier state and observations.
The discrete sampling operation prevents straightforward pathwise differentiation through the choice. Training can instead use a score-function estimator such as REINFORCE, often with a baseline to reduce variance. Other architectures may use different estimators or relaxations, whose bias and behavior need separate consideration.
Selection does not automatically save computation. If the system already computed every candidate representation and every score, some of the expensive work has already happened. Savings depend on which operations the selection lets it avoid. At evaluation time, sampling, selecting the highest-probability location and combining several samples also produce different procedures.
Three practical examples
1. Image description: select a visual region when generating a word, then use that region's representation in the language decoder. The selected region is part of the computation, not proof that the generated word is correct.
2. Visual inspection: choose successive image patches for closer processing. Evaluate failures where the selection mechanism overlooks a small but important feature rather than measuring only successful inspections.
3. Sequential information access: select a limited set of candidate memory locations before an expensive downstream operation. Check whether the architecture actually avoids that operation for unselected items.
Think of it this way
Think of opening one folder from a set before continuing a task, instead of blending summaries from every folder. The analogy captures discrete access. A hard-attention model does not necessarily understand a folder's significance, and its learned selection distribution can be wrong or influenced by patterns that do not generalize.
Worked example
Suppose there are three candidate scalar representations: A=2, B=6 and C=10. Their selection probabilities are 0.5, 0.3 and 0.2. A stochastic hard-attention draw returns exactly one of the three values. For instance, a uniform draw of 0.65 falls in B's cumulative interval from 0.5 to 0.8 and therefore selects value 6.
Soft attention using the same probabilities would instead return 0.5 × 2 + 0.3 × 6 + 0.2 × 10 = 4.8. The expected hard-selected representation is also 4.8, but any individual draw is 2, 6 or 10, never 4.8.
Now let the next computation square its input. Its expected result under hard attention is 0.5 × 4 + 0.3 × 36 + 0.2 × 100 = 32.8. Squaring the soft context gives 4.8 squared = 23.04. Matching the expected context therefore does not make the downstream nonlinear computations equivalent. These invented scalar features illustrate the distinction; they are not image pixels or results from a trained attention network.
Illustrative case study
Fictional case: Juniper Inspection tests a model that selects a few image patches for detailed analysis. The team's first demonstration shows clear selections on obvious defects, but a separate evaluation finds misses when defects lie near patch boundaries. Reviewers also discover that the full high-resolution feature map is still computed before selection.
The team revises its evaluation to include difficult locations and measures end-to-end processing cost. It documents both selection failures and the actual source of any savings. This fictional case makes no claim that hard attention always provides faster inference or more reliable explanations.
Common misconceptions and mistakes
A hard choice need not be the highest-probability choice: stochastic sampling can choose a less probable location. Conversely, always choosing the maximum changes a stochastic policy into a deterministic rule and may alter performance.
Hard attention is not automatically an explanation of model reasoning. A selected representation can contain information from a broader receptive field, and downstream computation can still be complex. An attention probability is not necessarily a calibrated probability that the associated region contains the correct answer. Finally, a visually appealing selection map does not replace controlled evaluation of accuracy, robustness or computation.
Business use
Use hard attention when discrete information access fits the task and its training complexity is justified. Define the cost or limitation that selection is meant to address: memory access, high-resolution processing, a fixed observation budget or another concrete constraint.
Measure the full pipeline and compare against an appropriate soft or simpler selection baseline. Test repeated stochastic runs, missed-information cases and differences between training and deployment selection rules. For operational decisions, provide a way to detect or handle inadequate observations. The ability to point to a selected input should be communicated as a record of access, with interpretation assessed separately.
People also ask
Is hard attention always random?
No. Discrete choices can be deterministic, although the classic stochastic variant samples from learned probabilities.
Why can training be difficult?
The discrete choice interrupts ordinary pathwise backpropagation, so gradient estimators or other training methods are needed.
Is soft attention the same as averaging many hard-attention predictions?
Not generally. A nonlinear downstream model applied to an averaged context can differ from the average of predictions from selected contexts.
Check your understanding
Why does the example give 32.8 for expected squared hard context but 23.04 for squared soft context?
Answer: Squaring is nonlinear. Averaging squared selected values is different from squaring their weighted average.
Related terms
- REINFORCE
- Soft attention
- discrete latent variable
- score-function gradient
- glimpse model
- attention distribution
- sparse attention
- information selection
Learn next
Reproduce the scalar selection example, then compare sampled choices with deterministic maximum selection. Next study how a score-function gradient trains the selection probabilities and why a baseline can reduce its variance.
More in Probabilistic machine learning
- Gibbs kernel - The Gibbs kernel is a Gaussian-process covariance function that allows the characteristic length scale to vary with the input location.
- Graphite graph generative model - Graphite is a graph generative modeling framework that combines latent node representations with an iterative graph-neural-network decoder.
- Hamiltonian Monte Carlo - Hamiltonian Monte Carlo, or HMC, is a Markov chain Monte Carlo method for sampling from continuous probability distributions using gradients of their log...
- Hard expectation maximization - Hard expectation maximization, or hard EM, fits a latent-variable model by replacing uncertainty about hidden variables with selected assignments and then...
- Harmonic mean estimator - The harmonic mean estimator is a method for estimating a Bayesian model's marginal likelihood from posterior samples by taking the harmonic mean of their...
- Hidden semi-Markov model - A hidden semi-Markov model, or HSMM, is a probabilistic model for sequences in which unobserved states can have explicitly modeled durations.
Sources
Xu and colleagues, Show, Attend and Tell: Neural Image Caption Generation with Visual Attention: https://proceedings.mlr.press/v37/xuc15.pdf
Independent review consulted Xu and colleagues, sections 4.1–4.2, including sampled selection, score-function training and soft context. Original nonlinear-expectation comparison verified; inspection case fictional.
Previous: Hannan - Quinn information criterion Next: Hard clustering