So guys, we are going to continue the discussion, uh, with respect to our Transformers.
So already inside a encoder, we have discussed how the self-attention exactly works.
Um, and again in our previous video we have spoken about query key value vectors and then how our score
is basically calculated attention score, how a softmax activation function is calculated, how our
softmax function is activation function is applied.
Everything we discussed right now after we get the vectors.
Now see in this scenario, right when I spoken about Multi-head attention, for every word, I'll be
getting like attention head one.
So I'll be writing z one, z two, z three, right z four, z five, and so much like it can be 7 to
8 z's, right?
So if I say there are eight attention heads, I'll be getting 8Z1, z two, Z3Z fours.
Uh, a kind of vectors by passing this specific word.
Now the next step will be that I need to pass all this to my feed forward network.
Feed forward neural network so that, uh, I'll be able to do the further steps right inside this particular
encoder.
Now, the problem is that before what we had drawn in the diagram here, I said that, hey, we are
just passing one word.
I'll be getting one vectors.
right.
But here we are getting multiple vectors.
So before we pass through the feed forward neural network, we need to combine all these specific vectors
and send it to the feed forward neural network.
And then probably do the forward and the backward propagation.
So um, so the feed forward neural network is expecting a single matrix.
So we need to find out a way how we can condense all this attention head into a single matrix, along
with some, uh, a single weights itself and probably do the entire task.
So here you'll be able to see, uh, right now I have multiple attentions, uh, like this.
Attention, head zero, attention head one till seven.
And here I have all the values, right?
I have over here, I have over here, I have over here.
All these values are specifically there.
Okay.
Uh, and when I say values here you have this skew curve.
Here you have separate q k v right here also you have a separate q k v.
Now the problem starts at how do we combine all these things before we forward it to the feed forward
neural network.
So for that here you can see an amazing diagram.
We concatenate all the attention head like this.
So Z0Z1Z2, Z3Z4, Z5Z6.
At seven we are considering that there is a seven head attention Okay, so we combine all of them.
We multi we do a dot product with W of zero.
So this will be my W of zero where I will be initializing a new weight for my feed forward neural network
feed forward neural network.
And after this we go ahead and calculate our final Z.
Okay.
So after this whatever z we are probably getting over here.
This will be a single Z.
This will be my another Z.
Right.
And this is what we are going to pass it to our next encoder.
Right.
So all the multiple attention head, we are just going to combine it together to make sure that we capture
all the specific importance.
We give it to the feed forward neural network.
And along with this we train a specific weight itself.
And this is just for one word.
Just imagine the one word that I've given the right.
Or it can be cat.
So from this cat, by using self-attention, I've created so many different heads right.
By using.
And all these heads will have a separate key value.
Key value vectors.
Everything will be separate, right?
Then.
Along with this, I'm taking a weight forwarding it to the feed forward neural network.
Then we'll train it to finally get our output that is called as Z.
Okay.
And this is pretty much all, uh, to the multi-head self-attention.
It is a handful.
It is a handful of matrices.
Okay.
And you'll be able to do it uh, probably in this way.
Overall, if you really want to check one more amazing diagram again from the same blog, I will also
draw it for you over here.
And this is how the things happen right from the entire self-attention till the feed forward neural
network.
Okay, so here you can see that from this part to this part, right from here or from here, the entire
feed forward neural network actually takes place.
So here is your m two words that I have given thinking machines.
Then for all these words you have this separate separate weights.
So that will be able to get our separate q, k and v vectors.
And finally you get your z zero.
This is for the attention head one.
Then again for this you have a separate vectors.
For this you have a separate vectors right.
And at for all the uh, for all the attention head, you will be having a separate, separate key.
Uh, Q queries key value vectors itself.
And finally you get this entire weights and you train it on a feed forward neural network to finally
get your Z.
So this was my initial vector.
This is what is my final vector that we are specifically getting.
Okay.
So this is our input sentence.
We embed each word split into eight heads.
We multiply uh x or ah with weight matrices.
You calculate the attention using the resulting matrix.
Then you concatenate the result of z matrices and multiply with the weight matrix w two to produce the
output of the layer.
So with respect to one word I will be able to get this.
Like let's say thinking is there.
Then I'll be able to get this vector machine is there.
I will be able to get this vector right.
So this overall explains about the entire attention head.
The the reason of having multiple attention is that I really want to show you with one practical example.
Okay.
So here you can see.
I have uh, over here one visualization diagram.
I'll be giving this link in the resources section.
Okay.
Here you'll be able to see that with respect to every word.
Right.
And uh okay.
This will not be a good diagram.
So here you can see I have multiple attention.
So this can be my attention.
Head one attention two head.
Attention three head.
If I click on each and every attention, you'll be able to see that every every attention has a different,
different importance word.
Right?
It is trying to capture much importance with respect to all the words that we have.
So if I go ahead and click this in my other attention, this can be my other attention.
This can be other other attention head.
You know, it shows the importance of each and every word.
So if I just go and highlight over here, see, Li is basically very much highly dependent on on.
Right.
So this vectors that will be defining this layer will be specifically on on right.
And similarly if I go ahead and click on other vectors let's say on is their cat, is their C mat is
their separator, is their cat and mat are highly correlated.
Right.
Correlated basically means there is a huge, uh, context dependency on that.
Right.
So this actually gives an idea.
And here also you can go ahead and check it out with respect to sentence to a to a sentence A to B.
Let's say the cat sat on the mat.
The cat lay on the rug.
If I want to probably see is there any dependencies?
So cat is having a dependency on cat.
If I probably consider sentence a to a cat sat on the mat.
So C on sat is the first word that it is having a dependency on.
So it's the main idea of self-attention is to get this all right.
And with respect to different different head we will be able to get this.
Some more examples which I will be putting it here.
Right.
You can probably check it out.
So this is one attention head which you will be able to see.
So here I have a sentence.
The animal uh, did cross the street because it was too tired, didn't cross the street, it was too
tired.
So here you can see when I highlight on it, it is dependent on street tired and animal.
Right.
So some more words are specifically here.
Similarly, if I go ahead and see with respect to other attention, head right.
Once we calculate, you will also be able to see this kind of context.
So here it is dependent on the animal with different different color cross right.
So multiple attention head basically means you are able to get information from multiple heads itself
with respect to the dependency on all the other words.
So, uh, this was it, uh, where we have specifically discussed about Multi-head attention.
We have discussed about self-attention and all.
Still, there are many more videos that are going to come.
Couple of videos.
One is positional encoding, which we are going to discuss.
And after that we will also go ahead and discuss about the entire transformer architecture, because
still we are just understanding some of the components in transformer.
Right.
So yes, this was it from my side.
I hope you liked this particular video.
I'll see you all in the next video.
Thank you.