# Lecture 21.2: Introducing Attention & Language Translation

[Recording](https://www.youtube.com/watch?v=dGfUQeMXT6U)

Automatic transcript; technical terms and equations may contain recognition errors.

00:00:00 We are getting into more and more complicated stuff now.

00:00:05 And the field is just evolving so fast.

00:00:08 So what are we talking about?

00:00:10 So I think we are now in the...

00:00:19 That's where we are, right?

00:00:22 And we started talking about the modern RNNs like RSTM and RNNs.

00:00:30 I'm sorry, GRUs last time.

00:00:32 And then we also talked about how the natural language processing field has been evolving

00:00:39 beyond 2013-15 when RNNs were dominant.

00:00:44 And then I think there are the two paper 2017,

00:00:48 Attention is all you need.

00:00:50 And since then, we have been paying much more attention now to transformers.

00:00:56 And that's why we've been talking about attention and we'll talk about self-attention today.

00:01:03 Okay?

00:01:04 So that's where we are.

00:01:05 And I think we need one more lecture next Tuesday because there are a lot of things about this transformers.

00:01:12 I'm not covering nearly enough, but this is the 766.

00:01:18 It's not a course about advanced deep learning.

00:01:21 So maybe we should introduce 689 to cover the advanced deep learning.

00:01:26 So the pre-training and the BERT and protein language model, we'll probably talk about it on Tuesday.

00:01:35 Okay?

00:01:36 Okay, let's continue.

00:01:38 So let me remind you where we are.

00:01:43 So we were talking about attentions and the way we started was here, basically.

00:01:52 We're looking at one example, right?

00:01:56 So let's see.

00:01:59 Let me have, it's right here.

00:02:01 This is one example, right?

00:02:03 So, and remember there was a slide talking about modern RNNs.

00:02:07 We talked about RSTM, RGRUs.

00:02:09 We did talk about the many details, right?

00:02:11 But there's the autoencoders.

00:02:13 I'm sorry.

00:02:14 Autoencoders or in this case, decoder, encoder decoder system that I didn't talk about.

00:02:21 But basically here, I'm looking at image captioning.

00:02:24 So there's encoder and there's a decoder, right?

00:02:29 So input is image and output is a sequence, right?

00:02:34 So we said what the problem was that if you look at this context vector, right?

00:02:40 Which is a summary of the feature vectors, right?

00:02:44 And that is posing two problems.

00:02:48 One is if, you know, this feature vectors is very large for a lot of pixels and the sentence is very long, right?

00:02:58 So it's kind of too simplified of you to have that kind of mapping.

00:03:02 And secondly, if you look at this C, it's fed to different time point on the decoder part and generating the different words.

00:03:12 So that is inadequate.

00:03:16 So that's why we talked about, you know, how our eyes were actually rolling so often to attend so that the light will pass through our eyes and reach one portion of the routine.

00:03:31 And that's extremely sensitive to the light, right?

00:03:34 So we have to do some kind of attention, right?

00:03:35 Right?

00:03:36 Just like when you are driving, sometimes you know that.

00:03:38 What's something you have to pay attention to?

00:03:41 The blind side, right?

00:03:45 So you have to somehow, you know, turn your neck, right?

00:03:49 So we humans do attend a lot.

00:03:54 So the same thing here, right?

00:03:56 Can we introduce some kind of, you know, attention mechanism?

00:04:01 Basically what it's doing is that, as I said last time, you actually have a C, right?

00:04:06 That is time point specific.

00:04:09 So for different time point of the decoder, you see that I'm calculating different C, okay?

00:04:16 And when I'm calculating each C sub T, what I'm doing is actually, you know, I'm calculating sort of an alignment matrix

00:04:25 corresponding to the feature matrix, right?

00:04:30 And then I do the softmax and then this gives me the attention matrix.

00:04:35 This is basically telling me which part of the feature matrix we're using a lot while getting this C vector, right?

00:04:45 So this is essentially about how the hidden vector aligns with the feature matrix, okay?

00:04:54 So we'll see again in future how this hidden vector aligns to the input basically, right?

00:05:00 So this hidden vector will give a different name when we are doing the self attention

00:05:05 because here this is attention essentially, you know, across the input and a hidden vector, right?

00:05:12 But when we talk about the self attention, this hidden vector will be replaced by something else, okay?

00:05:16 We'll talk about it.

00:05:17 Okay, so we also talked about the interpretability of this attention mechanism because it's not only giving you the accuracy, right?

00:05:28 But it also gives you the interpretability.

00:05:30 You can see, right, based on the soft or hard attention, whether it's a weighted average or the max,

00:05:36 you can see where when generating each word of the sentence where the model is paying attention to in the original feature matrix, right?

00:05:48 So this is very clear.

00:05:49 And another example, I think that's where I ended up last time, right?

00:05:54 For instance, you can see that this is making a lot of sense, right?

00:05:58 When generating the word frisbee, the attention is paid here a lot.

00:06:02 So the light, you know, the brightness tells how much the attentions were paid across different feature maps.

00:06:10 In this case, it's the pixels, okay?

00:06:12 And this is where the attentions were when generating dark, right?

00:06:17 So this is the attention matrix for the corresponding time point T, or in this case, T equal to 2 for the second word, right?

00:06:24 The stop sign, and then a little girl, and so on, right?

00:06:28 So this makes a lot of sense, right?

00:06:31 And I think being able to explain your model can help you remove some kind of, you know, bias that you don't know about.

00:06:47 We think that our models are just data driven, so there's no way that we are biased, but don't forget that.

00:06:53 You are the person who decide what data to train the model and how you train the model.

00:06:58 So your models could be biased, right?

00:07:02 And explainability can actually detect such bias.

00:07:06 In this case, for instance, you know, if you are generating this caption, right?

00:07:12 And the second word is, you know, a man, right?

00:07:17 For instance, a wrong sentence could be a man sitting at a desk with a laptop computer.

00:07:23 When you realize that there's something wrong because apparently it's not a man, it's not a woman, right?

00:07:27 And you wonder, you know, where is that bias coming from?

00:07:31 You notice that attention when generating the second word was actually mostly here, right?

00:07:38 It's like here, right?

00:07:40 Like a monitor, right?

00:07:42 That's clearly not intrinsic to whether it's a man or a woman, right?

00:07:47 This probably reflects some bias in your data and the way you're training models.

00:07:51 So in their model, this is, you know, in this paper, right?

00:07:57 You know, it's generated the correct sentence, but more importantly, you are generating the correct prediction for the correct reason, right?

00:08:06 Woman, right?

00:08:07 You know, the key features are here, right?

00:08:09 Same here like a man holding a tennis record when it's actually a woman.

00:08:13 And then, you know, you see that in the wrong model or the model with less accuracy, this bias is actually showing that it's not really using anything intrinsic to the gender difference, right?

00:08:27 So this is, you know, the start of a series of paper.

00:08:33 So basically, you see that they are all using attention mechanism, right?

00:08:36 Remember, I remember in 2017 or 18 when we started doing the protein or drug target affinity prediction, right?

00:08:44 We used the sick to sick, you know, RNN encoder, in our case, GRUs or RSTM actually, and then RNN decoder, right?

00:08:54 And then you have attentions across encoder and decoder.

00:08:58 That's what we used back in 2017 and 18.

00:09:01 And that all started with this paper, show, attend, and tell, right?

00:09:07 So actually, after that, there are a bunch of papers, which basically titles are X, attend, and Y, right?

00:09:14 So you basically see that, you know, Y is a task and X is something related to the context.

00:09:23 And they are all using this kind of attention mechanism.

00:09:25 There are a bunch of papers using the similar title, okay?

00:09:30 Now, I want to show this attention mechanism again once more using a second task, okay?

00:09:38 Not image captioning, but language translation, okay?

00:09:42 I want you to make sure that you really understand this encoder-decoder, okay?

00:09:48 Pay attention to this encoder-decoder system, right?

00:09:51 In our case, with image captioning, the input is image, right?

00:09:55 And we use a CNN to encode the image, right?

00:09:59 And then we have these hidden vectors, and then we have this decoder.

00:10:03 This decoder is using this kind of recurrent, right?

00:10:11 Architecture and pay attention to what is input Y0, Y1, Y2, Y3, and Y3, and what is output?

00:10:19 Y1, Y2, Y3, and Y4.

00:10:21 Okay, make sure that, right?

00:10:23 For instance, in the beginning, when the input Y0 is just the start, and then the only thing I have is the context vector at time point one,

00:10:33 I'm supposed to predict that the first word that is person, right?

00:10:39 Right?

00:10:40 And then with the first previous word, and, you know, the previous input, and the new context, I'm supposed to predict wearing, right?

00:10:48 The second word.

00:10:49 So, now let's change the application to language translation, and make sure that you understand this encoder-decoder system, okay?

00:11:02 So, this is translation.

00:11:04 So, this is French, right?

00:11:07 And then the output would be here, right?

00:11:10 The input is French, and the output would be a sequence Y0, Y1, Y2.

00:11:15 I think Y1, Y2, Y3, and Y4.

00:11:17 So, for instance, okay?

00:11:19 So, just keep in mind that this is our input, and this is our output.

00:11:24 I'm not plotting it yet, okay?

00:11:26 And, basically, we'll have, again, an encoder here, and a decoder here, okay?

00:11:33 Like the seek-to-seek is an encoder-decoder system, where the encoders and decoders are simply RNNs, such as RSTM or GRUs, okay?

00:11:43 So, in our case, let's look at, you know, this example again, and see how this attention mechanism can be embedded into this encoder-decoder system, okay?

00:11:53 Again, we have the hidden state of the encoder, right?

00:11:58 Then we have the, you know, H0, which could be the last hidden state, which could be the last cell state, depending on what unit you are using, GRU or LSTM.

00:12:09 And then, given the H0, again, this is before you introduce attention mechanism, right?

00:12:16 There's only a single context, right?

00:12:18 A single context, and you're feeding this single context.

00:12:22 And then, you have the start, this is very similar to what we had in the image captioning, because, you know, the, because of the need of the output, their decoders are essentially the same, right?

00:12:34 Did you see that the decoders are the same for these two applications, right?

00:12:37 And, again, Y0, Y1, Y2, Y3 are my input to the decoders, and I'm supposed to get a Y1, Y2, Y3, and end for the decoder output, okay?

00:12:48 And, again, this is the same problem you see before, that I have a fixed context, which is sort of the, you know, summary of how I should utilize the feature map of the input while generating the output sequence, right?

00:13:05 This is a very fixed, and this is the bottleneck, right?

00:13:08 So, now you should know, in this example, how we introduce attention mechanism, right?

00:13:13 So, we need to generate, again, again, in this case, right?

00:13:19 So, you know, start with this, right?

00:13:22 We can get sort of the alignment score.

00:13:25 Now you understand why I used to call it alignment score, right?

00:13:28 Because starting from NLP, it's like which word of the input aligns to which word of the output to what extent, right?

00:13:36 So, that's why it was called alignment score, right?

00:13:39 And then this alignment score needs to be normalized into the attention, right?

00:13:45 Then I have the attention over the whole sequence.

00:13:47 Now, this is not a matrix because it's a sequence, not an image like in the image captioning, right?

00:13:53 And if you look at the math here, right, I'm finding this alignment or later on similarity between H at T minus 1 and every Zi, okay?

00:14:07 Every Zi, okay?

00:14:09 And I could basically add each time point T, right?

00:14:13 The time point is corresponding to the position of the output sequence.

00:14:17 At every time point T, I'm asking what is alignment of the, you know, previous or the last, right?

00:14:27 T is present and the T minus 1 is the last.

00:14:30 The last hidden state and each part of the feature map, okay?

00:14:36 If I call this Zi the feature map, where i is the position of the input, okay?

00:14:42 As I said before, this alignment function can be, and actually started that way, parametrized by an MLP, okay?

00:14:53 And then we can normalize them using softmax, right?

00:14:57 And now the scores are between 0 and 1 and, you know, for every given time T, we have some normalization factor, condition satisfied, right?

00:15:08 They sum up to 1, and this is for every given time T, we sum over i and j, and then we give 1, right?

00:15:16 So, and then, now we have a weighted average for at time point T, right?

00:15:22 So, we have the weighted average, which is the context.

00:15:25 So, we are basically saying that at every time T, right, we ask what amount of attention A is spent on each part of the feature map, okay?

00:15:38 Each part i of the feature map, okay?

00:15:40 T is given, and then we sum this over all the i, right?

00:15:45 Over all the portions of the feature map, and that's giving us the average.

00:15:49 And this is the context for time T, okay?

00:15:52 So, that's what we are doing here.

00:15:55 Very similar like what we did before in the image captioning, okay?

00:15:59 Now, the decoder part, right, as we said before, right, at every time, we are feeding two things to the current character generation, right?

00:16:14 We have the current context C sub T, which was just introduced, and we also have the Y zero, or Y one, or Y sub T minus one, right?

00:16:26 Y sub T minus one, and this is C is replaced by a C sub T already, right?

00:16:33 And then we have also the input from the previous, okay?

00:16:38 So, that's why it's H sub T minus one, okay?

00:16:41 So, I think there's a missing link here, right?

00:16:46 So, basically the three inputs together make the first output, right?

00:16:51 Similarly, for generating the second word, wearing, I'm actually using the current context, and the Y one, that's the first word, person, and I'm using the hidden state of the decoder, right?

00:17:06 The last hidden state, H one, the previous hidden state of the decoder, right?

00:17:11 So, that's what this expression is about, okay?

00:17:14 All right, so, here we are, you know, this is what, you know, I think C is now replaced by C sub T.

00:17:25 That's what this attention mechanism is about, okay?

00:17:27 Where the attention was from is this weighted average, okay?

00:17:32 This weighted average.

00:17:34 So, this context is a weighted average of the feature map, and how much weight you put on each of the data?

00:17:40 The weight you put on each part, I, of the feature map is attention, okay?

00:17:45 All right, so, now let's do a similar visualization for interpretability, and I said, I said before that, you know, at the beginning, I call it alignment matrix or alignment scores.

00:17:56 You may not fully understand it, but in NLP, it should be much more understandable, but I want to show you something more than that, right?

00:18:03 This can be like the visualization for interpretability that I showed you before in the image captioning, but I'm now looking at the translation, right?

00:18:13 This is a translation between, is this French?

00:18:19 Is it?

00:18:20 Some of you speak French and English, okay?

00:18:24 So, what it's doing here is basically, you know, for instance, if you're translating from French to English, right?

00:18:31 For translating, you know, to get each word, right?

00:18:36 For instance, you're translating from the row words to the row sequence to sentence, or to the column sentence from French to English, to get each output word, how much attention I put on the word.

00:18:53 Okay?

00:18:54 So, you see that there's, you know, the brighter, the more attention, and there's a nice diagonal, right?

00:19:03 Showing that there is some, you know, correspondence between the French and English with regard to the word order, right?

00:19:15 So, there is such, but notice that it's not strict.

00:19:19 Let's look at this one, right?

00:19:20 For instance, you know, zone, so in English you say European economic area.

00:19:27 In French, you know, part of my French, you see that you're in this zone, economic, European.

00:19:34 So, apparently the order is not the same, right?

00:19:37 And the model was actually capable of figuring that out.

00:19:41 You see zone was corresponding to area, right?

00:19:44 Instead of European.

00:19:47 Economic is economic, and European is corresponding to European.

00:19:51 So, it's not exactly diagonal, right?

00:19:54 So, in other words, you know, without supervising the attentions, without telling the model directly where you should pay attention, the model learned the order, right?

00:20:09 For different languages, okay?

00:20:12 So, that's something to keep in mind, okay?

00:20:16 Any questions so far about attention mechanisms?

00:20:20 We use the two examples, right?

00:20:23 Both using the encoder-decoder architecture, okay?

00:20:28 One is a sentence to sentence.

00:20:30 This is very similar to the Seek2Seek, actually.

00:20:33 One is from image to the sentence.

00:20:38 So, both have the same decoder, but their encoders are different, okay?

00:20:44 In the image captioning, the encoder is CNN because the input is image.

00:20:49 And in the case of language translations, the encoder is very similar, right?

00:20:57 To the decoder, which is recurrent, okay?

00:21:00 But is it true that the natural choice of image captioning, the natural choice of the encoder for image captioning should always be CNN?

00:21:19 Well, that's not necessarily the case either.

00:21:22 The fact that we'll show you the answer.
