Animish Sharma

"Attention is all you need" explained like you are a 5 yo

Attention is all you need is a breakthrough paper by researchers at Google and collaborators in 2017. It introduced an architecture called the Transformer. Let's understand this in a simpler way.1

The core idea is instead of passing information through a sentence one word at a time, let words look at other words and figure out which ones matter. This is attention, in a basic sense.2

For eg, "the dog is drinking water because it is tired", now suppose AI wants to understand what "it" refers to here.

For us, it is the dog. Water is not sitting there exhausted after a long day. But how do we get a computer to make this connection?

Let's start from the prerequisites.

Embedding

A computer does not understand English the way we do. It works with information represented as numbers. So before a neural network can do anything useful with our sentence, we have to give it a numerical representation. This is where embeddings come in.3

Suppose our sentence is "The cat ate fish".

We can represent each word using a list of numbers. This list is called a vector. Suppose:

cat=[0.20.80.1]

and

fish=[0.70.10.5]

These are made-up numbers for our example.

Now you might ask, why these numbers? Why is cat not represented by some other numbers?

It could be. In a model trained from scratch, the embedding numbers usually start randomly and get adjusted during training. The model learns representations that are useful for its task. We do not manually write down the correct numbers for every word.3

Also, do not assume that 0.2 means "has whiskers" and 0.8 means "likes milk". Think of the whole list as a description written in a numerical language, rather than each number having a neat English label.

One small detail: models actually work with tokens, which can be words, parts of words, or punctuation. A tokenizer splits the text into these pieces and assigns them IDs. The embedding step then looks up a vector for each ID. For this blog, let's pretend every word is one token.4

So our starting point is:

Text → tokens → IDs → vectors.

Positional encoding

Now we have numbers for our words. But we have another problem.

Look at these two sentences:

"The cat ate fish."

"Fish ate the cat."

Same words, very different situation for the cat.

Attention needs information about word order. In the original Transformer, a position vector is added to each word's embedding before it enters the layers. This is called positional encoding.5

Think of it like children wearing name tags and sitting in numbered seats.

The name tag says who you are. The seat number says where you are.

Similarly, we give the model both the word's representation and its position:

input vector=word embedding+position vector

The position is represented by another list of numbers, not just a single seat number. But the seat-number example is the basic idea.

Self-attention

Let's return to our sentence:

"The dog is drinking water because it is tired."

Suppose we are working on the word "it".

Its starting embedding represents the token "it", but that alone does not tell us which thing we are talking about. We need information from the sentence.

Self-attention lets a word's representation gather information from other positions in the same sequence, including its own position. That is why it is called self-attention: the sentence is looking within itself.5

Imagine "it" asking the other words:

Which of you can help explain what I am doing here?

In a useful attention pattern, "dog" might contribute more than "water".

But how do we calculate who contributes what?

For this, we need three things: Query, Key and Value.

Query, Key and Value

The names sound more complicated than the idea.

Imagine you are in a classroom and need a red pencil.

Your request is: "Who has a red pencil?"

Each child shows a label describing what they have.

After finding a useful match, you get something from that child.

The request is like a Query. The label you compare it with is like a Key. The thing you receive is like a Value.

In attention, each word's current representation is transformed into three vectors with these roles.6

For our sentence, we can loosely imagine the Query of "it" saying:

What information would help explain me?

The Key of "dog" helps determine whether "dog" is a useful match.

The Value of "dog" contains the information that can actually be passed along.

So the distinction is:

Query and Key help decide how much attention to give. Value is what gets mixed into the result.

These are not actual English questions or labels inside the computer. They are all lists of numbers, produced using learned transformations. The same transformation can turn different input vectors into different queries, keys or values.6

Think of putting the same word through three different machines. One makes its question, one makes its matching label, and one makes its message.

How do we calculate attention?

Let's break this into small steps.

First, calculate a matching score

We compare the Query of "it" with the Key of each word using something called a dot product. This means multiplying matching numbers and adding the results.6

For example:

[1,2]·[3,4]=1×3+2×4=11

That is all the dot product is doing here.

For attention, the resulting number acts as a matching score. A larger score gives that position more influence after the next steps. It does not mean the two words have the same meaning.

The original Transformer also divides these scores by the square root of the length of the Key vector. This helps stop the scores from becoming too large as the vectors get longer, which can make learning difficult.1

For now, think of this as adjusting the volume before listening.

Next, turn the scores into shares

Suppose, in a tiny made-up example, we consider only three words. After scaling, their scores are:

dog:    2
tired:  1
water:  0

We want to turn these into shares of attention that add up to 1.

For this, we use softmax. It turns a collection of scores into positive weights that sum to 1, with bigger scores receiving bigger weights.7

For our example, the result is approximately:

dog:    0.665
tired:  0.245
water:  0.090

So we are giving around 66.5% of the attention to "dog", 24.5% to "tired", and 9% to "water".

These are invented example scores, not measurements from a real model. In the full sentence, we would include every position that this attention operation is allowed to look at.

Also, 66.5% attention does not mean "the model is 66.5% certain that 'it' means 'dog'". It is a mixing weight, not a confidence score for that claim.

Finally, mix the Values

Now we use those shares to combine the Value vectors:

zit=0.665Vdog+0.245Vtired+0.090Vwater

Here, zit is the result of this little attention calculation.

Imagine mixing paint. More blue paint makes blue contribute more to the mixture. Similarly, a larger attention weight makes that word's Value contribute more to the output.

We do not just pick "dog" and throw everything else away. We make a weighted mixture.

This is the central operation of attention: use Query–Key comparisons to decide the weights, then use those weights to combine the Values.1

The famous equation

Now we can look at the equation from the paper:1

Attention(Q,K,V)=softmax(QKTdk)V

Q, K and V collect the queries, keys and values into tables of numbers.

QKT calculates the matching scores.

dk is the length of a Key vector.

The final multiplication mixes the Values using the attention weights.

So this whole equation is saying:

Compare, scale, turn scores into shares, and mix.

The notation is just a compact way of writing the steps we already did.

Multi-head attention

Now suppose we do not want just one way of looking at the sentence.

Imagine showing the same picture to three friends. One notices the people, another notices their expressions, and another notices what is happening in the background.

They are looking at the same picture, but they may notice different things.

Multi-head attention runs several attention calculations with different learned transformations. Their outputs are joined together and transformed again. This gives the model multiple ways to collect information.6

For our sentence, imagine one head noticing a connection between "it" and "dog", while another notices a connection between "drinking" and "water".

These are examples of possible behaviour, not jobs we assign by hand. We do not tell head one, "You are now the pronoun expert". Nor does every head necessarily learn a tidy role that we can explain in one sentence.

The idea is simply to allow more than one view.

What happens after attention?

Attention is important, but a Transformer is not just attention repeated forever.

After attention, there is a feed-forward network. It processes each position's vector separately, using the same learned operation at every position within that layer.5

Think of attention as talking to your classmates and collecting their notes. The feed-forward network is the part where you work on the information in your own notebook.

There are also two supporting pieces.

A residual connection adds a sublayer's input back to its output. Instead of only passing along the new calculation, we also give the previous representation a direct route forward.5

In our notebook example, it is like adding new notes rather than immediately throwing away the old page.

Layer normalization adjusts the numbers within a representation using their average and spread, followed by learned adjustments. Think of it as keeping the numerical scale organised before the next stage works on it.8

In the original-style encoder layer, these pieces fit together like this:

Input
  ↓
Multi-head self-attention
  ↓
Add the input back, then normalize
  ↓
Feed-forward network
  ↓
Add its input back, then normalize
  ↓
Output

Layers like this are stacked, so later layers work with representations that earlier layers have already updated.5

Encoder and decoder

So far, we have explained the main building blocks. How do they become a complete model?

The original Transformer has two main parts: an encoder and a decoder. The encoder creates representations of the input, and the decoder uses them while producing the output sequence.2

Think of translating a sentence.

The encoder is the reader. It works on the sentence you provided.

The decoder is the writer. It works on the translated sentence.

The decoder uses attention in two ways: self-attention over the output text so far, and attention to the encoder's output. That second kind is called cross-attention. Its queries come from the decoder, while its keys and values come from the encoder.5

In our classroom example, self-attention is discussing your own group's notes. Cross-attention is asking another group for information from theirs.

But the decoder cannot peek ahead

Suppose we are training a model to produce:

"The cat ate fish."

When predicting "fish", it should be able to use "The cat ate", but it should not be allowed to look at the answer it is supposed to predict.

That would be like giving someone a spelling test while leaving the answers open beside them.

So the decoder uses masked self-attention. A mask blocks access to later output positions. During training, the decoder's input is shifted relative to the answers: each position predicts the next token rather than the token it was given. Together, these prevent it from peeking at the answer.9

This is an important detail about the opening idea of "every word looking at every other word".

The encoder can look across the input sentence. The decoder's self-attention has a no-peeking rule.

How do numbers become words again?

At the end of the decoder, a learned transformation produces scores for the possible next tokens. Softmax turns these scores into a probability distribution. The generation procedure then uses those probabilities to choose what comes next.9

Suppose our output so far is:

"The cat ate"

A made-up distribution might look like this:

fish:             0.70
food:             0.20
all other tokens: 0.10

Suppose we choose "fish".

Now the output is "The cat ate fish", and we repeat the process to decide what comes next, eventually producing an end token. Generating a new sequence this way still happens one token at a time.9

So "the Transformer can process positions in parallel" does not mean "it produces an entire new answer in one step".

How does it learn all this?

You might have noticed that we kept saying "learned".

Learned embeddings. Learned transformations. Learned ways of combining information.

Where does the learning happen?

During training, the model makes predictions and compares them with the training targets. A loss function measures how wrong those predictions are, and an optimizer adjusts the model's learned numbers to reduce that loss.7

Think of practising a game.

You try something, get feedback, and make an adjustment. Then you try again.

The feedback is numerical rather than someone saying "good job". But that is the basic loop.

For translation, the training examples include an input sentence and its target translation. The model learns to predict the target tokens from the input and the earlier target tokens.9

Nobody needs to manually write a rule saying, "Whenever you see this exact dog sentence, make 'it' attend to 'dog'".

The training objective teaches the model which adjustments help it make better predictions.

So why was this a big deal?

Many earlier language systems used recurrent networks, which passed information along a sequence step by step. The Transformer allowed direct attention between positions and much more parallel computation during training.2

Think of passing a message down a line of children versus letting the children directly consult one another's notes.

Attention already existed before this paper. The important change was building the Transformer without the recurrent or convolutional sequence-processing structure that earlier systems relied on.1

And despite the title, attention is not literally the only thing inside it. We just met embeddings, position information, feed-forward networks, normalization and more.

But the central idea is easy to carry with you:

A word alone gives you some information. The words around it help tell you what it means here.

For us, "it" referring to the dog feels obvious.

For the model, there are vectors, matching scores and weighted mixtures.

Attention is the part that lets those little lists of numbers exchange information.

Thank You
Animish Sharma

  1. Vaswani et al., Attention Is All You Need, 2017.↩

  2. Google Research, Transformer: A Novel Neural Network Architecture for Language Understanding.↩

  3. TensorFlow, Word embeddings.↩

  4. Hugging Face, Tokenizers.↩

  5. TensorFlow, Neural machine translation with a Transformer and Keras.↩

  6. Keras, MultiHeadAttention layer.↩

  7. TensorFlow, TensorFlow 2 quickstart for beginners.
    ↩

  8. Keras, LayerNormalization layer.↩

  9. Keras, English-to-Spanish translation with a sequence-to-sequence Transformer.↩