Cloud Engineer Lab
Cloud Engineer Lab
Cloud Engineer Lab
Cloud Engineer Lab
© 2026
Transformers Explained Visually: The Architecture Behind Modern AI

Transformers Explained Visually: The Architecture Behind Modern AI

The T in ChatGPT stands for Transformer. A picture-first, plain-language guide to attention, queries, keys, values and why one 2017 idea changed AI.

17 min read
Share

ChatGPT, Claude, Gemini, Copilot, Google Translate, the AI that writes captions for videos, even the AI that predicts the shape of proteins. They look like completely different tools, but almost all of them are built on the same invention: the transformer.

It's even in the name. ChatGPT stands for Chat Generative Pre-trained Transformer.

This post explains what a transformer is, using pictures first and words second. There's no maths you need to follow. Where a formula helps, it sits in an optional box you can skip.

Read this first, or after

This is a companion to From Text to Tokens to Answers, which follows one sentence through an AI model step by step. That post shows what happens. This one zooms in on the design that makes it work. Either order is fine.

These charts use real measurements

The attention charts in this post weren't drawn by hand. They come from running BERT, a well-known open transformer model from Google (110 million settings, 12 layers), on a laptop and reading its actual attention values.

The Problem Transformers Solved

To understand why transformers were such a big deal, you need to know how AI read text before them.

Until 2017, the best language AIs were recurrent neural networks (RNNs). They read a sentence the way you'd read through a keyhole: one word at a time, from left to right, carrying a small "memory" from each word to the next.

Diagram comparing an RNN reading six words one at a time in a chain with a transformer connecting all six words to each other at once
Left: older models passed information along a chain, one word per step. Right: a transformer connects every word to every other word in one go.

That approach had two big problems:

  1. It was slow to train. Word 6 can't be processed until word 5 is done, which can't start until word 4 is done. Computers with thousands of parallel processors (GPUs) mostly sat waiting.
  2. It forgot things. Information from early words faded as it was passed along the chain, a bit like a game of telephone. In a long paragraph, the start was often lost by the end.

In 2017, a team of eight researchers at Google published a paper with a bold title: Attention Is All You Need. Their idea: throw away the chain completely. Let every word look directly at every other word, all at the same time. They called the design the transformer.

The Big Idea in One Sentence

A transformer lets every word in a sentence look at every other word, decide which ones matter, and update its own meaning based on them, all in parallel.

That "decide which ones matter" part is called attention. Everything else in a transformer exists to support it.

Step 1: Words Become Numbers (Embeddings)

Computers only work with numbers, so the first step is to turn each word (or piece of a word, called a token) into a list of numbers called an embedding.

You can picture an embedding as a location on a giant map of meaning. Words with similar meanings are placed close together: "cat" near "dog", "server" near "computer", "restart" near "reboot". Nobody draws this map by hand. The AI learns it by reading huge amounts of text. The previous post measured some of those distances in a real model.

In the original 2017 transformer, each word's list had 512 numbers. In BERT it's 768. In today's largest models it's many thousands.

Adding position: who came first?

Here's a catch. Because a transformer looks at all words at once rather than in a chain, it has no natural sense of order. Without help, "dog bites man" and "man bites dog" would look identical.

The fix is to add a second signal to every word: a kind of seat number that tells the model where the word sits in the sentence. This is called positional encoding. The original transformer created those seat numbers from smooth wave patterns (that's what the wavy lines on the cover image represent). Many modern models use a newer method that "rotates" the numbers slightly depending on position, but the purpose is the same.

Optional maths: the original position formula

The 2017 paper gave each position a pattern of sine and cosine waves of different speeds:

text
PE(pos, 2i)   = sin( pos / 10000^(2i / d_model) )
PE(pos, 2i+1) = cos( pos / 10000^(2i / d_model) )

pos is the word's position, i counts through the 512 numbers, and d_model is 512. Slow waves tell the model roughly where it is in a long text; fast waves tell neighbouring words apart. (The cover image shows this formula with 1000 instead of 10000, a small typo in the picture.)

Step 2: Attention, the Heart of the Transformer

Read this sentence:

The animal didn't cross the street because it was too tired.

What does "it" mean? The animal, obviously. Now change one word:

The animal didn't cross the street because it was too wide.

Now "it" means the street. You worked that out instantly by looking at the other words for clues. That is exactly what attention does.

We ran both sentences through BERT and looked at where the word "it" was focusing. Here's one of its attention units (called a head) in action:

Two bar charts from BERT layer 7 head 11. With the word tired, it focuses 28 percent on animal and 7 percent on street. With the word wide, it focuses 1 percent on animal and 59 percent on street
Measured in BERT. Change one word at the end, and the word "it" shifts its focus from "animal" to "street".

With "tired", "it" puts 28% of its focus on "animal" and only 7% on "street". With "wide", that flips completely: 1% on "animal" and 59% on "street". Nobody programmed a rule about tired animals or wide streets. The model learned it from reading.

Being honest about this example

We checked all 144 attention heads in BERT. This one (layer 7, head 11) is the clearest; only 22 of the 144 shift their focus in the right direction at all. That's normal. Each head has its own job, and only a few specialise in working out what "it" refers to. Attention charts are a useful window into a model, not a complete explanation of it.

Step 3: Queries, Keys and Values

So how does a word decide where to focus? This is where three terms come in that you'll see in every explanation of transformers: query, key and value.

The easiest way to understand them is a library:

  • Query: what you're looking for. You walk in and ask, "I need something about animals."
  • Key: the label on each book's spine. You compare your question with every label.
  • Value: what's actually inside the book. Once you know which books match best, you read mostly from those.

In a transformer, every word plays all three roles at once. From its own list of numbers, each word creates:

  • a query: "what kind of information am I looking for?"
  • a key: "here's what I can offer to others"
  • a value: "here's the information I'll share if you pick me"

Then, for each word:

  1. Ask. Take its query.
  2. Match. Compare that query with the key of every word. A good match gives a high score.
  3. Weigh. Turn the scores into percentages that add up to 100%.
  4. Blend. Mix together the values of all the words, using those percentages. That blend becomes the word's new, context-aware meaning.
Flow diagram: the word it sends a query, which is compared with the keys of animal, street and it; the scores become 58, 14 and 28 percent; the values are blended into a new meaning for it
The four moves of attention. The word "it" asks a question, checks every word's label, and builds a new meaning mostly from "animal". Toy numbers, worked out in the optional box below.

After this step, "it" is no longer just the generic word "it". It's now closer to "it, meaning the animal". That's the whole trick.

Optional maths: the attention formula, worked by hand

The whole of attention fits in one line:

text
Attention(Q, K, V) = softmax( Q · Kᵀ / √d ) · V

Here's the same thing with tiny made-up numbers, using lists of just 2 numbers instead of hundreds:

WordKeyValueQuery · Key÷ √2Weight (softmax)
animal[2, 1][1, 0]21.4157.6%
street[0, 2][0, 1]0014.0%
it[1, 1][0.5, 0.5]10.7128.4%

The query for "it" is [1, 0]. Multiplying it with each key (a dot product) gives the scores 2, 0 and 1. Dividing by √2 (the square root of the list length) keeps the numbers in a sensible range. Softmax turns them into percentages. The new "it" is the weighted blend of the values: 0.576 × [1, 0] + 0.140 × [0, 1] + 0.284 × [0.5, 0.5] = [0.718, 0.282], mostly "animal".

Why divide by √d at all? Without it, the scores grow as the lists get longer, and softmax starts giving almost everything to one word. Even in this tiny example, skipping it would push "animal" from 57.6% to 66.5%. With 64 or 128 numbers per head, the effect is far bigger.

Step 4: Many Heads, Many Points of View

One round of attention can only focus on one kind of relationship at a time. So a transformer runs several attention heads side by side, each with its own query, key and value settings. This is called multi-head attention.

Think of a team reading the same document, each with a different highlighter: one marks grammar, one marks who-did-what, one marks which nouns go together. Afterwards, their notes are combined.

The original transformer used 8 heads per layer. BERT uses 12. Very large models use 64 to 128 or more. Here are four real heads from BERT reading a short sentence, each doing a visibly different job:

Four attention heatmaps from BERT for The cat sat on the mat: one head looks at the previous word, one at the next word, one links cat and mat, and one parks attention on the end marker
Four real heads, four jobs. Darker squares mean more focus. Rows are the word doing the looking; columns are the word being looked at.
  • Layer 4, head 6 mostly looks at the word before.
  • Layer 3, head 10 almost always looks at the word after.
  • Layer 2, head 4 links "cat" and "mat" across the sentence ("mat" puts 34% of its focus on "cat", and "cat" puts 41% on "mat"), even though four words sit between them.
  • Layer 12, head 8 sends nearly everything to [SEP], a special marker at the end of the input. This looks useless, and in a sense it is: heads with nothing useful to do learn to "park" their attention on a marker like this.

The cover image's attention grid

The big grid on the cover shows the same idea: rows and columns are the words of "The cat sat on the mat", and brighter squares mean more attention. The numbers in that picture are illustrative. The ones in this post are measured.

Step 5: The Rest of the Building Block

Attention is the star, but a transformer layer (also called a block) has a few more parts. Here's the full design, the same one used in the GPT family of models behind most chatbots:

Vertical diagram of a transformer: text becomes tokens and embeddings with position, passes through a repeated block of multi-head self-attention, add and normalise, feed-forward network, add and normalise, then every word in the vocabulary is scored and the next word is chosen and fed back in
One transformer block: attention, then a thinking step, each with a shortcut and a clean-up. Stack the block 6 to 100+ times, then score the next word.

Inside each block:

  1. Multi-head self-attention. Words share clues with each other (Steps 2 to 4).
  2. Add & normalise. The result is added to what the word already knew, rather than replacing it, and the numbers are rescaled to stay in a stable range.
  3. Feed-forward network. Each word now "thinks" on its own for a moment, processing what it has just gathered. In the original transformer this step briefly expands each word's 512 numbers to 2,048 and back. Much of a model's general knowledge is believed to live in these parts.
  4. Add & normalise again.

Those "add" steps are shortcuts (the purple lines in the diagram). They mean each block only has to add a little extra understanding on top of what's already there, like adding notes to a notepad instead of rewriting it from scratch. Without them, very deep stacks of blocks would be almost impossible to train.

Then the block is repeated. The original transformer stacked 6 of them. BERT has 12. GPT-3 has 96. With each layer, the words' meanings get richer: early layers tend to pick up grammar and nearby words, later layers pick up meaning and relationships across the whole text.

At the very top, the model scores every possible next word and turns the scores into percentages, exactly as traced in the previous post.

Three Kinds of Transformer

The original 2017 transformer had two halves, because it was built to translate between languages:

  • an encoder, which reads the whole input sentence (for example, in English) and builds an understanding of it;
  • a decoder, which writes the output (for example, in German) one word at a time, looking back both at what it has written so far and at the encoder's understanding.

Later models kept one half, the other, or both:

TypeHow it readsGood atFamous examples
Encoder-onlySees the whole text at once, in both directionsUnderstanding: search, classification, spotting meaningBERT (2018)
Decoder-onlyOnly looks backwards, never at later wordsWriting: chat, code, essaysGPT models, Claude, Llama
Encoder-decoderReads with one half, writes with the otherTurning one text into another: translation, summariesThe original transformer, T5

That table explains something about our "tired" and "wide" example. We used BERT because it reads in both directions, so "it" can see the word "tired" that comes after it. A decoder-only chatbot model can't: when it reaches "it", the word "tired" hasn't happened yet. Instead, it sorts out the meaning a moment later, when "tired" arrives and looks back at "animal".

Why Transformers Changed Everything

Transformers didn't just do translation a bit better. They changed what was possible.

1. They train fast on modern hardware. Because every word is processed at the same time, transformers make full use of GPUs, which are built to do thousands of calculations in parallel. The original base model reached top translation quality after about 12 hours on 8 GPUs, a fraction of the time older approaches needed.

2. They connect distant words directly. "Cat" and "mat" are one step apart, no matter how much text sits between them. No more game of telephone.

3. They get better when you make them bigger. This turned out to be the most important property of all. More data, more layers and more computing power kept producing better models, year after year. That's how we went from 65 million settings in the 2017 base model, to 1.5 billion in GPT-2 (2019), to 175 billion in GPT-3 (2020), and on to today's chat assistants.

4. They work on more than words. Anything that can be cut into pieces can go through a transformer. Images are cut into small squares (Vision Transformers, 2020). Audio is cut into short slices (speech recognition tools such as OpenAI's Whisper). Google DeepMind's AlphaFold 2, which predicts the 3D shapes of proteins, relies heavily on attention too.

The catch: attention gets expensive

Because every word looks at every other word, the work grows with the square of the text length. 1,000 tokens means about a million word-to-word comparisons in each head of each layer. 100,000 tokens means about ten billion. That's why long documents cost more and run slower, and why a lot of current engineering goes into smarter ways of computing attention. It's also part of why running models efficiently has become such a big topic.

A Few Common Misunderstandings

You might hearWhat's actually true
"Attention means the AI understands like a human."Attention is a way of mixing information between words. It's powerful, but it's arithmetic, not awareness.
"Each attention head has one neat, clear job."Some do (like the "word before" head above). Many do several things at once, or nothing obviously useful.
"Transformers are only for chatbots."They power translation, search, speech recognition, image tools, code assistants and science models too.
"Bigger context windows are free."Every extra word adds comparisons with every other word. Long inputs cost more time and money.
"The transformer was invented for ChatGPT."It was invented in 2017 for translation. ChatGPT arrived five years later, built on the decoder half.

The Original 2017 Transformer at a Glance

For readers curious about the numbers on the cover image:

SettingOriginal "base" transformer
Built forTranslating English to German and English to French
Layers6 in the encoder, 6 in the decoder
Numbers per word512
Attention heads8 per layer, each working with 64 numbers
Feed-forward size2,048
Total settingsAbout 65 million
TrainingAbout 12 hours on 8 GPUs (a larger version took 3.5 days)

The Short Version

IdeaIn plain words
EmbeddingsEach word becomes a location on a map of meaning
Positional encodingEach word gets a seat number so order isn't lost
AttentionEach word looks at the others and decides which matter
Query, key, valueThe question a word asks, the label it shows, and the information it shares
Multi-head attentionSeveral "readers" with different highlighters, working side by side
Feed-forward + shortcutsA thinking step for each word, added on top of what it already knew
LayersThe whole block repeated, building richer meaning each time
Why it matteredFast, parallel training; direct links between distant words; better with scale

Before 2017, AI read text through a keyhole, one word at a time. The transformer opened the door: every word can see every other word, all at once, and decide for itself what matters. Nearly every AI tool you use today grew out of that one idea.

Curious what happens around the model when you actually press Enter? Follow the journey of a prompt from browser to GPU. Want to go further? See how this engine is used in practice in the new AI application stack, or how embeddings power document search in RAG From Scratch.

CChetan Yamger

Written by

Chetan Yamger

Cloud Engineer · AI Automation Architect · Modern Workplace Consultant

Cloud Engineer, AI Automation Architect, and Modern Workplace Consultant based in Amsterdam, Netherlands. Specializing in scalable, secure enterprise solutions with Microsoft Azure, Intune, PowerShell, and AI-driven automation using ChatGPT, Gemini, and modern LLM technologies.

Cloud & Modern WorkplaceMicrosoft Intune & MDMAzure & Microsoft 365AI AutomationPrompt EngineeringPowerShell & Graph APIWindows AutopilotConditional Access & Zero TrustSCCM / MECM & MSIXVDI / WVDPower BINode.js & Next.js
Newsletter

Stay in the loop.
New articles, straight to you.

Deep-dive technical articles on Intune, PowerShell, and AI — no noise, no spam.

New article notifications
No spam, ever
Free forever

Discussion

Share your thoughts — your email stays private

Leave a comment

0/2000

Your email is used to prevent spam and will never be displayed.