Bahdanau Attention › Visualizing Attention Alignments

Michael BrenndoerferUpdated May 22, 20258 min read
Language AI HandbookAll chapters
Part 12 of 67Sequence-to-Sequence
Chapter 87Bahdanau Attention › Visualizing Attention Alignments
Reading section 6 of 11 · Visualizing Attention Alignments · 8 minSection 6 of 11 · 8 min

Book contents

Section 6 of 11 · 8 min read

Bahdanau Attention › Visualizing Attention AlignmentsOne section · Sections in this chapter6 / 11 ↓
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

Attention mechanisms are interpretable. We can visualize the attention weights to see what the model is "looking at" when generating each output token.

Out[28]:
Visualization
Heatmap showing attention weights for English to French translation, with bright diagonal cells indicating word-to-word correspondences.
Attention alignment visualization for English to French translation. Each row shows the attention distribution when generating a particular French word. The model learns to align 'Le' with 'The', 'chat' with 'cat', and so on, without explicit supervision.

The attention heatmap reveals several interesting patterns:

  • Monotonic alignment: For this simple sentence, attention roughly follows the diagonal. This reflects the similar word order between English and French.
  • Word-to-word correspondence: "Le" attends strongly to "The," "chat" to "cat," and "tapis" to "mat."
  • Many-to-one mapping: Both "était" and "assis" attend primarily to "sat," since the single English word requires two French words.
  • Soft attention: Even when one position dominates, other positions receive small but non-zero weights.

We can also visualize how the attention distribution evolves step by step during decoding. Each row in the heatmap above represents a single decoding step, but viewing them as individual distributions makes the shifting focus more apparent:

Out[29]:
Visualization
Bar chart showing attention distribution when generating Le, with highest bar over The.
Generating ''Le'': attention focuses on ''The'' (α=0.80).
Bar chart showing attention distribution when generating chat, with highest bar over cat.
Generating ''Le'': attention focuses on ''The'' (α=0.80).
Bar chart showing attention distribution when generating était, with highest bar over sat.
Generating ''Le'': attention focuses on ''The'' (α=0.80).
Bar chart showing attention distribution when generating assis, with highest bar over sat.
Generating ''Le'': attention focuses on ''The'' (α=0.80).
Bar chart showing attention distribution when generating sur, with highest bar over on.
Generating ''Le'': attention focuses on ''The'' (α=0.80).
Bar chart showing attention distribution when generating le, with highest bar over the.
Generating ''Le'': attention focuses on ''The'' (α=0.80).
Bar chart showing attention distribution when generating tapis, with highest bar over mat.
Generating ''Le'': attention focuses on ''The'' (α=0.80).

This interpretability is valuable for debugging and understanding model behavior. If a translation is wrong, we can inspect the attention weights to see if the model was looking at the right parts of the input.

Attention Entropy: Measuring Focus

A useful metric for understanding attention behavior is entropy, which quantifies how spread out or focused the attention distribution is. Low entropy indicates concentrated attention on few positions; high entropy indicates diffuse attention across many positions.

Out[30]:
Visualization
Bar chart showing attention entropy for each decoding step, with bars colored by entropy level.
Attention entropy varies across decoding steps. Content words (nouns, verbs) typically receive focused attention (low entropy), while function words and multi-word expressions may require distributed attention (higher entropy). Most words in this translation have entropy below 1.0, indicating clear word-to-word correspondences.

The entropy analysis reveals that most decoding steps in this translation have relatively focused attention (entropy below 1.0), indicating clear word-to-word correspondences. Steps with higher entropy might indicate cases where the model needs to aggregate information from multiple source positions, such as translating idiomatic expressions or handling word reordering.

Non-Monotonic Alignment Patterns

While English-French translation often exhibits roughly monotonic alignment (words appear in similar order), attention can handle more complex patterns. Languages with different word orders, or sentences with long-distance dependencies, produce non-monotonic attention patterns.

Out[31]:
Visualization
Heatmap showing attention alignment for English to German translation with non-diagonal attention pattern due to verb placement differences.
Non-monotonic attention patterns in English-German translation. German verb placement differs from English, requiring the attention mechanism to 'jump' across the source sentence. The model learns to attend to 'has' and 'eaten' when generating the German verb 'gegessen' at the end.

This example demonstrates one of attention's key strengths: it can learn arbitrary alignment patterns without explicit supervision. The model discovers that German places the past participle ("gegessen") at the end of the clause, and learns to attend to the corresponding English words ("has eaten") regardless of their position.