Give Me 21 Min, I Will Make Attention Click Forever — Transcript
Full transcript
- 0:00You have seen the attention formula
- 0:01before. This exact formula [music] runs
- 0:03inside chat GPT. But what does it
- 0:05actually mean? So you go looking [music]
- 0:07for an explanation and you find the
- 0:09paper that introduced it. Attention is
- 0:11all you need from 2017. But you just
- 0:14come away more confused.
- 0:16Give me 21 minutes.
- 0:18I will make attention click forever.
- 0:21Here's that same formula as code. One
- 0:23short Python function. This may look
- 0:26confusing to you. By the end of this
- 0:27video, you will understand every single
- 0:29line. Here's the plan. First, we build
- 0:33your intuition with a story for each
- 0:35piece of the formula. So, every one of
- 0:37them becomes super super easy. Then, we
- 0:40walk through the whole algorithm end to
- 0:42end on one tiny toy example, line by
- 0:45line, so you can see every variable,
- 0:47every shape, every number in the open. I
- 0:50know this table looks overwhelming.
- 0:52Don't read it now. We earn every row of
- 0:54it before the end.
- 1:00Now let's first see why attention is
- 1:02needed at all. You want to train a model
- 1:04to understand human language.
- 1:06But a computer only understands numbers.
- 1:09So a sentence means nothing to it. You
- 1:11need a translator that takes human
- 1:13language in and pushes numbers out. So
- 1:16what will we ask the translator to
- 1:18translate? We will consider two
- 1:20sentences.
- 1:21The first the crane lifted steel and the
- 1:24second the crane ate fish. There are
- 1:27three ways to translate words into
- 1:29numbers and the first two get something
- 1:31wrong. The first way is word ids. You
- 1:34give every word its own number. Every
- 1:37word gets one number and crane is one.
- 1:39Between them the two sentences use
- 1:41exactly six different words. That is our
- 1:44whole vocabulary.
- 1:45In Python, two lines build the mapping
- 1:48and each word's number is just its
- 1:50position in the list. The first print
- 1:52shows the whole table and each sentence
- 1:54becomes a list of ids. The crane lifted
- 1:57steel turns into zero, 1, 2, 3. Why is
- 2:01crane one and why is lifted two? There
- 2:04is no reason at all. Put the same six
- 2:06words in a different order and every
- 2:08number can change. In vocabulary 1,
- 2:12crane is one. So let's shuffle the same
- 2:14six words into vocabulary 2. The two
- 2:16tables come out different and the same
- 2:18sentence comes out different. 0 1 2 3
- 2:22becomes 1 3 2 0. Crane jumps from 1 to
- 2:26three while lifted happens to keep its
- 2:28two. You want the numbers to mean
- 2:30something. Words with close meanings
- 2:33should sit close together. Animals at
- 2:35one end, machines at the other and crane
- 2:37in the middle. Close meanings should get
- 2:39close numbers. That is the second way.
- 2:41The embedding
- 2:43It gives every word a row of numbers and
- 2:45similar words get similar rows. A real
- 2:48model fills this table during training,
- 2:50but let's just write a simple one by
- 2:52hand. The embedding table maps each word
- 2:54ID to its row. So looking up, Crane
- 2:57means reading row one, counting from
- 2:59zero. Out comes Crane's row 0.70.7.
- 3:04Each row is just two numbers. So you can
- 3:06draw every word as an arrow on a graph.
- 3:09The two axes are the two columns of the
- 3:11table. how animal a word is and how
- 3:13machine. Start with the machine words.
- 3:16Their two rows light up in the table and
- 3:18their arrows land lifted and steel
- 3:20pointing steeply up almost the same way.
- 3:23The animal words mirror them at the
- 3:25other end. Eight and fish lie almost
- 3:27flat and the tiny arrow stuck in the
- 3:30corner is the word the meaning almost
- 3:32nothing. Now watch Crane's row become
- 3:34its arrow. 0.70.7
- 3:38the one long arrow sitting exactly
- 3:40between the two ends bird or machine and
- 3:44that middle is the problem. The first
- 3:46sentence needs the machine crane and the
- 3:48second needs the bird but the table
- 3:50holds exactly one row for crane. Ideally
- 3:53the row a word gets should depend on the
- 3:55context. In the crane eight fish, crane
- 3:57should sit at the animal end close to
- 3:59eight in fish. And in the crane lifted
- 4:02steel, at the machine end close to
- 4:04lifted in steel. That leaves answer
- 4:07three, attention. Now for the heart of
- 4:09this video, query, key, and value piece
- 4:12by piece. So how do you get context into
- 4:15a word when the table only hands back
- 4:17the same row? The same way you did as a
- 4:20reader. Lifted and steel told you which
- 4:22crane this is. So after the lookup, you
- 4:25let the words share information.
- 4:27Then you let each word rebuild itself
- 4:29from its neighbors. That process is
- 4:32attention.
- 4:33How does the formula do that? The three
- 4:35capital letters are three roles every
- 4:37word plays at once. The roles are query,
- 4:40key, and value. Think of it like a group
- 4:42quiz in class. You have a question, your
- 4:45classmates's reply, and the better a
- 4:48reply matches, the more you take from
- 4:49that classmate.
- 4:51The query is the question you ask the
- 4:53class. For Crane, the question is, "Am I
- 4:56an animal or a machine?" Each key is a
- 4:59classmate's reply, the help they
- 5:01advertise.
- 5:03The machine words raise their hands, and
- 5:05a word as general as the word that says
- 5:07it cannot help. Each value is different.
- 5:10It is the offer a classmate hands over,
- 5:12the word's own meaning. The machine
- 5:14words replied best, so Crane becomes a
- 5:16machine. The formula does exactly that
- 5:18in numbers. Now, let's break that
- 5:21formula into its five pieces.
- 5:23Here's the road map. What each piece
- 5:26does and what it is like in the
- 5:27classroom. We will score the matches,
- 5:30scale them down, mask the future, turn
- 5:32scores into shares, and mix the offers
- 5:34one piece at a time.
- 5:39It is like a group quiz in class. Piece
- 5:41one, the score, is you walking the room,
- 5:45glancing at every name tag for the
- 5:46classmate who can actually help. Piece
- 5:50two tones the voices down so nobody gets
- 5:51too loud. Piece three blocks whoever you
- 5:54are not allowed to ask like the
- 5:56classmate holding the answer key. No
- 5:58matter how much you want that answer,
- 6:00they are off limits. Piece 4 splits your
- 6:04100% of trust into shares. And piece 5
- 6:07blends the offers using those shares so
- 6:09the best match contributes the most.
- 6:12Where do K and V come from? In the GPT
- 6:15model behind chat GPT, they are learned.
- 6:18Trained weights turn each word's row
- 6:20into its question, its reply, and its
- 6:23offer. For our walk, they are simply
- 6:25given. And how the learning works is a
- 6:27story for another video. Each row is one
- 6:29word of the sentence. The crane lifted
- 6:31steel. Each row has just two dimensions.
- 6:34The same two axis as the arrow graph
- 6:36from earlier. How animal a word is and
- 6:38how machine.
- 6:41and K and V line up the same way, row
- 6:43for row. All three tables use the same
- 6:46two axis, but they say different things
- 6:47about them. V is the word's own meaning.
- 6:51Coup is how loudly a word asks about
- 6:53each axis, and K is how much it
- 6:55advertises. Crane does not know what it
- 6:57is, so it's coup asks loudly about both
- 6:59axes, 1.4 and 1.4.
- 7:03And I made its key short on purpose.
- 7:060.20.
- 7:07A word unsure of itself has little to
- 7:10advertise and that short reply will cost
- 7:12Crane attention soon.
- 7:16Let's start with piece one, the score.
- 7:18It is where each word asks its question
- 7:20to every word, itself included.
- 7:23To score one pair, Crane compares its
- 7:25question to one reply to check the
- 7:26match. To score every question against
- 7:29every reply, you multiply the matching
- 7:31numbers, then add them up once for every
- 7:34pair. One matrix multiplied does all 16
- 7:36at once. Crane's row is the second from
- 7:38the top. 0.14, 0.56, 0.70, and 0.63.
- 7:45Here is that multiply with a word on
- 7:47every row. The second matrix is K
- 7:50flipped on its side, which the formula
- 7:52calls Krposed.
- 7:55Crane's row is the one highlighted.
- 7:58That multiply and add is the dot
- 7:59product, a similarity meter. The score
- 8:02goes up when two arrows point the same
- 8:04way and up again when the arrows are
- 8:06long. That is why lifted and steel, both
- 8:09long and both pointing the same way, hit
- 8:120.81, the biggest score between two
- 8:14different words. For the full story,
- 8:16check out our linear algebra video.
- 8:19Next, let's look at piece two, the
- 8:21scale. It divides every score by the
- 8:23square root of DK, where DK is just the
- 8:26width of one reply row, two here,
- 8:28because each vector holds two numbers.
- 8:32From here on I will just say the square
- 8:33root of D. The scale step is one line of
- 8:37code. You divide the whole grid by the
- 8:39square root of two which gives the same
- 8:41shape with smaller numbers. Take Crane's
- 8:44score for little the 0.14 divide it by
- 8:47the square<unk> of 2 and it becomes
- 8:490.099.
- 8:52The rest of the row comes down to 0.3960
- 8:560.4950
- 8:58and 0.4455.
- 9:01If you write it out with a word on every
- 9:02row, it is the same grid, just smaller.
- 9:05Every remaining cell is the same single
- 9:07division. So, I will not read them one
- 9:09by one. Why divide it all? Because
- 9:12vectors with many numbers make the
- 9:13scores too large. Let's watch it happen
- 9:16on the questions and replies themselves.
- 9:19Each vector holds just two numbers. So,
- 9:21you repeat every number a 100 times and
- 9:24two numbers become 200. Here is Crane's
- 9:27query before and after. The 200 in the
- 9:30name Q 200 just means 200 numbers. Real
- 9:34vectors hold 200 different numbers.
- 9:37Repeating one number changes nothing
- 9:39else. So you can watch what the width
- 9:40alone does to the score. In code that is
- 9:43one repeat interle and Crane's whole row
- 9:46of scores comes out 100 times bigger.
- 9:48The shape is now 4x 200. Crane's long
- 9:51question is 1.4 four all the way down
- 9:53and its scores come out at 14, 56, 70,
- 9:57and 63. That is exactly 100 times the
- 10:00row from before. You do not need to
- 10:02understand softmax yet. It turns scores
- 10:04into shares, and piece 4 explains how.
- 10:07Here are the original small scores and
- 10:09the big scores after softmax.
- 10:11The top row came from the small scores,
- 10:13the bottom from the big ones. The big
- 10:15row's first two entries only print as
- 10:17zero because the display rounds them
- 10:19down.
- 10:20Softmax never quite reaches zero. With
- 10:24the small scores, every word gets a
- 10:25share, even little the. With the big
- 10:28scores, the winner takes 99.9%.
- 10:31Almost everything else rounds to zero.
- 10:34Dividing by the square root of D stops
- 10:36this.
- 10:37For those 200 number vectors, the square
- 10:39root of D is the square root of 200. The
- 10:42division brings the big scores back down
- 10:44into a real spread. 0.0095 0095
- 10:480.1858
- 10:500.4999
- 10:5203048
- 10:55Lifted still leads at about half with
- 10:57steel and crane behind it instead of one
- 11:00word taking everything with two numbers
- 11:02in each vector. The division barely
- 11:03matters with 200. It is the difference
- 11:06between sharing and winner take all. And
- 11:09that is all the scale piece does. It
- 11:11turns the volume back down before
- 11:12softmax. So a big score row like this
- 11:15one shares again. Piece three only shows
- 11:18its effect once the scores have become
- 11:20shares. So let's do piece four first.
- 11:22The normalized step where the scores
- 11:24turn into shares that add up to one.
- 11:26Crane has exactly 100% of attention to
- 11:29give. Crane hands out its trust. More
- 11:32for the helpful words, less for little
- 11:34the and nothing left over. Every share
- 11:37is positive. Even little the keeps its
- 11:39small chunk. And together the four of
- 11:41them add up to exactly one whole.
- 11:44The obvious move is to divide each score
- 11:46by the rows total. But that is not
- 11:48enough because in a real model scores
- 11:50can be negative and a negative share
- 11:52means nothing. So you raise E to each
- 11:54score first where E is just a fixed
- 11:56number about 2.718. E to any score is
- 12:00always positive. So the results are
- 12:02always positive.
- 12:04Crane's row has every step written out.
- 12:07Raise E to each of its four scaled
- 12:09scores and the four results add up to
- 12:105.7916.
- 12:12Then you divide by the total. That is
- 12:15all softmax is. Make every score
- 12:17positive. Then divide by the row total.
- 12:20Take little the e to its scaled score is
- 12:231.1041
- 12:24and divided by 5.7916.
- 12:27That is 0.1906.
- 12:29The other three shares come out the same
- 12:31way. 0.2566,
- 12:340.2832,
- 12:36and 0.2696.
- 12:38It is the same function you borrowed for
- 12:40the demo back at piece 2. And notice
- 12:42what E does. Every extra point of score
- 12:45multiplies a word share, which is why
- 12:47those big scores collapsed onto one
- 12:49winner. Here, the scaled scores sit
- 12:51close together, so the shares will too.
- 12:54Here it is in code. Softmax on every row
- 12:57gives these numbers. The second row is
- 12:59exactly the crane shares you just built.
- 13:02The second row holds Crane's attention
- 13:03weights. 19% on little the the smallest
- 13:06share because it scored lowest and still
- 13:08not zero. Softmax never hands out an
- 13:11exact zero. 26% on itself. That is what
- 13:15Crane's short key cost it. 28% on Lifted
- 13:18and 27% on steel. Crane trust its
- 13:21machine neighbors more than itself and
- 13:23every row adds up to one. The shares sit
- 13:25close together because the scores are
- 13:27small and close and they use the scaled
- 13:30scores so they come out a little
- 13:31different from the quick demo back at
- 13:32P2.
- 13:34What moves Crane is the offers behind
- 13:37them. Lifted and steals rows are the
- 13:39machine heavy ones. Softmax can shrink a
- 13:42share, but it can never make one exactly
- 13:44zero. Some words do need exactly zero,
- 13:47the ones that have not been written yet.
- 13:49That is piece three, the mask. Hold that
- 13:52thought. The mask comes last. First,
- 13:55piece five, the mix. You blend the
- 13:57offers using those shares. Each word
- 14:00rebuilds itself as a weighted average of
- 14:02every word's offer. It uses its own
- 14:04trust shares as the weights. The matrix
- 14:07multiply weights time V does the math.
- 14:09Crane shares are the weights on the four
- 14:11value rows and every other row blends
- 14:13the exact same way. The weights grid on
- 14:16the left multiplies the value rows in
- 14:18the middle. And the result on the right
- 14:20is one rebuilt row per word with Crane's
- 14:22row highlighted. Every row here blends
- 14:24the same four offers. So all four land
- 14:27close together and every one of them
- 14:29moved. Even little the in the top row
- 14:32comes out machine heavy. And crane is
- 14:34the only word whose animal number went
- 14:36down on the map. The mix is a weighted
- 14:38pull. Lifted and steel pull crane toward
- 14:41the machine side. Little the ads it's
- 14:4319% and crane's own 26% share is what
- 14:46keeps it from flying all the way over.
- 14:49Crane lands with its animal score down
- 14:50from 0.7 and its machine score still
- 14:53high. Nobody told the model that Crane
- 14:56is a machine here. It leaned on its
- 14:58neighbors and lifted and steel carried
- 15:00the machine numbers. That was enough.
- 15:03Crane finishes the step rebuilt from its
- 15:04neighbors. Attention rebuilds every word
- 15:07as a weighted average of itself and its
- 15:10neighbors. A different sentence brings
- 15:12different neighbors. Run the sentence
- 15:14the crane ate fish through the same
- 15:15pieces. Eight and fish sit at 0.9 on the
- 15:18animal axis. The exact mirror of lifted
- 15:21and steel. So the offers crane averages
- 15:23in are animal offers and crane lands on
- 15:25the animal side instead. So we have
- 15:28walked four of the five pieces. Score,
- 15:30scale, normalize, mix. But there is one
- 15:33important piece left. Piece three, the
- 15:35causal mask. Without it, every word can
- 15:38still use the words that come after it.
- 15:39And yes, that includes the help Crane
- 15:41just took from Lifted and Steel. The
- 15:44mask is for models like GPT, which write
- 15:46one word at a time by predicting the
- 15:48next word. Here it is as a robot at a
- 15:50desk. It sees the words the crane
- 15:52lifted, and it must guess steel using
- 15:55only the words typed so far. During
- 15:58training, the robot reads the whole
- 15:59sentence at once, and at lifted, it
- 16:01should guess steel. But Lifted's row
- 16:03mixed in 30% of steel itself, so the
- 16:05answer is inside the clue. That 30% is
- 16:08the 0.2976
- 16:10sitting in Lifted's row of the weights.
- 16:13Reading the answer gets a good score in
- 16:15training, but it fails in use. The
- 16:17future words are not there yet. You fix
- 16:20that by adding the mask matrix M into
- 16:22the formula. M holds a zero where a word
- 16:25is allowed to look and negative infinity
- 16:27where it is not. Adding it overwrites
- 16:30every future score because negative
- 16:32infinity plus anything is still negative
- 16:34infinity.
- 16:36You use negative infinity instead of
- 16:39zero because softmax raises e to each
- 16:42score before it divides. E to the 0 is 1
- 16:45which still takes a share but e to the
- 16:47minus infinity is zero which takes
- 16:49exactly nothing. Here's the scaled grid
- 16:52from piece 2. Again, in code, the trill
- 16:54function builds a triangle of ones for
- 16:56the past and zeros for the future. A one
- 16:59in the code marks a cell a word may look
- 17:01at, which is the same thing as a zero in
- 17:03M. You overwrite every zero position
- 17:06score with negative infinity. Then run
- 17:08softmax again. Read the zeros down the
- 17:11rows. The first word keeps only itself.
- 17:14Crane keeps two. Lifted keeps three. And
- 17:17steel last in line keeps all four. Look
- 17:20at Crane's row second in line. It now
- 17:22sees only the word the and itself. The
- 17:25masked entries hold negative infinity
- 17:27and e to the minus infinity is exactly
- 17:29zero. So those words drop out. The total
- 17:32is now just e to the 0.0990
- 17:35plus e to the 0.3960
- 17:38which is 2.5899.
- 17:40Crane's row becomes 0.4263
- 17:440.5737
- 17:460 0. Its shares on lifted and steel did
- 17:50not shrink. They became exactly zero.
- 17:54Then the word the and crane split the
- 17:56whole amount. Let's draw it as a grid.
- 18:00The mask puts negative infinity into
- 18:02every cell above the diagonal. After
- 18:04softmax, the lower left triangle holds
- 18:06the weights and every masked cell is
- 18:09exactly zero.
- 18:11Information moves one way only from past
- 18:13to present. The word the is first, so
- 18:15its row keeps only itself. In training,
- 18:18every row makes its own guess. Even the
- 18:20early rows that can only guess blindly.
- 18:22When GPT writes the newest word is the
- 18:24last row. GPT reads its guess from the
- 18:27last row. The only row that sees
- 18:29everything.
- 18:31Turning that row into a word is another
- 18:33video story. Here is the cue. Steel
- 18:36stands last. So its row collects from
- 18:38every word ahead of it. That makes
- 18:41Steel's row the last row of the masked
- 18:43weights. Now attention runs start to
- 18:46finish. Every piece you have walked now
- 18:48running as one function with the mask
- 18:50on. Here is the whole function with
- 18:53every line labeled for the piece it
- 18:55computes. You start with the same three
- 18:57given tables. Each word gets one
- 18:59question row, one reply row, and one
- 19:02offer row. Every shape uses two sizes. T
- 19:06is the sentence length of four words and
- 19:08D is the embedding size of two numbers
- 19:10per word. That makes each table T by D,
- 19:13which is 4x two. Line one scores the
- 19:17pairs. You multiply Q shape T by D by K
- 19:20flipped on its side shape D by T and out
- 19:23comes a T by T grid. One row per asking
- 19:26word, one column per replying word. The
- 19:28row for crane is highlighted and the
- 19:30word the sits at 0.14.
- 19:33Next line two. Divide everything by the
- 19:36square root of D, which is the square
- 19:38roo<unk> of 2. And the grid keeps the
- 19:40same shape with smaller numbers. Scores
- 19:43are ready. Now stop the model from
- 19:45reading ahead. On lines three and four,
- 19:48let's add the mask m a real matrix with
- 19:52a zero wherever a word may look and a
- 19:54negative infinity wherever it may not.
- 19:57On line five, you normalize every row
- 19:59with a softmax. The shares now sum to
- 20:01one and each negative infinity becomes
- 20:03an exact zero.
- 20:06And the last line mixes the offers by
- 20:07the shares. A t by t * a t by d and the
- 20:11shape returns to t by d. one rebuilt row
- 20:14per word. The word that sits first, so
- 20:16its row kept only itself. Crane is
- 20:19second in line. 42.63%
- 20:21comes from the first word, little the
- 20:24and 57.37% from crane itself. So under
- 20:28the mask, crane stays ambiguous at
- 20:300.4442
- 20:32on both axis. So does Crane ever find
- 20:35out it is a machine? Not on its own row.
- 20:38Look at Crane's row of the mass weights.
- 20:41The last two numbers are zero.
- 20:43Crane cannot see lifted or steal the two
- 20:45words that would have told it under the
- 20:47mask. That evidence comes too late. But
- 20:50the story moves down the queue. The
- 20:52later words do see them. And the last
- 20:54row, the one GPT actually reads, sees
- 20:56the whole sentence.
- 21:00Here is the whole run in one table. It
- 21:02shows every line of the algorithm, the
- 21:04shape that line makes, and every number
- 21:06it produced for your sentence.
- 21:09You have walked every one of these rows
- 21:11already, so I will not go through them
- 21:13again.
- 21:15Pause here as long as you like.
- 21:19And to prove it, let's run the whole
- 21:21function in one call. Out comes the
- 21:23exact table you just walked.
- 21:26Now, attention clicks.
About this transcript
This page contains the full transcript of Give Me 21 Min, I Will Make Attention Click Forever by Zachary Huang, generated from the public captions YouTube serves with the video. The transcript has 3,618 words across 530 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.