Attention Visualization › Attention Mechanisms: A Brief Recap

Michael BrenndoerferFebruary 11, 20264 min read
Language AI HandbookAll chapters
Part 59 of 67Interpretability
Chapter 447Attention Visualization › Attention Mechanisms: A Brief Recap
Reading section 2 of 15 · Attention Mechanisms: A Brief Recap · 4 minSection 2 of 15 · 4 min

Book contents

Section 2 of 15 · 4 min read

Attention Visualization › Attention Mechanisms: A Brief RecapOne section · Sections in this chapter2 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Building on the transformer architecture covered in the earlier parts of this book, recall that attention computes a weighted combination of values, where the weights reflect how relevant each position in the input is to the current query. This chapter assumes you are familiar with that mechanism. The key point for visualization purposes is that attention is not a single scalar per layer but a matrix per head per layer. This dimensionality is both a strength and a challenge: a model like BERT produces so many attention matrices that there is too much to examine by eye, which is why systematic, entropy-guided analysis is necessary rather than optional.

For a sequence of nn tokens, each attention head produces an n×nn \times n weight matrix. Entry (i,j)(i, j) in this matrix represents the degree to which position ii attends to position jj when the model processes that head. With a model that has LL layers and HH heads per layer, a single forward pass generates L×HL \times H such matrices. For BERT-base, which has 12 layers and 12 heads per layer, that is 144 attention matrices for every sentence. Each of those 144 matrices encodes a different view of the same sequence, learned independently through gradient descent. This redundancy is a feature: it allows the model to simultaneously reason about multiple types of relationships within the same input.

Attention Head

An attention head is one of the parallel attention mechanisms within a transformer layer. Each head applies its own learned query, key, and value projections to the input, producing an independent attention pattern. Multiple heads allow the model to simultaneously attend to different aspects of the input. The outputs of all heads are concatenated and linearly projected to produce the layer's final output.

The attention weight matrix for a single head is computed as:

A=softmax(QKTdk)A = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)

where:

  • QQ: the query matrix, shape (n×dk)(n \times d_k), obtained by projecting the input sequence through a learned weight matrix WQW^Q
  • KK: the key matrix, shape (n×dk)(n \times d_k), obtained through a learned projection WKW^K
  • dkd_k: the dimension of the key vectors, used to scale the dot products and prevent gradient saturation from large values
  • softmax\text{softmax}: applied row-wise, so each row of AA sums to 1 and can be interpreted as a probability distribution over positions

The resulting matrix AA has shape (n×n)(n \times n), and row ii is the attention distribution for token ii: it tells you how much weight token ii places on each token in the sequence when computing its updated representation.

Why does this formulation matter for visualization? The softmax normalization means that every row of AA is a proper probability distribution. You can interpret each row as a question: "Given that I am at position ii and I want to update my representation, which other positions should I consult, and by how much?" The answer varies by head and by layer, and that variation is exactly what attention visualization tries to expose.

Why Attention Weights Are Accessible

Attention weights occupy a privileged position among neural network internals: they are explicitly computed as an intermediate variable during the forward pass, before being used to weight the value vectors. Unlike the raw numerical weights of a dense layer, which transform inputs in high-dimensional space and resist direct interpretation, attention weights sit between zero and one and sum to one across each row. That probabilistic structure makes them look interpretable even when they may not fully reflect causal reasoning.

This accessibility explains why attention visualization emerged so early in transformer research. By 2018, papers like "Visualizing Attention in Transformer-Based Language Representation Models" and the original transformer paper's supplementary material were already displaying attention heatmaps. The technique requires minimal tooling, produces easy-to-read outputs, and maps onto intuitive concepts like "which words are important?" Whether the visual appeal tracks the underlying computation is the deeper question this chapter addresses.