Gradient LabStart the lesson

Lessons › Chapter 11: Transformers and LLMs

Attention

attention lets the word “it” look at every word in the sentence, all at once, and score how relevant each one is.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

A stolen card

“My card was stolen yesterday and it still shows purchases.” Pip must link “it” to “card”.

Problem

What does “it” mean?

In “The animal didn’t cross because it was tired”, you instantly know “it” is the animal. A model reading word by word needs a way to connect “it” back to the right word.

Concept

Look at every word

attention lets the word “it” look at every word in the sentence, all at once, and score how relevant each one is.

Key idea

Weigh what matters

The scores become weights that add up to 1. “animal” gets most of the weight (55%); filler words get almost none. Thicker arcs mean more attention.

Build

Blend in the meaning

“it” is then updated with a weighted mix of all the words, mostly “animal”. Now its numbers carry the idea “the tired animal”. Every word does this for every other word, in parallel.

Real life

In real life: translation apps

When translating, word order often changes: French says “le chat noir” for “the black cat”. Attention lets each output word look straight at the input words it needs, wherever they are. That idea made translation apps dramatically better.

Quick check: In attention, what decides how much one word looks at another?

Scores are computed from the words’ vectors and normalized with softmax.

Pseudocode

sentence = "The animal didn't cross because it was tired"
for the word "it":
    score every word: how relevant is it to "it"?
    turn the scores into weights that add up to 1   # softmax
    new meaning of "it" = weighted mix of all the words
# "animal" gets most of the weight

Key terms

attention
A way for each token to look at the other tokens and pull in the information most relevant to it.

In the news

Recent models and papers that build on this idea: