A transformer replaces recurrent sequence processing with positional encoding and multiheaded attention. This enables parallel processing and scales better with depth and powerful compute, although training is computationally expensive. section 12.0 section 12.5
A vanilla transformer is organized around two block types:
- Encoder blocks apply self-attention, using the same sequence as queries, keys, and values, followed by a residual linear layer. They can stand alone for tasks such as sentiment classification. section 12.5.2
- Decoder blocks usually pair with an encoder for multi-output tasks such as machine translation. Their additional attention layer uses encoder outputs as keys and values. section 12.5.2
- Positional encodings supply sequence information because transformer blocks have no explicit recurrent time connections. section 12.5.2
- Autoregressive models require a triangular mask to prevent attention from accessing future inputs. section 12.5.2
PyTorch classifier skeleton
A practical encoder classifier embeds tokens, adds positional encodings, stacks three TransformerEncoder layers with eight attention heads, pools attention outputs, and predicts classes through linear layers. section 12.5.2
Figure 12.7 The two types of transformer blocks: the encoder (left) and decoder (right). Both use layer normalization, residual connections, and MHA layers. The first MHA in each is called self-attention because the same input is used for Q, K, and V.
Use the architecture diagram to compare encoder and decoder paths, residual connections, layer normalization, self-attention, and multiheaded attention. section 12.5.2



























