Transformer attention is often inspected as a matrix of weights. That works for a few examples, but it becomes difficult when you need to compare many heads, layers, tokens, or model runs. A useful summary is attention entropy: a number that describes how concentrated or spread out one attention distribution is.

Entropy can answer a narrow but practical question: does this query place most of its attention mass on a few available positions, or distribute that mass broadly? It does not tell you whether the model is correct, whether a token caused the prediction, or whether a head is important. Used with those limits in mind, it is a compact diagnostic for attention behavior.

This article builds the metric from a small example, explains how sequence length and masking affect it, and shows how to interpret comparisons without giving entropy more meaning than it has.

Start with one attention distribution

Consider one attention head processing a query token that can attend to four positions. After softmax, its attention weights might be:

[0.70, 0.10, 0.10, 0.10]

The weights are non-negative and sum to 1, so they form a probability distribution over the positions available to that query.

Compare it with:

[0.25, 0.25, 0.25, 0.25]

The first distribution is concentrated: one position receives most of the mass. The second is uniform: every position receives the same mass. Attention entropy turns this difference into a scalar.

For attention weights p_1, ..., p_n, Shannon entropy is:

H(p) = -sum(p_i * ln(p_i))

Terms with p_i = 0 contribute 0, using the limit of p * ln(p) as p approaches zero.

Using the natural logarithm gives entropy in nats. A different logarithm base changes the unit but not the ordering of distributions when comparisons use the same base.

Read entropy as a concentration measure

For a distribution over n available positions, entropy has two useful endpoints.

If all attention mass is on one position:

[1, 0, 0, 0]

then:

H = 0

If attention is uniform over all n positions, entropy reaches its maximum:

H_max = ln(n)

For four positions, the uniform distribution therefore has:

H = ln(4) ~= 1.386

The earlier distribution [0.70, 0.10, 0.10, 0.10] has entropy of about 0.94 nats. It is neither fully concentrated nor uniform, which matches what we can see directly from the weights.

This gives a simple mental model:

  • lower entropy means attention mass is more concentrated;
  • higher entropy means attention mass is more diffuse across the available positions.

That interpretation describes the distribution only. Low entropy does not imply good attention, and high entropy does not imply bad attention.

Normalize when the number of available positions changes

Raw entropy depends on how many positions a query is allowed to attend to. This matters immediately for variable-length inputs and causal attention.

Suppose one query has four valid positions and another has 100. Their maximum possible entropies are different:

ln(4)   ~= 1.386
ln(100) ~= 4.605

Comparing their raw entropy values can therefore mix two effects: the shape of the attention distribution and the size of its support.

When n > 1, a common normalization is:

H_normalized = H(p) / ln(n)

This produces a value between 0 and 1 for a valid discrete attention distribution:

0 -> maximally concentrated
1 -> uniform over all available positions

For n = 1, the only possible distribution is [1] and its entropy is 0, but the normalized formula would divide by ln(1) = 0. Handle that case explicitly rather than applying the formula blindly.

Normalization is especially useful when the goal is to compare concentration across queries with different numbers of valid keys. Raw entropy is still useful when all compared distributions have the same support size and you want the value in a conventional entropy unit.

Count valid keys, not padded positions

Padding can quietly corrupt an attention-entropy analysis.

Imagine a sequence with six tensor positions but only four real tokens. If the attention mask correctly excludes the two padding positions, the query effectively has four available keys, not six. Its maximum entropy is therefore ln(4), not ln(6).

A robust calculation follows the model’s actual attention support:

1. identify keys that are valid for this query
2. take the attention weights over those keys
3. verify or restore normalization over the valid weights
4. compute entropy
5. if needed, normalize by ln(number of valid keys)

Do not include masked padding positions merely because the returned attention tensor contains slots for them. Depending on the implementation and numerical precision, masked entries may be represented by exact zeros or extremely small values. The conceptual support is defined by the mask, not by the tensor width.

The same rule applies to other masks. A causal decoder query near the beginning of a sequence can attend to fewer preceding positions than a query near the end. A custom local-attention pattern may restrict the support further.

Average only after deciding what a sample means

A model can produce an entropy value for every query position in every head and layer. Turning those values into one summary requires a deliberate aggregation policy.

For example, suppose a batch contains one sequence with 20 valid query tokens and another with 200. If you average all token-level entropy values together, the longer sequence contributes ten times as many observations. That may be appropriate for a token-weighted analysis, but it is not the same as giving each sequence equal importance.

Two valid summaries answer different questions:

Token-weighted mean:
average entropy over every valid query token

Sequence-weighted mean:
average within each sequence, then average sequence means

Likewise, averaging across attention heads can hide specialization. One head with consistently low entropy and another with high entropy may produce an ordinary-looking mean. Keep per-head or per-layer distributions when those distinctions matter.

A practical report often includes more than a mean: median and selected quantiles can reveal whether a change affects most queries or only a small tail.

Compare like with like

Attention entropy is most useful in controlled comparisons.

Suppose you change a model’s prompt format and observe that normalized entropy in one layer falls from 0.78 to 0.52. That tells you attention became more concentrated under the new condition. It does not, by itself, explain why.

Before attributing the difference to the prompt format, check whether other factors changed:

  • tokenization and sequence lengths;
  • attention masks;
  • model parameters or checkpoint;
  • inference mode and architecture-specific behavior;
  • which query positions, heads, and layers were aggregated.

This is particularly important when comparing different model architectures. Attention mechanisms can differ in head structure, masking, positional treatment, and whether attention weights are even exposed in the same form. A mathematically identical entropy calculation does not make all cross-model comparisons semantically equivalent.

Entropy does not measure model importance

The most important limitation is interpretive.

An attention distribution with low entropy says that its probability mass is concentrated. It does not establish that the attended token caused the model’s output or that removing the corresponding head would damage performance.

Several reasons separate attention concentration from model importance:

  • attention weights determine how value vectors are mixed inside an attention operation, but the final output also depends on those value vectors;
  • residual connections and later layers provide other paths for information;
  • multiple heads can carry redundant or complementary signals;
  • a sharply focused head can consistently attend to a position that contributes little useful information.

Therefore, use entropy as a descriptive statistic, not as a causal explanation.

If the real question is whether a component matters to model behavior, an intervention is usually more informative. Examples include masking selected inputs, ablating a head when the architecture and tooling permit it, or measuring how an output metric changes under a controlled perturbation. Those tests answer different questions from entropy.

Entropy can hide where attention goes

Two distributions can have the same entropy while attending to different positions.

For example, these distributions are permutations of each other:

[0.8, 0.1, 0.1]
[0.1, 0.8, 0.1]

Their entropy is identical, but the dominant key is different. If the identity of the attended position matters, entropy alone discards exactly that information.

This is why entropy works well as a screening metric. It can reveal heads, layers, examples, or conditions whose concentration changes, after which you can inspect the underlying attention patterns for location-specific details.

The reverse is also important: unchanged entropy does not mean unchanged attention. Mass can move between positions without changing the distribution’s overall concentration.

Use entropy for diagnostics, not as an automatic objective

It can be tempting to decide that a model should have lower or higher attention entropy and then optimize for that value. There is no general rule supporting either direction.

Some tasks may benefit from sharply selecting a small number of relevant positions. Others require integrating evidence across many tokens. Different heads in the same model can also serve different roles.

Adding an entropy term to a training objective changes the optimization problem. Encouraging low entropy pushes distributions toward concentration; encouraging high entropy pushes them toward diffusion. Either choice can interfere with the task objective if it is based only on a visual preference for what attention “should” look like.

Treat such regularization as a modeling hypothesis that needs task-specific validation, not as a default improvement.

A practical analysis workflow

For a useful attention-entropy experiment, keep the question narrow. For example: “Does this fine-tuning run make layer 8 attention more concentrated on the same evaluation set?”

Then:

1. run the same examples through both checkpoints
2. preserve the actual attention masks
3. compute entropy over valid keys for each valid query
4. normalize if support sizes differ
5. compare the same layers and heads
6. inspect the distribution, not only the global mean
7. relate the change to a task metric before drawing quality conclusions

For large models, retaining every attention matrix can be expensive because attention tensors grow with query and key lengths. If the framework exposes the required weights, computing summaries during analysis and discarding full matrices can reduce storage pressure. The exact memory and execution cost depends on the model and attention implementation, so measure it in the environment you use.

Also note that some optimized attention implementations do not return materialized attention probabilities by default. Access to weights is an implementation capability, not a guarantee of the Transformer concept itself.

When attention entropy is the wrong tool

Skip entropy when your actual question can be answered more directly.

If you want to know whether the model’s task quality improved, use task-appropriate evaluation metrics. If you want to know whether predictions are reliable at a stated confidence, study calibration. If you want causal evidence that an input or component matters, use a controlled intervention. If you need to know which positions receive attention, inspect or aggregate the positions themselves rather than compressing them into one scalar.

Attention entropy is valuable when concentration itself is the property you want to measure.

Conclusion

Attention entropy compresses an attention distribution into a simple measure of concentration. Low values indicate that mass is focused on fewer available keys; high values indicate a more diffuse distribution. Normalize by the maximum entropy when support sizes differ, derive that support from the real attention mask, and define aggregation carefully across tokens, sequences, heads, and layers.

Most importantly, keep the metric’s meaning narrow. Entropy describes the shape of attention weights. It does not prove model quality, feature importance, or causality. Used as a diagnostic alongside task metrics and targeted inspection, it can make large collections of attention patterns much easier to compare without pretending that one number explains the model.