Attention in Sequence-to-Sequence Models
Derive attention weights, check their limiting behavior, and implement batch-first attention for German-to-English translation.
Yong-Hwan Lee
Published · Updated
Overview
This study combines attention derivations with a recurrent sequence-to-sequence experiment on Multi30k. The implementation uses a bidirectional GRU encoder and a GRU decoder, rather than a full Transformer.
The main questions are when attention can select one value, when it can average two values, and how changing key magnitudes changes that behavior.
Attention weights and selection
For a query , keys , and values , define
The weights are positive and sum to one. Finite, unmasked logits do not produce exact zero or one weights. The formulation follows Vaswani et al..
Selecting one value
A sufficient condition for is :
The target score must dominate the combined contribution of the others. A large key norm alone does not guarantee this; its alignment with the query matters.
Averaging two values
Suppose the keys are orthonormal: for and . Choose distinct indices and
Then , while the other scores are zero. Thus,
For , the selected weights approach only when their logits are sufficiently large relative to the remaining mass. Simply setting does not establish that approximation. When , equal logits give exactly equal weights.
Noisy keys and multiple heads
Let , with orthonormal and independent . Here denotes the variance.
Using gives
The relative weight is
Squared magnitudes determine which value dominates. Since , the expected selected score is , not .
Symmetry implies , but other values still receive positive weight at finite scale. In general, is not exactly . Applying softmax to expected scores is also different from averaging softmax outputs.
A two-head construction
Choose and . Each head's selected score is , with zero scores for the orthogonal keys. For nonzero selected scales and sufficiently large , the heads approach and separately. Averaging their outputs then approaches the desired mean.
This is a limiting construction that assumes access to the current keys. Standard Transformer multi-head attention uses learned projections and concatenates head outputs before another projection; averaging two outputs here is an explanatory simplification.
PyTorch implementation
This corrected example uses batch-first encoder outputs. The optional Boolean mask has shape (B, S), with True for valid tokens. Every sequence must contain at least one valid token.
import math
import torch
from torch import nn
class SingleQueryScaledDotProductAttention(nn.Module):
def __init__(self, enc_hid_dim, dec_hid_dim, kq_dim=64):
super().__init__()
self.W_k = nn.Linear(2 * enc_hid_dim, kq_dim)
self.W_q = nn.Linear(dec_hid_dim, kq_dim)
self.scale = math.sqrt(kq_dim)
def forward(self, hidden, encoder_outputs, mask=None):
# hidden: (B, H_dec); encoder_outputs: (B, S, 2 * H_enc)
query = self.W_q(hidden).unsqueeze(1) # (B, 1, D)
keys = self.W_k(encoder_outputs) # (B, S, D)
scores = torch.bmm(
query, keys.transpose(1, 2)
).squeeze(1) / self.scale # (B, S)
if mask is not None:
if mask.dtype != torch.bool or mask.shape != scores.shape:
raise ValueError("mask must be Boolean with shape (B, S)")
if not mask.any(dim=1).all():
raise ValueError("each sequence needs a valid source token")
scores = scores.masked_fill(~mask, float("-inf"))
weights = torch.softmax(scores, dim=-1) # (B, S)
context = torch.bmm(
weights.unsqueeze(1), encoder_outputs
).squeeze(1) # (B, 2 * H_enc)
return context, weights
transpose(1, 2) swaps the source and feature axes; .T(1, 2) is not a valid tensor operation. The attention weight shape is (B, S), not (B, B). See PyTorch's batch matrix multiplication documentation and the original notebook repository.
Masking is an explicit addition to this example. The historical runs below were not rerun with this revised snippet.
Translation results
The original report recorded three runs for each variant. These are historical results; the training runs were not repeated for this revision.
| Attention | PPL mean ↓ | PPL variance | BLEU mean ↑ | BLEU variance |
|---|---|---|---|---|
| None | 18.107 | 0.057 | 16.300 | 0.118 |
| Mean pooling | 15.809 | 0.028 | 18.599 | 0.277 |
| Scaled dot product | 10.447 | 0.049 | 34.640 | 0.793 |
BLEU is on a 0–100 scale. The attention variant has the lowest recorded perplexity and highest recorded BLEU. Three runs support a descriptive comparison, but the table alone does not establish statistical significance or explain run-to-run variation.
Inspecting alignments




Off-diagonal weights can reflect word-order differences and multiword translations. These plots are useful diagnostics, but do not alone establish that the model has learned a particular grammatical rule.
Takeaways
- Selection and averaging claims need explicit assumptions about score gaps, masking, and scale.
- Consistent tensor layouts prevent confusion between batch and sequence axes.
- The recorded translation results favor attention in this experiment; stronger claims need matched settings and more runs.