الانتقال إلى المحتوى
Deep Learning و LLMsTransformers· 7 دقائق للقراءة

What Attention Really Computes, and Why Its Weights Don't Explain Answers

An attention weight says where one head read from, not how much it mattered or why the model answered.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

Your team fine-tunes a BERT-style model to route support tickets, and the dashboard explains each decision with a heatmap of last-layer attention weights. An auditor asks why one ticket was marked urgent. The brightest cells sit on the separator token and two periods. Is the model routing tickets on punctuation? Probably not. The heatmap answers a different question than the one the auditor asked.

The common misconception

"Attention weights show which words the model focused on, so they explain why it gave an answer." There are good reasons to believe it:

  • The name suggests human focus, and a weight between 0 and 1 looks like a share of importance.
  • The classic teaching example works: in "The animal didn't cross the street because it was too tired", Google's Machine Learning Crash Course shows self-attention rating "animal" as more relevant than "street" to "it".
  • The original Transformer paper said self-attention "could yield more interpretable models" and showed heads that seemed to track sentence structure.
  • Heatmaps are one line of code away, and they look like an answer.

It's a half-truth. Some heads do track meaningful relations. But a weight describes one routing step inside the model, and an explanation needs to say what changed the output.

What actually happens

What one attention head computes

In , each token's vector is updated with information from the other tokens:

  1. ProjectLearned matrices turn every token's vector into a query, a key and a value.
  2. ScoreThe current token's query is multiplied with every key, and each result is divided by the square root of the key size.
  3. NormalizeA softmax turns the scores into weights that are positive and add up to 1.
  4. MixThe head outputs the weighted sum of the value vectors. The heads' outputs are combined and added back to the token's own vector.

That's one head. The original Transformer ran 8 heads in each of 6 layers in its encoder, and every layer also has a feed-forward network that transforms each position on its own. The weights show only the mixing step of one head.

A weight isn't a contribution

The output is weight times value, summed. A large weight on a tiny value vector moves almost nothing. Kobayashi and colleagues measured this in BERT-base in 2020: the separator and classification tokens, periods and commas received remarkably large attention weights, but the weighted vectors they supplied were small. Weight and vector size tended to cancel out, so "a lot of attention" meant "collect almost nothing". This toy head shows the effect:

Python attention_vs_contribution.py
import numpy as nptokens = ["<s>", "refund", "not", "approved"]q = np.array([1.0, 0.2])                # query of the current tokenK = np.array([[2.6, 0.0], [0.9, 0.5],   # one key per input token              [1.2, 0.0], [0.5, 0.8]])V = np.array([[0.02, 0.01], [0.6, 0.4],  # one value per input token              [1.8, -1.4], [0.5, 0.9]])scores = K @ q / np.sqrt(len(q))         # scaled dot productsweights = np.exp(scores) / np.exp(scores).sum()  # softmax: sums to 1output = weights @ V                     # what this head addsfor tok, w, v in zip(tokens, weights, V):    share = np.linalg.norm(w * v)        # size of this token's part    print(f"{tok:>8}  weight {w:.2f}  contribution {share:.2f}")

The start token <s> gets 0.51 of the attention but contributes 0.01. The word "not" gets 0.19 and supplies 0.44, the largest part of the output. A heatmap of weights would point at the wrong token.

Softmax has to put the weight somewhere

The weights must add up to 1 even when no token is relevant. Xiao and colleagues found that LLMs, Llama-2 among them, give a surprisingly large share of attention to the first tokens regardless of their meaning. In Llama-2-7B, beyond the bottom two layers, heads consistently focused on the initial tokens. When the first four tokens were replaced with line breaks, the models still attended to them heavily. They called these tokens an : a place to park weight the head doesn't need, chosen by position, not meaning.

Different weights, same answer

Jain and Wallace tested attention in 2019 on text classification, question answering and inference models. Attention weights were often uncorrelated with gradient-based and leave-one-out measures of which words mattered. And they could build very different attention distributions that gave essentially the same prediction: in one negative movie review, the original weights favored "waste", the alternative favored "was", and the output didn't move. Both can't be the reason.

Their models were recurrent networks with attention, not Transformers, so treat the numbers as theirs. The logic carries over: if you can swap the heatmap without changing the output, the heatmap isn't the explanation.

Layers mix the tokens

After the first layer, the vector at the position of "refund" already blends information from other positions. A weight in a deep layer points at a position whose contents have been mixed many times, not at the original word. And the feed-forward layers between attention steps do work that no attention map shows.

None of this makes attention useless. Kobayashi and colleagues extracted reasonable word alignments from a translation Transformer once they accounted for vector sizes. Attention is evidence about routing, to be tested against what happens when you change the input.

MythReality
A high weight marks an important wordWeight times value size decides what moves; BERT gives separators big weights and small vectors
Attention shows where the model looksIt shows where one head in one layer read from, among many heads and layers
The heatmap explains the answerVery different heatmaps gave the same prediction in Jain and Wallace's models
Heavy attention on the first token means it mattersLLMs park spare weight on initial tokens, even when they're line breaks
So attention weights are uselessThey show routing, and can be informative once checked against ablations

How to apply it in your project

  1. Name the question"Which inputs changed this output?" is an attribution question. Answer it by changing inputs, not by reading weights.
  2. Start with perturbationRemove or replace a word, a sentence or a document, rerun the model and measure the change in the output probability. Leave-one-out is slow, but it means exactly what it says.
  3. Weigh attention by vector sizeIf you study attention in an open model, plot the size of weight times value rather than raw weights, set the first-token sink aside, and label the chart "where this head read from".
  4. Test sources in LLM featuresTo learn whether an answer depends on a retrieved document, drop that document and ask again. If the answer stays, the document wasn't the reason.

Key takeaways

  • Attention is a weighted mix of value vectors: a routing step inside one head, not a reason.
  • A large weight can move almost nothing, because the value vector's size matters too.
  • Softmax must spend its weight, and LLMs park it on the first tokens as a sink.
  • Very different attention patterns can give the same prediction, so a heatmap isn't a unique explanation.
  • To explain an output, change the input and measure the effect.

📚 المصادر

  1. Vaswani et al., Attention Is All You Need, arXiv:1706.03762
  2. LLMs: What's a large language model? — Google Machine Learning Crash Course
  3. Jain and Wallace, Attention is not Explanation, arXiv:1902.10186
  4. Kobayashi et al., Attention is Not Only a Weight: Analyzing Transformers with Vector Norms, arXiv:2004.10102
  5. Xiao et al., Efficient Streaming Language Models with Attention Sinks, arXiv:2309.17453

التعليقات (0)