Transformer Architecture

What Is the Transformer?

The transformer is the neural network architecture introduced in the 2017 paper "Attention Is All You Need." It replaced earlier recurrent architectures and became the universal foundation of modern language AI — GPT, Claude, Gemini, Llama, and virtually every state-of-the-art model today.

The Key Shift

Before transformers, language models read text sequentially — word by word, left to right. This made it hard to connect context across long distances. Transformers eliminated this constraint: every position in the input can attend to every other position simultaneously, regardless of how far apart they are.

The Three Core Innovations

01

Self-Attention — lets every token see every other token at once and determine which context is most relevant

02

Positional Encoding — adds word order information so the model knows where each token sits in the sequence

03

Stacked Layers — many transformer blocks stacked on top of each other, each refining the representation from the one below

Self-Attention: The Core Mechanism

Self-attention lets the model determine, for each token, how much to attend to every other token when building that token's representation.

Intuition

When processing the word "bank" in "I deposited money at the bank," self-attention helps the model figure out that "deposited" and "money" are highly relevant context — more than "the" or "I" — for understanding which sense of "bank" is intended. The model learns these relationships during training.

The mechanism uses three learned vectors for each token:

Q

Query — "What am I looking for?" — represents what this token needs from the context

K

Key — "What do I represent?" — represents what this token offers to other tokens looking for context

V

Value — "What information do I carry?" — the actual content that gets aggregated into the output

Attention scores are computed by comparing each token's Query against all other tokens' Keys. High score = attend a lot. Low score = mostly ignore. The output is a weighted average of Value vectors — each token's final representation is enriched by the context it attended to.

Multi-Head Attention

In practice, transformers use multi-head attention — multiple attention mechanisms running in parallel, each attending to the input from a different perspective.

What different heads might capture

One head might track grammatical relationships (subject → verb agreement).

Another might track semantic similarity (synonyms and related concepts).

Another might track co-reference (which pronouns refer to which nouns).

Why multiple heads matter

Language is simultaneously structured by grammar, semantics, and discourse — one attention pattern cannot capture all of these at once.

Multiple heads let the model build richer, multidimensional representations by attending to the input through many lenses simultaneously.

Positional Encoding

Why It's Necessary

Attention computation has no inherent sense of order. Without position information, "The dog bit the man" and "The man bit the dog" look identical to the model — same tokens, same attention patterns. Positional encoding adds each token's position in the sequence to its representation before the first attention layer, giving the model order awareness.

Stacked Layers and Depth

A full transformer model is built by stacking many transformer blocks. Each block contains: self-attention → feed-forward network → layer normalization.

Layer Progression

Lower layers tend to capture low-level patterns: grammar, punctuation, common phrases. Higher layers tend to capture abstract semantics: meaning, reasoning steps, factual relationships. Think of each layer as an editorial pass — reading the draft from the layer below and producing a more refined, higher-level understanding. GPT-3 has 96 layers; modern frontier models often have hundreds.

Encoder vs. Decoder Transformers

Decoder-only (GPT, Claude, Llama)

Reads tokens left to right with causal masking — each token can only attend to previous tokens, never future ones.

Optimized for text generation. The standard architecture for chat assistants and code generation.

Encoder-only (BERT)

Reads the full input bidirectionally — each token attends to all others in both directions simultaneously.

Optimized for understanding and classification. Not suited for generation tasks.

Summary

Key Takeaways
  • The transformer replaced sequential RNNs and powers all modern LLMs
  • Self-attention lets every token attend to every other token simultaneously — no sequential bottleneck
  • Queries, Keys, and Values are the three vectors used in the attention computation
  • Multi-head attention captures grammar, semantics, and co-reference in parallel
  • Positional encoding gives the model awareness of token order
  • Stacked layers refine representations from simple syntax to abstract semantics
  • Decoder-only transformers (GPT, Claude, Llama) are the standard for text generation

Need Help?

Ask the AI if you need help understanding the transformer architecture, how self-attention works, what Queries, Keys, and Values are, why positional encoding is needed, or the difference between encoder-only and decoder-only models.