
What Attention Really Computes, and Why Its Weights Don't Explain Answers
- كاتب موثّق
محتويات المقال
The common misconception
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.
What actually happens
What one attention head computes
Project Learned matrices turn every token's vector into a query, a key and a value. Score The current token's query is multiplied with every key, and each result is divided by the square root of the key size. Normalize A softmax turns the scores into weights that are positive and add up to 1. Mix The head outputs the weighted sum of the value vectors. The heads' outputs are combined and added back to the token's own vector.
A weight isn't a contribution
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}")<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
Different weights, same answer
Layers mix the tokens
How to apply it in your project
Name the question "Which inputs changed this output?" is an attribution question. Answer it by changing inputs, not by reading weights. Start with perturbation Remove 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. Weigh attention by vector size If 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". Test sources in LLM features To 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.
📚 المصادر
Vaswani et al., Attention Is All You Need, arXiv:1706.03762 LLMs: What's a large language model? — Google Machine Learning Crash Course Jain and Wallace, Attention is not Explanation, arXiv:1902.10186 Kobayashi et al., Attention is Not Only a Weight: Analyzing Transformers with Vector Norms, arXiv:2004.10102 Xiao et al., Efficient Streaming Language Models with Attention Sinks, arXiv:2309.17453
التعليقات (0)