We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
AI Engineering · How Models Generate
Free preview: the first 2 parts of this 22-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
Generative AI and Agentic AI Engineering
Unlock this lesson and the 208 other paid lessons
$35 once, by PayPal
See the Generative AI and Agentic AI courseread the rest of this lesson in
Generative AI and Agentic AI Engineering
Unlock this lesson and the 208 other paid lessons
$35 once, by PayPal
See the Generative AI and Agentic AI coursein How Models Generate
This lesson is part of the AI Engineering course. Unlock the full course, a separate one-time payment, for lifetime access.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime AccessFoundation lessons of this course are free, no payment needed. Try them now
Read this sentence: "The trophy did not fit in the suitcase because it was too big." What was too big? The trophy. Now change one word: "...because it was too small." Now "it" means the suitcase.
You worked that out without effort, by looking back at the earlier words and deciding which one "it" is about. A language model has to do the same kind of thing for every word it reads. The part of the model that does it is called attention.

In lesson 3 you saw that the model reads a prompt quickly and writes slowly. This lesson opens up the reading step. You will work out attention by hand with a few small numbers, then read the real attention weights of a real model, and you will find that a real model does not behave exactly like the simple picture.
By the end you will be able to compute attention for one word by hand, read an attention table, explain why a word can only look backwards, and say what attention weights can and cannot tell you.

Inside the model, every word (every token, to be exact) is a list of numbers, like the from the Tokens and Embeddings chapter. Attention works on those lists.
Query. A list of numbers that describes what the current word is looking for. Think of it as a question the word asks.
Key. A list of numbers for each earlier word that describes what it offers. The query is compared with every key.
Value. A list of numbers for each earlier word that holds what it will pass on if it is chosen.
Weight. A number between 0 and 1 that says how much one word pays attention to another. For one word, all its weights add up to exactly 1.
Pronoun and adjective. A pronoun is a short word like "it" or "they" that stands for a noun mentioned elsewhere. An adjective is a describing word like "big" or "small".
Softmax. The step that turns raw scores into weights: it makes every number positive and scales them so they add up to 1. Lesson 1 showed the odds of the next token; softmax is also the step a model uses to make those odds from its scores.
Head. One complete set of queries, keys and values. Each head can learn to look for something different. The model in this lesson has 14 heads in every layer.
Layer. One step of the model. The model here has 24 layers, one after another, and every one of them has attention.