Lesson 4

Transformer Architecture

The complete picture

The Problem: Before 2017, language models processed text one word at a time, like reading a sentence with a magnifying glass. They struggled with long texts because they forgot the beginning by the time they reached the end. How can we build a model that sees the entire text at once?

The Solution: The Transformer — A Factory for Understanding Language

The Transformer is a neural network architecture that revolutionized AI in 2017. Instead of reading text sequentially, it processes all tokens in parallel using the attention mechanism we learned about in the previous lesson.

Think of it as a factory with specialized departments. Text enters the factory, passes through multiple processing stages, and comes out as a prediction of the next token. Each stage refines the understanding of the text.

Think of it like a factory assembly line for language:

  • 1. Tokenization + Embeddings: Text is split into tokens via tokenization, and each token becomes a numerical vector (embedding) — like giving each word an ID badge.
  • 2. Positional Encoding: Since the model processes all tokens in parallel (not one by one), it has no idea about word order! Positional encoding solves this by adding a unique pattern of numbers to each token's embedding based on its position. Think of it like page numbers in a book — without them, shuffled pages would be meaningless. For each position (1st, 2nd, 3rd...), a special vector is generated and added to the token's embedding. After this, the model knows that "dog bites man" ≠ "man bites dog".
  • 3. Self-Attention Layers: Each token "looks at" every other token to understand context. The word "bank" learns it means "river bank" (not "financial bank") by attending to nearby words like "river" and "water".
  • 4. Feed-Forward Networks (FFN): After attention figured out which words relate to each other, the FFN layers process each token independently — like a specialist who got a briefing and now analyzes their assignment. Each FFN is two linear transformations with an activation function in between (a "filter + amplifier"). This is where facts and knowledge live: roughly 2/3 of the model's parameters are in FFN layers. For example, the fact that "Paris is the capital of France" is encoded in these weights.
  • 5. Layer Normalization (Layer Norm): Between each stage, layer normalization keeps numbers in a healthy range. Why is this needed? After many multiplications, numbers tend to either explode (→ infinity) or vanish (→ zero), making the model untrainable. Layer Norm rescales each token's values to have a stable mean and variance — like a thermostat keeping the temperature just right. Without it, deep networks (96+ layers in GPT-4!) would be impossible to train. Modern Transformers use "Pre-Norm" — normalizing before attention and FFN, not after.
  • 6. Output: Next Token Prediction: After all layers, the model outputs a probability distribution over the vocabulary — "the next word is 90% likely 'Paris', 5% 'London', ...". Steps 3-5 repeat many times (layers are stacked).

Modern LLMs like GPT-4 and Claude use a decoder-only Transformer — they only have the "prediction" half of the original architecture, with causal masking so each token can only see tokens before it (not after).

Encoder vs Decoder — What's the Difference?

  • Encoder-only (BERT): sees all tokens at once (bidirectional). Great for understanding: classification, search, sentiment analysis.
  • Decoder-only (GPT, Claude, LLaMA): each token sees only previous tokens (causal). Great for generation: chat, writing, code. This is what most modern LLMs use.
  • Encoder-Decoder (T5, original Transformer): encoder understands the input, decoder generates the output. Used for translation, summarization.

Fun Fact: The original "Attention Is All You Need" paper (2017) introduced the Transformer for machine translation. The authors probably didn't imagine that 7 years later, decoder-only Transformers would write code, compose poetry, and pass law exams! GPT-4 is estimated to have 1.8 trillion parameters across 120 Transformer layers.

Try It Yourself!

Below is an interactive visualization showing how data flows through a Transformer, step by step!

🤖 How does a Transformer work?

Follow how text becomes a prediction in 6 steps.

📝Input
🧩Tokens
📍Embed
👁️Attention
🔄Layers
🎯Output
📝
Step 1

You type a question or prompt — raw text goes in.

User enters a prompt:

|

💡 Key insight:

  • A Transformer is a pipeline: text → tokens → numbers → attention → processing → prediction.
  • The main magic is in the attention mechanism — it finds connections between words.
  • More layers = deeper understanding (GPT-4 has 96+ layers).

Frequently asked questions

What is the Transformer architecture?

The Transformer is a neural network architecture introduced in the paper 'Attention Is All You Need' (2017). It uses self-attention instead of recurrent layers, enabling parallel processing of sequences and efficiently capturing long-range dependencies.

What is the difference between encoder and decoder in Transformers?

The encoder processes the input sequence and creates contextual representations for each token. The decoder generates the output sequence token by token, using masked attention and cross-attention to the encoder output. GPT uses decoder-only, BERT uses encoder-only, and T5 uses both.

Why is positional encoding needed?

Unlike RNNs, Transformers process all tokens in parallel and have no built-in notion of order. Positional encoding adds information about each token's position in the sequence so the model understands word order.

Which models are built on the Transformer architecture?

All modern LLMs use Transformers: GPT-4 and ChatGPT (decoder-only), Claude (decoder-only), Llama (decoder-only), BERT (encoder-only), T5 (encoder-decoder). The architecture is also used in Vision Transformers for images and Whisper for speech.

Try it yourself

Interactive demo of this technique

Technique Comparison
Demo Mode
Pre-recorded responses
TaskBeginnerAnalysis

Get the current stock price of Tesla

Without technique
Without technique
Prompt
What is the current Tesla stock price?
Response

As of my last data (2024) Tesla stock was trading around $250. However, I do not have access to real-time data. Please check current price on financial websites.

Tokens:12/45
Time:380ms
Quality:
With Transformers
With technique
Prompt
Analyze historical factors that influenced Tesla stock price in 2020-2024: 1. Stock split 2. S&P 500 inclusion 3. Competition in the EV sector What patterns can be identified?
Response

Key patterns:

  1. Split (Aug 2020, 5:1): +80% growth in the month before split — retail investors bought on expectations.
  2. S&P 500 (Dec 2020): +60% jump in one month — index funds were required to buy.
  3. Competition (2023-2024): pressure from BYD and European EVs — price wars narrowed margins.

Pattern: Tesla reacts to "narrative" events more strongly than financial metrics.

👁️Transformers generate text based on training data — they are not connected to the internet
🧠Requesting real-time data (current price) is a task for an API/tool, not a language model
Transformers are strong at pattern analysis, reasoning and synthesis — reframe the task to these strengths
Tokens:45/120
Time:680ms
Quality:
Why this works

Transformers have no real-time access — they generate text from training data. Use them for analysis and reasoning, and connect external tools for current data.

1 / 2

Lesson Quiz

1 of 3

1.Why do Transformer models use positional encoding?

Practice Challenges

Create a free account to solve challenges

4 AI-verified challenges for this lesson

Related lessons:AttentionInference

This lesson is part of a structured LLM course.

My Learning Path