1. Home
  2. Explore
  3. Computer Science
  4. Attention and Transformers

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Attention and Transformers

Transcript

All right, welcome back everyone to lecture 8. Uh, today we're going to talk about attention and transformers. And I think this is a this is a really fun one. So, as a quick recap, last time we were talking about recurrent neural networks and recurrent neural networks were this new kind of neural network architecture meant for processing sequences. And in particular, we saw how neural networks by by processing sequences let us attack a whole new different kinds of problems than we than we could with convolutional networks uh before. So in particular usually we had been thinking about these onetoone problems where you input one thing like an image and then output one thing like a classification for what's in that image. But once you have the ability to move beyond images and move towards sequences of data it let us tackle a lot of new kinds of problems like one to many problems image captioning maybe we want to input an image output a textual

description of that image which is going to be a sequence of words. Um maybe many to one where we do input a sequence of frames and output a classification for those frames. um and a bunch of other problems along this vein. Um so now we're seeing that moving be moving into these more um sophisticated neural network architectures um both is sort of more interesting architecturally but also lets us tackle new problems than we could with um with kind of more traditional feed forward neural networks. So today we're going to build on that and talk about two new things in today's lecture. Um the first thing is going to be attention which is going to be a brand new neural network primitive that fundamentally operates on sets of vectors. And then the second thing we're going to talk about is the transformer. And the transformer is a different neural network architecture that has self attention at its core. Um, and this and the spoiler alert is that transformers are basically the

architecture that we use for almost all problems in deep learning today. So any of the largest applications that you're seeing out there in the wild today, whether it's classifying images, generating images, generating text, classifying text, working with audio, basically any kind of large neural network today, um that is large, state-of-the-art, trained in a lot of data, um deployed by a big company, almost all of them are going to be transformers today. Um so that's really exciting that we get to get you up to speed on the latest and greatest architectures that people are using now. Um, but even though transformers are this sort of state-of-the-art architecture that everyone is using for everything today, they have kind of they have a relatively long history. Um, and they initially like it's kind of interesting watching these fields develop because looking back on it when the moment that Transformers came out, it feels like it ought to have been this

big moment, this big thing when there was a big sea change, this new architecture, this new thing. But it actually didn't feel that way because even though there was one moment where the transformer architecture was born, um these ideas around self attention, around using attention in various ways, those had actually been around in the field for several years at that time. And in particular, these ideas around attention, self attention, they actually developed out of recurrent neural networks. So we're going to start there to talk about and motivate these problems. So this is going to be a little bit mirroring the historical development of these ideas. So for that reason um we're actually going to in order to introduce transformers we're actually going to roll back and recap a little bit about this idea of recurrent neural networks that we saw in the last lecture. So as a motivating problem um let's think about this sequence to sequence problem of

translation. So we want to input one sequence which is going to be a sequence of words in English. Then we want to output another sequence which is going to be a sequence of words in a different language Italian. Right? And because you and we can't make assumptions that there's any correspondence between those words. Right? The the number of words in the English sentence might be different from the number of words in the Italian sentence and the order of those words might be totally different. So this is a perfect application of the kind of sequence processing algorithms that we uh sequence processing architectures that we saw in recurrent neural networks. Um and indeed this idea of processing these uh sequencetosequence problems with recurrent neural networks. This goes all the way back to 2014 um even even a bit earlier than that. But people had been processing sequences with recurrent neural networks for more than a decade more than a decade at this

point. So the basic architecture for processing sequence sequence to sequence problems with recurrent neural networks is that typically you'll start with one encoder. Your encoder is a recurrent neural network. Um the recurrent neural network recall is this function that gets applied recursively um on two inputs. One is your xt your input at the current time step and the other is your ht minus one which is your hidden state at the previous time step. and your recurrent neural network unit will then spit out a next hidden unit, a next hidden state at the next time step. And then we can apply that same recurrent neural network unit over in time um to process a sequence of a of potentially variable length. So in this case, we're using a recurrent neural network encoder that inputs the input sequence in English. Um input sequence, you know, you you got to use relatively short sentences um to fit on slides and still have all the boxes show up. So we're

using a kind of short, silly sentence, uh we see the sky. Um, and this, you know, each word in that sentence gets processed but via one tick of the recurrent neural network. And now, and then we're going to the idea of this encoder neural recurrent neural network is it wants to process all of the words in in the input sequence and somehow summarize the content of that input sentence so that we can translate it into a different into our output target language. So the the the more the more concrete way that this happens is that after processing all the words in the input in the input sequence, we're going to summarize the the entire content of that input sequence into a single vector called the context vector. Um and there's a there's a couple different ways that people would typically do these in recurren recurrent neural networks. I don't think the details are too interesting. So as a you can just think that that context vector is basically the last hidden state of the

of the encoder recurrent neural network. Um and now the idea is that because you know this this these because of the recurrent structure of our recurrent neural networks the last hidden state sort of incorporates information of the entire input sequence. So we can think of that last hidden state as summarizing or encoding all of the information in the entire input sequence. Um so then that that is one vector that is going to kind of summarize that entire input sequence to do whatever we want with it. Um and in this case what we want to do with it is translate that input sequence into an output sequence in a different language. So to do that we're going to use a second recurrent neural network called the decoder. Um which usually has the same architecture but potentially different a different weight matrix different set of learned parameters. Um and this decoder GU is going to be a different recurrent neural network with

different different learnable weights u but has the same basic idea. Um now this a recurrent neural network unit is going to take three inputs at every time step. It's going to take um yt minus one which is the token and the output sequence at the previous time step. It's going to take ST minus one, which is the previous hidden state in the output sequence, and C, which is that context vector summariz summarizing the entire input sequence. Um, and then we kind of unroll that output sequence just as we saw in the last lecture and and produce words one at a time in the output sequence. And I don't speak Italian, so I'm not going to try to I'm not going to try to pronounce these, but um there's some Italian words on the screen that you can see. And I'm assuming that that indeed translates to we see the sky. Um hopefully that's correct, right? But the idea is we're going to like tick this recurrent neural network one tick at a time. It's going

to output words one at a time. And this is basically a summary of what we saw last lecture. So this should this should basically not be too surprising in light of the previous lecture. But there's a potential problem here, right? And that's the there's a communication bottleneck here between the input sequence and the output sequence, right? The only way in which the input sequence is communicating with the output sequence is via that context vector C. Um, and that C is going to be a fixed length vector, right? Because the size of that vector is fixed when we set the size of our recurrent neural network. Um, and maybe that's fine, right? So C might be a fixed length vector of like 128 floats, 1024 floats, but the size of that input vector is not going to change as our input and output sequence sizes grow or shrink. And that's a potential problem, right? So if we're doing short sequences like we see the sky, maybe it seems pretty plausible

that we can summarize everything we need to know about that sequence in that fixed vector of 12 the 1024 floats. But what if we're not trying to translate four words? What if we're trying to translate a whole paragraph or a whole book or like an entire corpus of data? um then in that case we're going to run into a bottleneck where at some point as we scale that input sequence it's just not going to be sensible for to ask the network to summarize the entire input sequence into a into a single fixed length vector. So that's going to be a problem. So the solution here is actually let's not bottleneck the network through a fixed through one fixed length vector. Instead let's change the architecture of our recurrent neural network. Intuitively, what we want to do is not force a bottleneck between a in a fixed length vector between the input and the output. Instead, as we process the output sequence, we're going to give the

model the ability to look back at the input sequence. And now, every time it produces an output vector, we want to give the network the opportunity to look back at the entire input sequence. And if we do this, there's going to be no bottleneck. It will scale to much longer sequences. And hopefully, the the model architecture will work much better. So that's the motivating idea that led to attention and transformers and all this great stuff that we see today. It all came like you know one way of telling the story is that it all came from trying to solve this bottleneck problem in recurrent neural networks. So let's see um how we can actually implement this intuition and endow our recurrent neural network with the ability to look back at the input sequence on every time step. So here we're going to you know start with the same thing. Um our encoder neural network is going to remain the same. No changes there. we still need to set some initial hidden

state for the output sequence. Um and so we need to set some initial um decoder state s0 in some way. But now once we have that decoder hidden state, what we're going to do is look back at the input sequence. So the way that we're going to do that is by computing some alignment scores by comparing that that basically compute um a scalar value a scalar value for each step in the input sequence that says how much does that initial decoder state s0 how much does that decoder state match each token of the input sequence. So in this case there were four tokens in the input sequence. So we want to compute four alignment scores each of which is just a single number um that says how how what is the similarity between the input to the the input sequence the token of the input sequence and this initial um this initial uh this initial decoder state as zero. Now there's a there's a lot of ways that we could implement alignment

scores but um a simple way is just use a simple linear layer that we're calling f subat. And so that linear layer is going to input is going to concatenate the decoder hidden state s um with one of the encoder hidden states H concatenate those two into a vector and then um apply a linear transform that squashes that down into a scaler. Um and that's just a linear operator that can be put into a computational graph and learned jointly via gradient descent just in the way that we learn all other parameters of a network. Um so now at this point we've got sort of this scalar alignment score um for each se for each step in the input sequence. And now we want to apply a softmax function. Right? These scaler alignment scores are totally unbounded. They're arbitrary val they're arbitrary real values from minus infinity to infinity. We want to put some um some structure on this to prevent things from blowing up. So one way that we do this is apply a softmax

function. So we've got four scalar values um telling us the alignment of that decoder hidden state which each with each of the encoder hidden states. Um now we apply a softmax over those four values to give us a distribution over those four over those four values. So remember the softmax function that we saw a few lectures ago is going to take a vector of arbitrary scores and convert it into a probability distribution which means it'll have the property that each entry in the output softmax probabilities will be between 0 and one and they will sum to one. So we can think of so whenever we have whenever we run a vector through a softmax we can think of the thing we get out as a probability distribution rather a discrete probability distribution over those input scores. So in this case, so in this so at this point after we take those alignment scores and run them through a softmax, what we what we've essentially done is predicted a distribution over the input tokens um

given that decoder hidden state. So now what we want to do is take that distribution over over the input tokens and use them to compute a vector um summarizing the information in the encoder. So the way that we do that is we take our attention scores which recall are these uh these numbers a a11 a12 a13 a14 they're all between 0 and 1. They sum to one. We're going to take a linear combination now of the encoder hidden states h1 h2 h3 h4. Um and take a linear combination of those encoder

hidden states weighted by our attention scores. Um and this will give us a new a context vector that we're calling c1 here in purple. um which is going to summarize the information in the encoder sequence um in in some way that's modulated by that by those by those attention weights. Um and now at this point right so then this this C1 is basically some linear combination of the input encoder states H1 to H4 things look basically the same as they did in the non-attention case. So we we have our context vector um we concatenate it with our in with our first token of the output sequence y0 pass that to our recurrent unit um to get both to get um to get our uh next hidden state of the decoder recurrent neural network as well as the first output token from the decoder recurrent neural network. Um so basically the structure of that decoder RNN did not really change. Um all we did is rather than set we've computed the context vector in a different way using

this attention linear combination mechanism. But now crucially um right so the intuition here is that this context vector basically attends or looks at different parts of the input sequence that is modulated by whatever the output RNN wants to look at at this moment in time. Um so for example you know part of the input you know as uh part of the input sequence has this token these these two words we see. So then in trying to produce that one word in Italian that corresponds to we see the

network probably wants to go back and look at those two words in the input sequence in order to know what output word to produce. So we might expect we we might want to have some we might expect that intuitively when trying to produce the word um vendo um then the network will want to look back at the words we see and put higher attention weights on those and it doesn't really care about the sky because those words are not necessary for producing that vendamo output. Um and that's the kind of intuition we're giving the network the ability to look back at the relevant parts of the input sequence for the word that it's trying to predict at this moment in time. Um and the other thing to keep in mind is that this is all differentiable. Um we don't need to supervise the network. We don't need to tell it which words in the input sequence were required for each word in the output. Instead, this is just a big computational graph composed of differentiable operations.

Um this all of this will can be learned end to end via gradient descent. So at the end of the day, we're still going to have this, you know, cross entropy softmax loss where the network is trying to predict the tokens of the output sequence. And in the process of trying to predict the right tokens in the output sequence, it's going to learn for itself how to attend to different parts of the input sequence. So that's that's really critical, right? If we have to go in and supervise and tell the network the alignment between the two, it would be very difficult to get training data for this kind of thing. The question is how do we initialize the decoder? Uh we're actually using the word you got to be careful we're using the word initialize a little bit overloaded here. So one question is the decoder is itself a neural network that has weights. When we start training that network we need to initialize those weights in some way.

So then we will typically initialize the weights of the decoder randomly and then optimize them via gradient descent just as we do with any other neural network weights. Um but there's a second notion of initialize which is that when the network is processing a sequence um whether whatever its current value of the weights are we need some way to set that initial hidden state at the time we start processing an output sequence. Um and in that case we need some rule or some some some way to set that initial hidden state of the decoder output sequence. Um there's a couple different mechanisms for this. Um sometimes you might initialize it as the hidden the last hidden state of the encoder is one thing you'll sometimes do. You might have a linear transform that um projects has some learned projection from the last decoder state to the first from the last encoder state to the first decoder state. Um or sometimes will people even

initialize the first hidden state of the decoder to be all zeros. Um any of those will work as long as you train the network to expect that kind of input. So the question is negations and exors would this cause a problem? Maybe this is this is a this is a hard problem but then you need a lot of data a lot of flops to try to hope the network can disentangle this. Um but basically recurrent unit takes three things as input. It take in the decoder right it takes the previous hidden state the previous decoder hidden state it takes the current context vector and it takes the current um token in the output sequence and then from that we produce the next hidden state and then from the next hidden state then we go and predict the output token. So that's actually the same setup as in the non-attention case. I guess there there's an implicit connection from there's a there's a connection from S0 to S1 that we're not drawing. So there there should have been

another arrow from S0 to S1. I think I just dropped the S0 arrow. So sorry about that. Well, we're basically letting the network decide for itself to look back at any part of the in input sequence that it thinks might be relevant for the task at hand. Um so but the reason why we think that this mechanism is plausible and might be helpful for the network is because we know that you know in a language task there often is some kind of correspondence between words in the output and words in the input and we want to let the network kind of look back and pick out which are the relevant bits of the input for producing this bit of the output. But again we're not directly supervising it. We're not telling it how to use these attention scores. But the intuition is that we think that's a plausible thing that it might choose to do given this mechanism. Okay. So that's that's sort of like one tick of the output. Um, and now basically we do it again. We do this

whole process again for every time we tick the decoder RNN, right? Remember the problem we were trying to solve is that previously the decoder was bottlenecking through a single vector. Um, now we're going to compute instead of bottlenecking through a single vector, we're going to repeat this whole process again and compute a new context vector for the second time step of the decoder and go let it go back and look at the whole input sequence yet again. So now um basically given our S1 which is our computed first hit like hidden state in the decoder we're going to go back you take S1 go back and compute comparison and and use our attention uh mechanism to compute similarity scores between S1 and all of the hidden states in the encoder. Um that will compute our similarity scores using that exact same fat that same linear projection that we used at the first time step. um compute these alignment scores again, cram them through softmax to get a a new

distribution over the input sequence for the second decoder time step and now compute a new linear combination of the encoder hidden states now weighted by this new distribution that we computed at the second time step. Um and this sec this this will basically give us a new context vector um C2 that now is a different summarization of the input sequence that's now computed as a new linear combination of the input encoder hidden states and then we then the whole thing kind of iterates right we have a new context vector we use that to run another tick of our decoder RNN unit that will now now does include that that mysterious missing arrow that wasn't there on the previous time step. So then given our new context vector, given the next token of the output sequence and given the S1 hidden state of the decoder, we compute a new decoder state S2 and then from that compute another token of the output sequence. Um and again remember you know in this case

it's producing ill which maybe is the according to the slide. I hope that's true. Um, and then you know in this case there's maybe a one to one correspondence between the word the the network is trying to produce for this sequence and one of the words in the output and one of the words in the input. So we might expect that the network should put relatively high attention weight on just one of the words in the input sequence and relatively low attention weight on all the other words in the input sequence. But again, we don't supervise this. The network is deciding for itself how to make use of this mechanism all driven by gradient descent on our training task. Um, and this whole thing is going to we're just going to repeat that whole process for every tick of the decoder RNN. Um, so now this basically solves our problem right there. We are no longer bottlenecking the input sequence through a single fixed length vector. Instead,

we have this new mechanism where at every time step of the decoder, the network looks back at the entire input sequence, reummarizes the input sequence um for the to generate a new context vector on the fly for this one time step of the decoder and then uses that to produce the outputs. So this is a this is a pretty cool mechanism and this is called attention because the network is attending or looking at different parts of the input sequence um at every at every moment in its output. So we talked about these attention weights and we said that they were driven that the network was learning for itself how to set these attention weights based on its training data based on its training task. Um and another really cool thing about attention is it also gives us a way to introspect and see what the network is looking at as it's trying to solve this problem. So we never told it how the what what what what what the what the alignment was between the input

sequence and the output sequence. But by looking at the attention weights that that the network predicts when trying to solve this task, we get a sense of what the network was looking at um while trying to solve the problem. So that gives us a way to interpret the processing of the neural network in some way. Um and so here's so one thing that we can do is then go and look at in the process of producing a particular of processing a particular sequence what were the attention weights that the network predicted when trying to do this task and we can visualize these in a two-dimensional grid. So here we're looking at an example of English to French translation. Um and across the and across the the top we have our input sequence. The agreement on the European economic area was signed in August 1992. And then running down the rows is the output sequence which is in French which I will not attempt to pronounce. Um but you can see that like basically through

this attention mechanism for every remember the way this attention mechanism worked is that each time the network produced one of these words in the output sequence it predicted a probability distribution over the entire input sequence. So we visualized that in that first row. So if you look at the first row of this matrix, we're visualizing that predicted probability distribution over the entire input English sentence. And we see that when trying to predict that first um word the of the French sentence, then it puts a lot of probability mass on the English word 'the' and basically no probability mass on any of the other words. Then when predicting the second word of the output sequence, remember it goes back and predicts a new distribution over the entire input sequence and that's going to be the second row in this matrix. So you can see that accord um I it puts a lot of probability mass on agreement and then no probability mass anywhere else.

So then that gives us some sense that the network actually kind of did figure out the alignment between the input words and the output words when doing this translation task. And there's some interesting patterns here that here that kind of pop up when we look when we see diagonal structures in this attention matrix. That means that there was a onetoone correspondence between words in order between the input sequence and the output sequence. So in particular we see that the agreement on the the first four words of the input sequence correspond to this diagonal structure um in the attention matrix. So that means that the network has decided for itself that these first four words of the input sequence sort of align or match up or correspond to the first four words of the input sequence. And the same thing for the last um several words. So again, we see this diagonal structure at the end of the sequence, which means that August 1992 or in August 1992 um

corresponds to these uh these last couple words in the French sequence. And again, there's this one:1 correspondence between words in the output and words in the input. But we see some other interesting stuff in the middle here. So in the middle, um we see European economic area, but in the French, we see words that look kind of like those in a slightly different order. Good question. How does it figure out the grammar? That's the mystery of deep learning. But like basically we told the we didn't tell the network anything about grammar. We told the network we supervised it with a lot of input output pairs. We told it here's an input sequence in English. Here's an output sequence in French. Here's a mechanism for processing this and learn via gradient descent to to like set the weights of this architecture in order to in order to produce this output from this input. Um we'd never told it anything about grammar. Um but it kind of because we as

human designers have this intuition that maybe it makes sense that there ought to be some correspondence between some of the words. So we bake in a mechanism that we think as human designers might be helpful for solving this problem and the network figures out for itself in the process of doing the endto-end task how to make use of that mechanism um to solve the problem we set for it. And it's pretty pretty amazing that it works. Um right but in this case you know it kind of figured out some of the grammar for itself. So it sees that you know we see this nondagonal sort of backward diagonal in the attention matrix here and that means that the network figured out for itself this um other this like different word order between words in English and words in French um or in the middle you see there's a little there's a little like 2x2 grid um kind of here and that kind of corresponds to a situation where there might not have

been a one to one correspondence between the English words and the French words. There might have been two French words that corresponded to two English words and they didn't perfectly disentangle perfectly. I mean the network just all figures out this for itself over the process of training um on a lot of data and putting a lot of compute through this and that's pretty cool. Okay, so there's actually uh so that that's basic so and this actually was the initial usage of attention in machine learning. Um it actually came from this from these machine translation problems. Um so this was from a paper back in uh back in 2015 uh neural machine translation by joint by jointly alerting to align and translate. Um, and this paper actually just won the runner-up test of time award at iclair 2025. Uh, so that's pretty cool. A nice nice uh, you know, this has been a really impactful paper over time. Um, but it turns out that there's actually a more general idea here and a more

general operator hiding here. You know, we approach this problem from the perspective of trying to fix our recurrent neural networks. But it turns out the mechanism that we used to fix the recurrent neural networks actually is something general and interesting and really powerful in its own right. So now we want to try to pull that out, pull out this idea of attention and divorce the idea of attention from the recurrent neural networks. And it turns out that attention will be a very useful and powerful computational primitive for neural networks in its own right. even even if then we can cut away the recurrent neural network part and just be left with attention as the core primitive in our architecture and that's kind of where we're what we're going towards. So now what we want to do is take this this idea of attention as we saw it in recurrent neural networks and try to generalize it and try to carve out this independent operator that can be used on its own. So let's think about

what this attention mechanism was doing. Basically what this attention mechanism did is there were a bunch of query vectors. These are Well, maybe maybe it makes sense to talk about these in the other order. So, there's data vectors which are like data that we want to summarize. These are the the the the encoder states of the encoder RNN. So, we have this input sequence and we've summarized that into a sequence of vectors. Um, and the sequence of vectors is sort of data that we think is relevant for the problem that we're trying to solve. Um, and now in the process of trying to make use of that data, we want to produce a bunch of outputs. And for each output, we have a query vector. A query vector is a vector that we're trying to use to solve an out to to produce some piece of output. Um, and in this case, the query vectors are the hidden states of the decoder RNN. Um, and we have this this property that for each query vector, we want to go back look at the data vectors and

summarize the information in the data vectors into a context vector. Um, for each well, okay, from the purpose of from the purpose of attention, this gets a little bit weird. So the output of the attention operator are the context vectors that we just talked about for the RNN. So if we're talk if we're thinking about just what does that attention operator do? The output of the attention operator were the context vectors that we feed into the RNN. So then what is the attention operator doing? The attention operator is taking a query vector going back to the input data vectors summarizing the data vectors in some new way to produce an output vector. Um and that's what the attention operator is doing. Is that does that is that does that kind of make sense as a generalization of this attention mechanism that we just saw? >> Yeah. Yeah. I I'll repeat it again because it's it's tricky. There's a lot of stuff flying around here. A lot of boxes and we're changing the words that

we're using to define the the define the boxes. So I get it. There's a lot happening. Um so what the attention operator is doing is there's a bunch of data vectors which are the encoder hidden states. Um then for then we have a bunch of query vectors which are the p the things we're trying to produce output for. Now, in the process of processing a query vector, we're going to go back to the data vectors, summarize the data vectors in a new custom way for each query vector, and that will produce um an output vector, which is the context to be fed into the next tick of the RNN. Right? So, our query vectors are these guys in green. For each query vector, we go back to the data vectors, summarize the data vectors, and then produce a new output vector, which is one of the contexts that we then feed into the the rest of the network. So um you know this is kind of tricky because we're trying to like go into this architecture and like cut

carefully cut out the attention part and cut it out from the RNN. Um so then we're going to try to like walk through this again from the perspective of just the attention operator. So from the perspective of just the attention operator we're going to start with just one query vector at first um which is you know one of the one of the states in our RNN. We also have a bunch of data vectors which are the encoder hidden states in the RNN. Now the computation that we want to perform is first compute similarities between that query vector and all of the data vectors. This is the exact same thing that we just saw just sort of written in a different way. So we use this fat function to compute these similarity scores um from our to to to compute similarities between each data vector and our one query vector. Then once we have those similarities, we're going to squash them through through a softmax to get attention weights. And this will be a distribution over the data vectors that has been

computed on the fly for this one query vector. Then we want to do is produce an output vector. And out this output vector is a linear combination of our data vectors where those linear combination weights are the attention scores that we just computed. So this is the output of the attention layer. And then in the context of the larger RNN that we saw, the output of the attention layer or the attention operator will become an input to the next tick of the decoder RNN. But we're trying to deprecate the RNN. So we don't want to talk about that. We just want to talk about the attention and focus on the computation happening inside the attention layer. So like so this is basically the operator that we saw in the RNN, right? We had this one like we we did this process over and over again of taking a query vector using it to compute similarity scores getting attention weights getting an output vector. Then we got a new query vector. Where did that query vector come

from? Attention operator doesn't care. Get a new query vector. Go back summarize the data vectors get a new output vector. Um and that's that's the core of the attention operator. So now let's try to generalize this and make it a even more powerful computational primitive. Yeah. So in principle this um this fat doesn't have to be it could be any function. It could be any function of two vectors that outputs a scaler um in principle but in practice we're actually going to make it simpler in in a couple slides. But in principle yeah you could just slot in any any function that you wanted there. Okay. So the first generalization that we're going to do is actually um the opposite of what you just suggested and make that similarity function simpler. So we said in principle it can be any function that takes two vectors and gives a similarity score. What's the simplest possible function that inputs two vectors and gives us a scalar similarity score? It's a dot product. So

we want to try to make things uh simpler and also generalize at the same time. And it turns out that a dot productduct is is good enough of a similarity score to be used for this purpose. So the first thing we're going to do is um actually just only use dot products to compute similarity. Um but it turns out there's a slight problem with dotproducts. So and this one's kind of subtle because there's a weird interaction between the dot product and the softmax. Um and that has to do with what happens when the when the dimension of those vectors scales up or down. Right? So if you have a like the motivating example is that if you scale the dimension of that vector, say we had a constant vector of all ones of like dimension 10 versus a constant vector of all ones of dimension 100, then as we go to the to the higher dimensional vector, then when we compute the sum inside that softmax, then we're going to be dividing by a larger number. So we'll end up with more squashed

probability scores as we go to higher dimensional vectors. Um that can lead to vanishing gradients as we just saw in the previous lecture. and prevent learning of this whole thing. So as kind of a slight hack to prevent that um and make this architecture more generally more generalizably scalable up and down to different dimension vectors um what we're going to do is actually not use the pure dot productduct but scale the dotproduct down by the square root of the dimension of those vectors that we're looking at. Um and this is just a way to prevent vanishing gradients and give nicer gradient flow through the softmax for a wider range of dimensions of vectors. Um, and this turns out to be very important because as we make these networks bigger and bigger and bigger over time, we want to get higher dimensional vectors because that gives us more compute, more capacity. So we always want to think about how our architectures will scale as we make the parts of those architectures get bigger

and bigger and bigger. So this um this this scale dotproduct is actually really important for preventing vanishing gradients here. Yeah. Question was we're limited to data and query vectors of the same size, but we'll actually fix that. So uh our first generalization was to use actually scaled dotproduct similarity as our as our similarity measure. Um so now you know if we go back and look at the shapes of these things we have one query vector of dimension dq. We have data vectors of dimension nx by dq as well because it's a dot productduct they need to match. Um but there's actually a next generaliz generalization that we're going to do is have multiple query vectors right like maybe we don't want to process just one query vector at a time. We want to have the ability to process a whole set of query vectors all at once. Um, and this kind of happens in the RNN. You know, we did end up with a bunch of query vectors. Um, and it's useful for the

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent