SevyDevy

Module 2 · How a language model thinks · Lesson 8 of 32

Attention and transformers, without maths

12 minFree GenAI course · BeginnerLesson 2.4By Saket Bhatnagar

Researched and drafted with AI, reviewed and edited by Saket Bhatnagar.

In short

  • Attention lets every word look at the other words and decide which ones matter for its meaning.
  • A transformer is a stack of layers, each with an attention step and a small thinking step, repeated dozens of times.
  • Transformers handle all the words in parallel, which made training on huge text practical. They power modern chat AI.
Soon

Video lesson · coming soon

Attention and transformers explained without maths

Lala, your AI mentor, will walk you through this lesson in a short animated video. Until then, everything is here to read and try.

  1. 01 “Put it in the fridge”
  2. 02 Listening for your own train
  3. 03 Who does “it” look at?
  4. 04 A transformer is a stack
  5. 05 2017: attention is all you need
  6. 06 The whole journey of a message
  7. 07 Tap the words yourself

Late one night, Priya’s nani posts on the family WhatsApp group: “Dal is on the stove. Put it in the fridge once it cools.” Nobody replies, “Wait, put the stove in the fridge?”

Everyone knows “it” means the dal. Nobody looked up a grammar rule. Their eyes went back to the right word in a split second. That quick glance back, picking out the words that matter, is what AI researchers call attention.

Why words need each other

Many words only make sense with their neighbours. “Sharp” means one thing in “a sharp knife”, another in “a sharp student”, and another in “8 o’clock sharp”. “It” means nothing at all until you know what it points to.

In the last lesson, every token got an embedding: a starting spot on the map of meaning. But that starting spot is the same every time. “Bank” gets one vector whether you are sitting on a river bank or applying for a bank loan. The model needs a way for each word to update its meaning using the words around it.

Here is the whole move, without maths. Each word holds up its query. It compares that query with every word’s key and gets a score for each. The scores are turned into shares that add up to 100%. Then the word takes a blend of everyone’s values, mostly from the high scorers. Its meaning is now updated by its context.

Try it: who does “it” look at?

This classic test sentence trips up anything that reads words one by one without context: “The trophy did not fit in the bag because it was too big.” You know “it” is the trophy. Change one word, “…because it was too small”, and “it” is suddenly the bag.

Try it yourselfWhere does each word look?Tap any word to see which words it pays the most attention to, then switch to the “too small” ending and tap “it” again.
  • With the “too big” ending, tap “it”. The strongest share, about 58%, goes to “trophy”.
  • Switch to “too small” and tap “it” again. Now most of its attention moves to “bag”.
  • With each ending, tap the last word, “big” or “small”. It looks back at the object it describes.
  • Tap a small word like “in” or “did”. It mostly looks at itself and its neighbours.

Many heads, many layers: what a transformer is

One round of attention is one glance. Real models take many glances side by side, each with its own query, key and value. Researchers have found some that seem to track which word a pronoun refers to, some that link a verb to its subject, and some that watch nearby words. Many others do jobs nobody has neatly named.

After the words have gathered information from each other, each word goes through a small step on its own, called a feed-forward network. You can think of attention as gathering notes from classmates, and the feed-forward step as sitting quietly and making sense of them. Much of what the model knows about the world seems to be stored in these steps.

Inside a transformer, one layer at a time

Tokens become embeddings with a position marker, then pass through many layers of attention and feed-forward steps, and the final vectors become next-token chances.

Diagram connections:

  • Token IDs to Embeddings + position (look up)
  • Embeddings + position to Attention (enter)
  • Attention to Feed-forward (then)
  • Feed-forward to Attention (next layer)
  • Feed-forward to Next-token chances (after last)
  • Token IDs: The numbers from the tokenizer.
  • Embeddings + position: Each token’s meaning vector, plus a marker for where it sit…
  • Attention: Every token gathers information from the tokens that matter…
  • Feed-forward: Each token processes what it gathered, on its own.
  • Next-token chances: The final vectors are turned into probabilities, as in the…

Notice the position marker. Attention on its own has no sense of order, so “dog bites man” and “man bites dog” would look the same. Adding a position signal to each embedding lets the model tell them apart.

Then the same kind of block is stacked again and again: dozens of layers in large models, sometimes more than a hundred. Roughly speaking, early layers pick up local patterns like word pieces and grammar, and later layers build more abstract meaning. By the top, “it” in our sentence carries a strong dose of “trophy”, and “bank” in “bank loan” has moved far from the river.

Why transformers took over

Before 2017, popular language models were recurrent neural networks. They read text one word at a time, carrying a running summary forward, a bit like passing a note down a long row of students. By the end of a long paragraph, the early words had faded. And you could not speed them up much, because each step had to wait for the one before.

In 2017, a team of researchers at Google published a paper called “Attention Is All You Need”. They dropped the word-by-word reading and built the model from attention alone. Every word could look directly at every other word, and during training all the words could be processed at once.

That “all at once” design fit GPUs perfectly. Training on far larger piles of text became practical, and the models got far better. The “T” in GPT stands for transformer: generative pre-trained transformer. Today, transformers or close cousins power most chat AI, translation tools and many image and speech models too.

Older word-by-word models vs transformers
Older recurrent modelsTransformers
How they read during trainingOne word after anotherAll words at once
Links between far-apart wordsEarly words fadeAny word can look at any other directly
Training on GPUsSlow; steps wait in lineFast; work runs in parallel
Weak spotForgets long passagesAttention work grows fast as text gets longer

That last row matters. Because every token looks at every other token, doubling the length of the text roughly quadruples the attention work. It is one reason every model has a limit on how much text it can take in at once. You’ll meet that limit, the context window, later in this module. Engineers have found many clever tricks to soften that cost, but it never fully goes away.

Check yourself

Check yourself

In “The trophy did not fit in the bag because it was too small”, what should attention help “it” connect to?

Pick an answer to see why it is right or wrong.

Check yourself

What made transformers much faster to train than older models that read one word at a time?

Pick an answer to see why it is right or wrong.

Check yourself

Arjun is drawing one transformer block on a whiteboard. What two parts should it have?

Pick an answer to see why it is right or wrong.

Remember these three

  1. Attention lets each word score every other word and blend in meaning from the ones that matter.
  2. A transformer stacks many layers of attention plus a feed-forward step, with a position marker on each token.
  3. Working on all words in parallel made transformers fast to train, but long texts get costly.

Questions people ask

What is attention in AI, in simple words?
Attention is the step where each word in a text checks every other word and decides which ones matter for its meaning. It then mixes in information from those words. That is how a model knows “it” means the trophy in one sentence and the bag in another.
What is a transformer in AI?
A transformer is a kind of neural network made of many stacked layers. Each layer has an attention step, where words look at each other, and a feed-forward step, where each word processes what it gathered. Almost every modern chat AI is built on this design.
What does GPT stand for?
GPT stands for generative pre-trained transformer. Generative means it creates text, pre-trained means it first learned from a huge amount of text, and transformer is the design described in this lesson.
Do I need maths to understand transformers?
Not for the core idea. Words score each other, blend in what matters, and repeat that over many layers. Engineers who build models use linear algebra, but you can reason about what transformers do, and their limits, without it.