liveBook · Manning

Transformer architecture

As explained in How Large Language Models Work

A transformer is a neural-network architecture that uses attention mechanisms to process relationships among elements in a sequence.

A transformer is a neural-network architecture for processing sequences. It represents tokens as numbers, compares their relationships, and predicts useful outputs. In language models, it predicts tokens rather than literally understanding language or reasoning like a person. section 3.1 section 3.1.1

The basic pipeline has three parts: an embedding layer maps tokens to meaning-bearing vectors; repeated transformer layers relate those vectors; and an output layer converts numerical results into readable words. Position information is added because word order does not arise naturally from token representations. section 3.1.1 section 3.5

Attention supplies the architecture’s flexible way to connect sequence elements:

  • Treat a query as what the model is looking for, keys as possible matches, and values as information associated with those matches. section 3.2.2
  • Compare every key with a query by similarity, weighting stronger matches more heavily instead of requiring an exact dictionary lookup. section 3.2.2
  • Use attention to emphasize relevant information and reduce the influence of distracting information; what matters changes with the available alternatives. section 3.2.2
  • Feed previously generated tokens back into the model at each step, then produce probabilities for every possible next token before selecting one. section 3.5

A fuzzy dictionary

A Python dictionary needs an exact key to return a value. A transformer instead compares a query with all available keys and combines their values according to similarity. In next-token prediction, the current token acts as the query and preceding words provide keys. The result is not a guaranteed answer: it is a probability distribution from which a token can be selected, with randomness affecting consistency and creativity. section 3.2.2 section 3.5

Figure 3.1 The basic components of the transformer model, consisting of the embedding layer, multiple transformer layers, and the output layer
figure

Use the architecture diagram to trace tokens through embeddings, multiple transformer layers, and the output layer. section 3.1.1

Transformer variants serve different jobs. Encoder-only models create representations for machine-learning tasks; decoder-only models generate continuations; and encoder-decoder models transform one passage into another, often helping with translation or summarization. Larger transformers can scale efficiently, but they demand more resources, increase prediction time, and may require remote hardware or continuous connectivity. section 3.1 section 4.5

How How Large Language Models Work covers it

How deep it goes
Explains layers, attention, prediction types, and scaling behavior, but stops short of building an LLM from scratch or exposing proprietary details.
Examples
Uses Python dictionaries, preceding words, next-token prediction, translation, summarization, and a component diagram.
What it assumes
Assumes basic coding constructs but teaches transformer vocabulary and mechanics without requiring advanced mathematics.
What you can do after
Evaluate/understand transformer outputs, model variants, sampling behavior, and practical deployment trade-offs.
How it connects
Connects embeddings, positional encoding, attention, autoregressive models, language models, tokenization, and sampling.

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 How Large Language Models Work. The section links point to the parts of the book it draws on.