Attention Visualization › Attention Across Layers
Language AI HandbookAll chapters Attention Visualization › Attention Across LayersOne section · Sections in this chapter10 / 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
Patterns in attention change substantially across the depth of the model. Understanding this layer-wise progression is important for knowing where to look when investigating a specific linguistic phenomenon. Early, middle, and late layers tend to serve different purposes, and that structure affects both what you can find and what you should expect to see.
Early Layers: Surface Patterns
Early layers (layers 1-4 in BERT-base) tend to show simpler patterns: strong self-attention, positional attention, or attention toward punctuation tokens. These early layers are building basic representations of word meaning and local context. At this stage, the model is essentially performing a form of contextualized smoothing: blending nearby token representations to create richer local context before attempting to resolve longer-range dependencies.
The positional heads that appear in early layers are not learning syntax. They are learning that adjacent words tend to be relevant to each other, a statistical regularity that holds across almost all natural language. A token like "quickly" is more likely to be relevant to the verb it modifies than to a noun three positions away, and early positional heads capture this general proximity bias before the model has built enough contextual information to make more specific decisions.
Middle Layers: Syntactic Structure
Middle layers (layers 5-8) show more syntactically-organized patterns. It is in these layers that the heads most resembling dependency parsers or part-of-speech taggers tend to appear. The representations at this depth encode sentence structure as well as word meaning. By layer 5, the model has built representations that distinguish "bank" in a financial context from "bank" in a geographical one, and the attention patterns reflect this richer understanding.
The syntactic specialization of middle layers makes intuitive sense given how BERT was trained. The masked language modeling objective forces the model to predict masked words in context. To predict "deposits" correctly when "bank" is visible, the model benefits from understanding the subject-verb-object structure of the sentence. Middle layers appear to be where the model encodes that structural information most explicitly, because it is at this depth that the representations are abstract enough to capture syntax but not yet dominated by task-specific semantics.
Late Layers: Semantic Aggregation
Late layers (layers 9-12) attend more selectively to semantically relevant tokens. In a sentence completion or classification task, these layers are focused on gathering the specific information needed for the output. The [CLS] token, which carries the aggregate sentence representation used for classification, accumulates information aggressively in these layers.
In later layers, you often see lower average entropy because the model is making more decisive choices about which information to aggregate. A [CLS] token in layer 11 that is computing a sentiment representation may strongly attend to sentiment-bearing words like "excellent" or "disappointing," producing a focused, interpretable pattern. But this focus is task-driven: a model fine-tuned for sentiment analysis will develop different late-layer patterns than a model fine-tuned for question answering.
This layered progression is not universal. It was documented primarily in BERT trained with masked language modeling. Decoder-only models like GPT, which always have causal (lower-triangular) attention masks, show different progressions because each token can only attend to preceding tokens. The absence of bidirectional context changes what functions early versus late layers can perform.
We can visualize this layer progression directly by plotting the mean entropy per layer (averaged across all heads and sentences). Layers where entropy is lower contain more selective heads on average; layers where entropy is higher contain broader, more aggregating heads.

The shaded band shows the range from the most focused head to the most diffuse head within each layer. Wide bands indicate high diversity among heads in that layer; narrow bands indicate that heads in that layer behave more uniformly.
Causal vs. Bidirectional Attention
A central visualization difference exists between encoder-only models (BERT) and decoder-only models (GPT). BERT uses bidirectional attention: every token can attend to every other token. The attention matrix is fully populated. This means every entry in the attention matrix carries useful information, and you can read patterns in any direction.
GPT-style models use causal masking: token can only attend to tokens . The attention matrix is lower-triangular, with the upper triangle set to before softmax (which maps to exactly zero after softmax). When visualizing GPT attention, you will see this triangular structure, and any interpretation must account for the fact that "future" tokens are invisible to earlier positions. This asymmetry fundamentally changes what patterns are possible: you cannot have a GPT attention head that connects a pronoun back to a later antecedent, because earlier tokens simply cannot attend to later ones.
# Demonstrate causal attention pattern with GPT-2
from transformers import AutoModelForCausalLM
from transformers import AutoTokenizer as GPT2Tokenizer
gpt_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
gpt_model = AutoModelForCausalLM.from_pretrained("gpt2", output_attentions=True)
gpt_model.eval()
gpt_inputs = gpt_tokenizer(
"The bank can guarantee deposits will", return_tensors="pt"
)
with torch.no_grad():
gpt_outputs = gpt_model(**gpt_inputs, output_attentions=True)
gpt_tokens = gpt_tokenizer.convert_ids_to_tokens(gpt_inputs["input_ids"][0])
gpt_attn = gpt_outputs.attentions
# Extract one head to show the causal (triangular) mask
gpt_head_attn = gpt_attn[5][0, 0].numpy()
The upper triangle is always zero in GPT-style models. This has a practical implication for visualization: entropy calculations, aggregation, and interpretation must account for the fact that later tokens have access to more context than earlier tokens. The attention distribution for the last token in a long sequence has the richest context; the distribution for the first token attends only to itself.
This positional asymmetry also means that entropy analysis of GPT-style attention requires different baselines than BERT analysis. A token at position 1 can attend to at most 2 positions (itself and position 0), while a token at the last position can attend to all positions. The maximum possible entropy scales with position, so a naive entropy comparison across positions within the same GPT attention matrix is not meaningful without position-dependent normalization.