One Attention Reading Is Not Yet a Transformer Layer
An attention reading answers what one position retrieves, but a reusable layer also needs a stable input-output width and a batch-safe interface. Add the output projection and identify which parts of a complete Transformer are still absent.
Suppose a query produces the attention reading
This equation answers a local question: what value vector should position read from the positions it is allowed to inspect? A neural-network layer must answer a larger question: what input does it accept, and what output can the next part of the network rely on?
Recall the Central Operation
For one sequence and one head,
Each row of belongs to one query position. Each column refers to a position whose value may be read. In causal attention, prevents a query from reading future positions.
Language Modeling derived this operation and calculated a complete four-token example. We retain it as the starting point. The new question is how becomes the output of a stable layer.
A Layer Needs a Stable Width
The input matrix has shape
The value projection is allowed to choose a different width:
If , then cannot be added to , and the next layer cannot assume that the model width stayed fixed. An output projection solves this interface problem:
Even when , is not redundant. It learns how the features produced by attention are written back into model-width coordinates. Chapter 2 will also use it to mix the concatenated outputs of several heads.
The Interface Extends to Batches
Training normally processes several sequences together. Adding a batch axis changes the shapes but not the operation:
| Quantity | Single sequence | Batch |
|---|---|---|
| Input | ||
| Queries and keys | ||
| Values | ||
| Scores and weights | ||
| Layer output |
The computation is independent for each batch item. A token in sequence 1 must never acquire an attention edge to sequence 2 merely because both are stored in one tensor.
Attention Predates the Transformer
Bahdanau, Cho, and Bengio proposed a learned soft alignment for neural machine translation in 2014. At each decoder step, that model searched over source annotations instead of relying only on one fixed-length source summary. The source and target sides had different roles.
The 2017 Transformer used attention throughout an encoder-decoder architecture and distinguished self-attention, where Q, K, and V come from the same sequence, from encoder-decoder attention, where queries and key-value records come from different sequences. The Transformer combined attention with multiple heads, position information, residual paths, normalization, and feed-forward layers. No single attention reading is the whole architecture.
Q1. Restore the layer width
A single head produces with shape . The model width is 10. What shape must have so that has model width 10?
Select one choice, then check.
Hint
Solution
One attention reading is only the central operation inside a reusable self-attention layer. The output projection restores the model width and the batch interface keeps sequences independent. A complete Transformer block still needs residual updates, normalization, and a position-wise nonlinear sublayer.
References
- Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio, Neural Machine Translation by Jointly Learning to Align and Translate (2014). The paper replaces exclusive reliance on one fixed source vector with a learned soft search over source annotations.
- Ashish Vaswani et al., Attention Is All You Need (2017). Sections 3.1 and 3.2 define the original Transformer architecture and its attention mechanisms.