liveBook · Manning

Transformer architecture

As explained in Inside Deep Learning: Math, Algorithms, Models

A sequence model built from positional encoding and multiheaded attention, used here as the standard original design for NLP and other tasks.

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

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Defines positional encoding, multiheaded attention, encoder-decoder blocks, residual paths, and future masking, without deriving their mathematics.
Examples
Uses sentiment classification, machine translation, PyTorch encoder code, and training-accuracy comparisons with GRU, CNN, and attention models.
What it assumes
Assumes familiarity with sequence modeling and develops positional encoding and multiheaded attention before assembling transformer blocks.
What you can do after
Build a basic PyTorch transformer classifier and recognize when autoregressive attention requires future-token masking.
How it connects
Connects positional encoding, multiheaded attention, self-attention, residual connections, layer normalization, RNNs, and sequence-to-sequence models.

Also explained in

Compare how 28 books explain transformer architecture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.