How AI works
What is a transformer in AI? Attention explained simply
The transformer is the neural network design behind ChatGPT, Claude and Gemini. What attention and self-attention mean, how a transformer processes text, and why it replaced older models.
Short answer
A is a type of , introduced in the 2017 paper "Attention Is All You Need", that powers almost every modern large language model. Its key idea is attention: for every word, the model looks at all the other words in the text and decides which ones matter most for understanding it. Because it processes all the words at once instead of one by one, it trains fast on huge amounts of data.
The problem transformers solved
Language is full of references. In "The trophy didn't fit in the suitcase because it was too big", you know "it" is the trophy. Change "big" to "small" and "it" becomes the suitcase. Understanding a word means looking at the right other words, sometimes far away.
Before 2017, the best language models were recurrent neural networks that read text one word at a time, carrying a running summary. They were slow to train, because each step waited for the last, and they tended to forget details from far back.
Attention, in plain English
Attention lets every word look directly at every other word and pull in information from the ones that matter. For each word the model asks, roughly: "which other words help me understand this one, and how much?"
Under the hood each word, as an embedding, produces three vectors:
- a query: what I'm looking for,
- a key: what I contain, for others to match against,
- a value: the information I'll pass on if chosen.
Each word's query is compared with every key to get attention scores, the scores become that add up to 1, and the word takes a weighted mix of the values. So "it" can put most of its attention on "trophy" and absorb that meaning. When the words attend to others in the same text, it's called self-attention.
See it move in the transformers lesson, which has an attention demo.
Inside a transformer
- Tokens in. Text is split into tokens and each becomes an embedding. Position information is added so the model knows word order. See what are tokens.
- Multi-head attention. Several attention "heads" run side by side, each free to track a different kind of relationship, such as grammar, references or topic.
- Feed-forward layer. A small neural network processes each position on its own, where much of the model's stored knowledge is thought to live.
- Repeat. These blocks are stacked dozens of times. Each layer refines the meaning a bit more.
- Predict. At the end, the model scores every token in its vocabulary for what comes next.
Models like GPT, Claude, Llama and Gemini use the "decoder" style, where each token can only attend to earlier ones, which suits writing text left to right. That's the loop described in how ChatGPT works.
Why transformers took over
- Parallel training. All tokens are processed at once, which suits GPUs and made training on trillions of tokens practical.
- Long-range understanding. Any word can connect directly to any other, however far apart.
- They scale. Bigger transformers trained on more data kept getting better, which led to today's LLMs.
- Not just text. The same design works for images (vision transformers), audio, code, proteins and multimodal models.
Key takeaways
- The "T" in GPT stands for transformer.
- Attention lets each word gather meaning from the words most relevant to it.
- Transformers stack many layers of attention plus small feed-forward networks.
- Processing in parallel made it possible to train on internet-scale text.
Learn it properly, free
- Attention is all you needAdvanced · 8 min
- Meet the chatbots: LLMsBeginner · 6 min
- How LLMs are trainedAdvanced · 7 min
- Mixture of experts: big models, small billsAdvanced · 8 min
Terms in this post
Questions
What does GPT stand for?
Generative Pre-trained Transformer: a model that generates text, is pretrained on a large amount of data, and uses the transformer architecture.
What is attention in AI?
Attention is a mechanism that lets a model weigh how relevant every other part of the input is when processing each part. It lets each word take in meaning from the words that matter most to it.
What is the difference between attention and self-attention?
Self-attention is attention where the words of one text attend to each other. Attention can also connect two different sequences, such as a sentence and its translation.
Who invented the transformer?
A team at Google, in the 2017 paper "Attention Is All You Need" by Vaswani and colleagues. It was first designed for translation.
Sources and further reading
- Attention Is All You Need · Vaswani et al., 2017
- The Illustrated Transformer · Jay Alammar
- Neural networks (includes transformers) · 3Blue1Brown · Free videos
- Let's build GPT: from scratch, in code, spelled out · Andrej Karpathy · Free video