Transformers Explained — The Architecture Behind GPT

Photo: Google DeepMind / Pexels
Every major AI model today — GPT-5, Claude, Gemini, Llama, DALL-E, Sora — is built on the Transformer architecture. Introduced in a 2017 Google paper called "Attention Is All You Need" (Vaswani et al.), it replaced recurrent neural networks and revolutionized AI. RNNs processed sequences one token at a time, forgetting context over long distances. Transformers process all tokens in parallel, attending to relationships between any pair regardless of distance. This single architectural shift made the current LLM era possible — and it's one of the most important innovations in computer science in the last 20 years.
This guide explains transformers without heavy math — the core ideas, why they work, how they scale, and why they power everything from ChatGPT to text-to-video generation.
The Big Picture
Before transformers: AI struggled with sequences (text, speech, time series). Recurrent neural networks (RNNs) processed one word at a time, slowly forgetting earlier context. Translation, text generation, and language understanding were limited.
After transformers: Models can process entire sentences (or whole documents) in parallel, "attending" to relationships between any pair of words regardless of distance. This unlocked dramatic capability gains.
The breakthrough: the attention mechanism.
What Attention Actually Means
When you read the sentence "The cat sat on the mat because it was tired," your brain knows "it" refers to "cat" — not "mat." You attend to the relevant earlier word.
Attention in neural networks does the same thing computationally. For each word being processed, the model computes how much to "pay attention to" every other word in the input.
Input: "The cat sat on the mat because it was tired"
Word: "it"
Attention weights:
- "The": 0.02
- "cat": 0.78 ← most attention here
- "sat": 0.05
- "on": 0.01
- "the": 0.01
- "mat": 0.10
- "because": 0.01
- "was": 0.01
- "tired": 0.02
The model learns these weights during training. Words with high attention shape the meaning the model assigns to the current word.
Self-Attention vs. Cross-Attention
Two flavors:
Self-attention: Each word attends to every other word in the same sequence. Used to understand a single sentence/document.
Cross-attention: Words in one sequence attend to words in another. Used in translation (output language attends to input language).
Modern LLMs use stacked layers of self-attention. Translation and multimodal models combine self- and cross-attention.
The Transformer Architecture
Imagine a transformer as a tall stack of identical blocks. Each block:
- Takes word embeddings as input (numerical representations of words)
- Runs self-attention (figures out which other words matter for each position)
- Runs a feed-forward neural network (combines the attention output through learned transformations)
- Adds the result back to the input (residual connection — helps training stability)
- Normalizes (keeps numbers in reasonable ranges)
- Passes to the next block
Modern LLMs stack 32-96+ of these blocks. Each layer refines the representation, building deeper understanding.
Why Transformers Beat RNNs
Three reasons:
1. Parallelization. RNNs process word 1, then word 2, then word 3... sequentially. Transformers process all words simultaneously. Massively faster on modern GPU hardware.
2. Long-range dependencies. RNNs forget. Transformers directly connect any two words regardless of distance, so they preserve long-range relationships.
3. Scale. Transformer benefits scale almost linearly with data and parameters. Make it bigger, train on more data → better. This "scaling law" wasn't true for RNNs.
The Encoder-Decoder Setup
Original 2017 transformers had two halves:
Encoder: Reads and understands the input (e.g., English sentence) Decoder: Generates the output (e.g., French translation)
The decoder uses cross-attention to look back at the encoder's understanding while generating each output word.
Variants exist for different tasks:
- Encoder-only (BERT-style): Best for understanding tasks — classification, search
- Decoder-only (GPT-style): Best for generation — chat, text completion. Almost all modern LLMs are decoder-only.
- Encoder-decoder (T5, original Transformer): Translation and seq2seq tasks
How LLMs Generate Text
Modern LLMs like GPT-5 are decoder-only transformers. They generate text one token at a time:
- You provide a prompt
- Model converts prompt to tokens (sub-word units)
- Model embeds tokens into vectors
- Vectors flow through 32-96+ transformer blocks
- Final layer outputs probabilities for what the next token should be
- Sample from those probabilities (with temperature controlling randomness)
- Append the new token to the input
- Repeat until end-of-text token or max length
Each new token influences what comes next. This sequential generation is why LLMs sometimes drift on long outputs.
Positional Encoding
Transformers process all words in parallel — so by default, they have no sense of word order. "Dog bites man" and "Man bites dog" look identical to a transformer without modification.
Solution: positional encoding. Add numbers to each word embedding that encode its position. Modern variants use rotary positional encoding (RoPE) for better long-context handling.
Multi-Head Attention
Real transformers use multi-head attention — running multiple attention mechanisms in parallel, each focusing on different relationships.
Head 1 might attend to: syntactic relationships (subject-verb)
Head 2 might attend to: semantic relationships (entity coreference)
Head 3 might attend to: positional patterns...
Head 32: combines them all
This lets the model capture different kinds of relationships simultaneously.
Scaling Laws
In 2020, researchers discovered that transformer performance follows predictable scaling laws:
Performance ≈ f(parameters, training data, compute)
Specifically: doubling any of these three roughly halves the loss. This insight drove the entire LLM boom — companies kept making models bigger and training on more data, with predictably better results.
Limits exist (data quality, compute costs, diminishing returns), but the basic principle held from GPT-2 (1.5B params) through GPT-4 (>1T effective params) to current frontier models.
Mixture of Experts (MoE) — The 2024 Innovation
Pure transformers compute every parameter for every token — expensive. Mixture of Experts modifies this:
- Train many "expert" sub-networks
- A "router" decides which 1-2 experts to use for each token
- Only those experts compute — rest stay idle
Result: 10x more parameters with similar compute cost. Models like DeepSeek V3 (671B total, 37B active) and Mixtral use this.
Practical Implications
What this means for you:
If you use AI tools: You're using transformers. Understanding them helps you write better prompts (longer context = more attention information available) and interpret results.
If you build with AI: Choose model architecture based on task. Decoder-only (GPT-style) for generation. Encoder-only (BERT-style) for classification.
If you study ML: Transformers replaced almost everything in NLP. Worth studying deeply.
If you're concerned about AI: The same transformer architecture scales to capabilities far beyond current models. Future scaling will likely continue the trajectory.
Inside the Attention Mechanism: Queries, Keys, and Values
The attention mechanism is the engine of transformers. Here's how it actually works mathematically (without heavy notation):
For each word in the sequence, the model computes three vectors:
- Query (Q): "What am I looking for?"
- Key (K): "What relevant information do I have?"
- Value (V): "What should I output if I match this key?"
The model then:
- Scores each (query, key) pair — higher score if they're related
- Normalizes scores to probabilities (so they sum to 1)
- Uses probabilities to weight the values — similar keys get higher weight
- Sums the weighted values → output for this word
Example:
Sentence: "The cat sat on the mat because it was tired"
Processing word: "it"
Query: "What does 'it' refer to?"
Keys: Every word
"cat" has high score (pronouns refer to animals)
"mat" has lower score (mats don't get tired)
Values: Representations of each word
Weighted sum: Dominated by "cat"'s representation
Result: The model understands "it" ≈ "cat"
This happens in parallel across all 1000s of word pairs, guided by learned query/key/value matrices. The model learns what to attend to by training on data where good attention leads to better predictions.
Multi-head attention runs this process many times with different learned Q/K/V matrices. Each head learns to attend to different relationships — some focus on syntax, some on semantics, some on position. The outputs are concatenated and passed forward.
Source: Vaswani et al. (2017), "Attention Is All You Need," NeurIPS 2017 — original transformer paper with full mathematical specification.
Attention Interpretability and Visualization
A major advantage of transformers: you can visualize what the model attends to. This has enabled interpretability research:
Attention heatmaps (showing which input words each output word attends to) reveal that transformers learn linguistically meaningful patterns:
- Pronouns attend to their referents (as above)
- Verbs attend to their subjects and objects
- Adjectives attend to the nouns they modify
Research by Raganathan et al. (2022) and others has shown that attention patterns in well-trained transformers recover syntactic and semantic structure without explicit training for it. The model infers grammar implicitly.
However: attention isn't always interpretable. Attention weights don't perfectly correspond to causal influence (a token with low attention weight might still affect the output through residual connections). Recent work emphasizes that attention is one piece of the computation — not the whole story.
This is important for AI safety: we can't fully trust attention visualizations to explain transformer behavior. Black boxes remain, in a sense.
Fine-Tuning Transformers for Your Task
Pre-trained transformers (GPT, BERT, Llama) are trained on general internet text. To use them for your specific task:
Option 1: Prompt-based (zero-shot or few-shot):
"Classify this review as positive or negative: 'This product is great'"
No training required. Works surprisingly well on modern LLMs but isn't optimal.
Option 2: Fine-tuning (supervised learning):
- Start with pre-trained model weights
- Collect labeled examples for your task (classification, translation, etc.)
- Train the model on your data for a few iterations (100s-1000s of examples)
- Deploy the fine-tuned model
Fine-tuning is powerful because the model already understands language structure from pre-training — you just teach it your specific task.
Option 3: Retrieval-Augmented Generation (RAG):
- Store documents/facts in a searchable database
- For a query, retrieve relevant documents
- Feed query + retrieved context to the transformer
- Model generates answer conditioned on the context
RAG is popular for knowledge-intensive tasks (question-answering over a custom knowledge base) because it lets the transformer access current information without retraining.
Cost of fine-tuning: A 7B-parameter model requires days of GPU time to fine-tune well. A 70B model requires weeks. This is why most practitioners use prompt-based approaches or APIs rather than fine-tuning.
Context Windows and Position Interpolation
Modern transformers have a maximum context window — the longest sequence they can handle. This is a hard limit set during training:
- GPT-3: 4K tokens
- GPT-4: 128K tokens
- Claude 3 Opus: 200K tokens
- Gemini 2.5 Pro: 1M+ tokens
Why is there a limit? The positional encoding mechanism (which tells the model where in the sequence it is) was designed for a specific range. Beyond that range, the model was never trained, so it performs poorly.
Solution: position interpolation — mathematically stretching the positional encoding to handle longer sequences. Techniques like YaRN (Yang et al., 2024) can extend context 2-4x without full retraining. But diminishing returns kick in — models trained on 4K tokens don't perform well at 128K even with interpolation.
This context window limitation shapes how transformers are used: you can't run a model on an entire book or codebase if it exceeds the window. Longer applications require either sliding-window strategies, hierarchical processing, or retrieval-based approaches.
Optimization Techniques: Quantization and Pruning
Training transformers is expensive, but inference (running them) is also expensive. Two main approaches reduce compute:
Quantization: Represent weights with fewer bits (instead of 32-bit floats, use 8-bit or 4-bit integers).
- Reduces memory: 4-bit quantization = 8x smaller
- Reduces compute: integer operations are faster than float
- Trade-off: slight accuracy loss (usually <1% for well-tuned quantization)
Pruning: Remove unimportant weights or neurons.
- Magnitude pruning: remove weights close to zero
- Lottery ticket hypothesis: a random subnetwork can match full network performance if trained
- Trade-off: requires careful tuning; aggressive pruning hurts performance
FlashAttention: Algorithmic optimization that reorganizes attention computation to use GPU memory more efficiently. Speeds up training/inference by 2-4x without accuracy loss.
These optimizations have enabled running large models on consumer GPUs (RTX 4090) or mobile devices, democratizing access.
Prompting Strategies and Their Technical Basis
Prompting language models is partly art, partly science. Why do certain prompting strategies work?
Chain-of-thought prompting ("Let's think step by step"):
- Forces the model to generate intermediate reasoning
- Intermediate tokens serve as context for final answer
- Empirically improves accuracy on reasoning tasks
- Theory: more tokens = more computation in transformer layers to find the answer
Few-shot prompting (include examples in the prompt):
- Examples show the task structure
- Model's attention mechanism learns to match new input to examples
- Works because transformers are in-context learners — they infer task from examples
Instruction tuning (model trained to follow instructions):
- Models fine-tuned on (instruction, output) pairs learn to parse natural language
- Results in GPT-style assistant behavior
- Most modern LLMs use this
Retrieval-augmented prompting (include relevant documents):
- Grounding the model in facts reduces hallucinations
- Reduces dependency on the model's training data (useful for current/proprietary information)
Technical understanding helps: you know why these work, so you can design custom strategies.
Training transformers: the computational challenge and scaling laws
Transformers are expensive to train but follow predictable scaling laws. Training cost scales roughly as:
Cost ∝ Model_Size × Data_Size × Compute
GPT-2 (1.5B parameters, 2019) cost ~$50K to train on standard hardware. GPT-4 (>1T effective parameters, 2023) cost an estimated $100+ million in compute. This exponential climb is what makes AI research increasingly capital-intensive and why a handful of companies (OpenAI, Google, Meta) dominate.
Scaling laws (Kaplan et al., 2020; Hoffmann et al., 2022) describe how performance improves with scale:
- Doubling model size improves loss (error) by ~13%
- Doubling training data improves loss by ~7-8%
- Doubling compute budget improves loss by ~8%
These laws hold from tiny models (100M parameters) to enormous ones (100B+), suggesting no hard ceiling — just economics. This predictability is why companies invest billions: they know larger models will be better.
The implication: transformer architecture is fundamentally scalable. Bigger and better might just be a matter of spending more. Some argue this path leads to AGI; others argue you eventually hit diminishing returns or need architectural innovations.
Beyond text: transformers everywhere
The architecture isn't limited to language:
- Vision Transformers (ViT): Image classification, replacing CNNs in many cases
- DALL-E 3, Sora: Image and video generation using transformer diffusion
- AlphaFold 2: Protein structure prediction (2020 Nobel Prize in Chemistry, 2024)
- Speech recognition (Whisper): Audio-to-text with remarkable robustness
- Multimodal models: Process text, images, audio simultaneously in a single model (GPT-4 Vision, Gemini, Claude)
- Graph Transformers: Molecule design, knowledge graph reasoning
- Music generation: Transformers trained on MIDI or audio spectrograms
The transformer is now the universal architecture for sequence modeling — and it turns out most data is sequential or can be represented as sequences. This universality is partly why the field converged on transformers so rapidly.
Python Example: Using a Transformer
from transformers import pipeline
# Sentiment analysis with BERT
classifier = pipeline("sentiment-analysis")
print(classifier("I love this product"))
# [{'label': 'POSITIVE', 'score': 0.9998}]
# Text generation with GPT-2
generator = pipeline("text-generation", model="gpt2")
print(generator("The future of AI is", max_length=50)[0]['generated_text'])
# Translation
translator = pipeline("translation_en_to_fr")
print(translator("Hello, how are you?"))
Hugging Face's transformers library makes using pre-trained transformers trivial.
Why Transformers Replaced RNNs and LSTMs
Before transformers, sequence modeling belonged to recurrent neural networks (RNNs) and their more sophisticated cousin, the LSTM (long short-term memory network). These architectures process a sequence one token at a time, carrying a hidden state forward from step to step — token 1 updates the hidden state, which feeds into processing token 2, and so on. This design has two structural weaknesses that transformers were built to fix.
The first is the vanishing gradient problem. As an RNN processes a long sequence, the influence of early tokens on the hidden state gets diluted with each subsequent step, and during training the gradient signal used to update the network's weights shrinks exponentially as it's backpropagated through many time steps. LSTMs added gating mechanisms — specialized structures that decide what to keep, forget, and output at each step — specifically to slow this decay, and they worked well enough to dominate NLP through the mid-2010s. But even LSTMs struggled to reliably connect information across sequences longer than a few hundred tokens, which limited their usefulness for tasks like document summarization or long-form question answering.
The second, more consequential weakness is that recurrence is inherently sequential: you cannot compute the hidden state for token 50 until you've computed it for tokens 1 through 49. This makes RNN training difficult to parallelize on GPUs, which are built to perform many independent calculations simultaneously. Transformers eliminate the sequential dependency entirely — self-attention lets every token attend to every other token in a single matrix operation, which means an entire sequence can be processed in parallel during training. This parallelizability, more than any single accuracy improvement, is why transformers scaled to the enormous training runs (trillions of tokens, across thousands of GPUs) that produced today's large language models — an RNN-based approach at that scale would have taken orders of magnitude longer to train, even with equivalent compute budgets.
RNNs and LSTMs haven't disappeared entirely; they still show up in latency-constrained edge applications and some time-series forecasting pipelines where a transformer's quadratic attention cost (compute grows with the square of sequence length) is a genuine liability. But for the vast majority of modern sequence modeling — language, code, audio, and increasingly vision — the parallelizability and long-range modeling of self-attention has made the transformer the default starting point.
Source: Hochreiter, S. & Schmidhuber, J. (1997). "Long Short-Term Memory." Neural Computation 9(8): 1735–1780 — the original LSTM paper. Source: Vaswani, A., et al. (2017). "Attention Is All You Need." NeurIPS — explicitly motivates the transformer as a parallelizable alternative to recurrence.
Frequently Asked Questions
Q: Why are transformers so expensive to train?
A: Attention complexity is O(n²) — for n tokens, you compute n² attention scores. A 10K-token sequence requires 100M attention computations. This becomes bottlenecking for long sequences and enormous models. Variants like FlashAttention and sparse attention reduce this cost by 3-10x through clever mathematics.
Q: Will transformers be replaced?
A: Possibly eventually, though not soon. Active research areas include state-space models (Mamba, S4), retrieval-augmented architectures, mixture-of-experts variants, and hybrid approaches. None has decisively beaten transformers yet across the full spectrum of tasks. Transformers' flexibility and scaling properties make them hard to dethrone.
Q: How long is "long context" and why does it matter?
A: GPT-4: 128K tokens. Claude 3: 200K tokens. Gemini 2.5: 1M+ tokens. Each represents roughly 75-300 pages of text. Long context matters because models can process entire documents, books, or codebases at once, enabling tasks like "analyze this 100-page legal contract" or "refactor this entire codebase."
Q: How much compute does it take to train a transformer?
A: Tiny model (100M params): ~1 GPU-day. Medium (7B params): ~1000 GPU-days. Large (70B params): ~50,000 GPU-days. Frontier (100B+ params): Millions of GPU-days and $10-100M+ in compute cost. This scaling is why AI research has become capital-intensive.
Q: Are transformers conscious or intelligent?
A: No to consciousness — transformers are sophisticated pattern matchers with no awareness or subjective experience. On intelligence: they excel at narrow tasks (text prediction, pattern matching) but lack general reasoning, embodied understanding, or causal inference. They're not conscious, but they're remarkably capable at specific jobs.
Q: Where can I learn transformers technically?
A: "The Illustrated Transformer" (Jay Alammar's blog), Andrej Karpathy's YouTube tutorials on building GPT from scratch, the original "Attention Is All You Need" paper, and Sebastian Raschka's Machine Learning textbook. Hands-on coding in PyTorch or JAX is the best way to understand them.
Read next:
Follow new articles
Add SciHub101 to your feed reader to see new science and technology stories.
About the Author
SciHub101 is an independent science and technology publication founded and edited by Buğra Sözeri, a software engineer and app developer based in Türkiye. AI tools assist with drafting and research. Source documentation in the existing archive is uneven, and articles are being reviewed and corrected. The Editorial Team byline identifies editorial responsibility; it does not certify specialist or independent human review. Where we compare products, anything built by our own team is labelled as such — see the editorial guidelines and About page for how corrections and disclosures work.
Related Articles

XGBoost Explained — Why It Wins Kaggle Competitions
XGBoost explained: how gradient boosting works, why it dominates tabular ML, a full Python tutorial, hyperparameter tuning, and LightGBM/CatBoost comparison.

MLOps for Beginners — Deploy ML Models to Production
MLOps for beginners: what it is, why it matters, key tools (MLflow, Kubeflow, Weights & Biases), and how to take ML models from notebook to production reliably.

How Large Language Models Work: Transformers, Attention, and Why They Hallucinate
Meta trained Llama 3 on more than 15 trillion tokens using 7.7 million H100 GPU-hours. Here is how next-token prediction at that scale turns into a conversation partner.

AI Training vs. Inference: What Actually Happens When AI Learns and Thinks
Training and inference are two fundamentally different computational processes. Understanding the distinction explains most of how AI systems work and cost.

How Does ChatGPT Actually Work? A Plain-Language Guide to LLMs
ChatGPT predicts the next word based on patterns it learned from billions of pages of text. Sounds simple — but it produces essays, code, and reasoning that look uncannily human. Here's what's really happening.

What is Machine Learning? Simple Guide for Beginners
Machine learning explained simply — no jargon. How computers learn from data, real-world examples (spam filter, Netflix, fraud detection), why it matters.