transcribe

MIT 6.S191: Recurrent Neural Networks, Transformers, and Attention

Alexander Amini · 57m · transcribed Jun 2026
More from Alexander Amini Business
𝕏 Share ▶ YouTube 📥 PDF 🤖 .md

Transcript

0:00 All >> [music] >> right, I think we can circle back and get started here. So, I'm Ava and welcome to lecture 2 of 6S191. And we're going to focus today in this talk on sequence modeling which is a very very powerful and general paradigm that has become super relevant today especially with the onset of these very powerful language models. And so just about an hour ago Alexander introduced us to this idea of neural networks and built the intuition about neural networks from the bottom up. And we focused specifically on these class of models known as feed forward models. And so when we turn our attention to applying neural networks to sequential processing and sequential modeling of data, we'll see how this modeling paradigm requires a different way of thinking, right? Because we now have data that's resolved in time. And we'll go through similarly to build up the intuition and the modeling framework step by step from the ground up.

1:11 So we'll with that we're going to start again with a very intuitive example which as you'll see throughout the course is kind of one of our things that we really hold very near and dear in terms of building up intuition about these concepts. So to do that let's start with this very simple example where I've given you an image of a ball in a 2D space here and I'm going to ask you okay can you predict where it will go next?

1:40 without any prior information about the ball's history, right? Anything that you say is going to be a guess on its next position, a guess, a random proposal about where the ball is going to travel to. However, if in addition to the current location of the ball, I were to give you a sequence of information back in time and ask you to use this in context to now predict where the ball is going to travel to next. the problem now becomes much easier and I think we can all agree on what's the most likely next path or trajectory of the ball in this problem. So this is really the core idea behind many sequence modeling problems where we have this data that's resolved in time or can be modeled as a sequence of information and we want to make a prediction about the state of the system. And so beyond that simple example, sequential data as Alexander mentioned is truly all around us. Right?

2:38 As I'm speaking and I'm projecting these audio waveforms, that is a sequence that can be split up into a series of chunks of sound waves. Similarly, with text, we can think about breaking that text up into a sequence of individual characters or individual words. And beyond these examples, sequential data is really really all around us. From medical signals to financial time series like modeling the stock market to biology and chemistry and the life sciences and thinking about biological sequences and modalities to patterns of movement to videos to weather. All around us there are these problems that can be thought of from a sequential modeling perspective.

3:25 And so that gives us a concrete notion of how we can bring this idea of sequence modeling to the real world. But what are some tasks that we now could think about training neural networks to facilitate in this context? In lecture one, we talked about this simple example of classification, binary classification. Yes or no. Will I pass this class? given a student as input produce a 01 prediction about pass versus fail. We can also do similar classification type problems or tasks with sequences. Right? We can think about taking a body of text, let's say a tweet, and assigning a emotion or a sentiment as a class label to that text.

4:14 On the other hand, we could go rather from sequence to one output. We could have one input and produce a sequence of output. So say in the image captioning world or the vision language world where given an image we want to train a neural network to produce text describing the content of that image that also falls into this paradigm of sequence modeling. And also we can think about many inputs going to many outputs. So if you want to translate text or audio from one language to another, we have to do this transformation of learning a function to map English text to let's say text in Mandarin. So these are some three examples of core types of modeling tasks we can bring to the sequence sequence modeling world. And so today we're going to think about these types of problems and we're going to build up intuition about how we can actually define different types of neural network architectures to tackle the unique uh features of these types of sequence modeling problems.

5:27 And so the first core part of this lecture is going to be around this idea of building neurons that have a notion of recurrence that allows them to process time resolved or sequential data one step at a time. And I think this this topic is is something that when I was first learning machine learning and deep learning was something I myself had a struggle to wrap my head around. And so what we've tried to do with this next block of content is really build a fundamental intuition about how this concept works. Building from the ground up and connecting back to what Alexander introduced with feed forward models to actually build up that intuition effectively.

6:13 So to do that, let's first go back to the perceptron, right? our building block of neural networks and go step by step to the sequence modeling world. When we looked at the perceptron, we had this kind of diagram, right? This function where we have a set of inputs, let's call them x1 through xm. And we have an individual uh perceptron unit that takes these inputs, multiplies each of them by a weight, applies a non uh nonlinearity, a nonlinear activation function to produce an output yhat. And so we saw that we can have multiple inputs going in to this perceptron. And you can think of these as inputs at a particular time. We also saw how we can stack these individual perceptrons together to build out a layer of a neural network. And so what you're seeing here is that extension right where in green we have this layer of individual perceptrons where we can map multi-dimensional input to a multi-dimensional output.

7:24 But so far, even though we have a multi-dimensional input, does this have a notion of time steps or of sequence? No, not yet. And so to get there, let's take this diagram and collapse it down and simplify it a bit. So all I'm showing you here is that inner layer of the stack of perceptrons collapsed into this layer wise block, right? Shown here in green. And our inputs are going to be x of t x x at some time step t. And they're going to be uh fed into this layer and producing an output y hat.

8:04 But we're still just considering the input at a particular time step. And we're denoting that by the marker little t. This is nothing different than what we saw in the first lecture. Right? We're just beginning to get this this notion that this is processing information from just one time step. Now let's flip this on its side. So we've just rotated this 90° input vector going into the layer getting our output vector.

8:35 What if we had this sequential data over time and let's say we took our same feed forward model and tried to apply it time step by time step over and over again. If we did that we would see something like this where at time step zero we take our input pass it through our neural network layer produce an output yhat. We did this at time step one. Do the same thing. Pass in the input, produce an output. At time step two, so on. And even if we did this time step from to time step, we'd be applying our neural network function to the inputs and predicting outputs, but still they're not connected at all. Right?

9:20 These time steps are isolated. We're just treating x of zero at time step zero, x of one at time step one, so on and so forth. But really the core thing with sequential data is that there is some relation between the inputs at time step t to the inputs at time step t minus one, t minus 2 and so on and so forth. Just like the example with the ball, if we treat the time steps in isolation, we have no information about the past trajectory of the ball. But if we connect the time steps together, then we can use that past history to make a better prediction about the current state or the future state.

10:03 And so this to to concretize this a little more, what that is saying is that the predicted output at a later time step, say t= 2, should depend somehow on the inputs at the prior time steps, say x of 1, x of0. And so we want a way to preserve this information and carry it forward to make predictions at later time steps. And the idea behind doing this is to link and relate the network's computations at the particular time step to the past history of the prior time steps. So let's try by doing exactly that.

10:49 What this diagram is now showing is by connecting these uh outputs and the states at these p past time steps, we're linking the computation of the network across time. Specifically, we can introduce this notion of an internal state of the of the network. And we're going to denote this using the variable h where this is going to be actually maintained by the neural network itself and pass on time step to time step. So we get this idea of a sort of memory of this internal state of the neural network that's going to depend not only on the past computations but also the input at that time step.

11:38 What this means very concretely is when the network makes a prediction at time step t that's going to be a function of not only the input at that time step but also the past memory or the state that's captured in this variable of state h of t minus one. So that past memory and you can see that concretely in this example on the middle where prediction at time step one depends on input at time step one as well as the past state.

12:16 And so this is this core idea of what we think of as recurrence meaning simply how can we link in just one network right the network is not u you know we're not creating new networks per time step it's still one network but we're connecting the information from the past computations to the predictions at a particular time step and so because this output this prediction is a function of both the current input and the past memory. We describe these neural networks via this recurrence relation.

12:53 And so you can show this on the left here where we have this notion of a cycle, this looping which shows that h of t is being recurrently provided to the network at these different time steps to make the predictions. And on the right, we just see that kind of rolled out over these time steps. It's in a way a clearer to me it's a clearer visualization of what is actually going on. And so this core idea of maintaining an internal state h oft updating it per individual time steps and using that to inform the predicted output is the core intuitive idea behind a very large class of neural networks called recurrent neural networks or RNNs.

13:42 And this is really the key to understanding these these frameworks of RNN's. So we're going to continue to build up from this foundation to gain more and more understanding about the behavior of these neural networks. So let's formalize this a bit more. Right? As we saw this core idea is to maintain the internal state update it at each time step that this as the sequence is processed dynamically. And so the way we do this is via this recurrence relation which defines how that update actually occurs per unit time.

14:22 Making this more concrete, this notion of this cell state h of t is going to be a function parameterized by a set of weights. Just like with fet forward networks, just like with any network we're going to see throughout this course, right? We always define these updates according to these learnable sets of weights and that state update depends on the current input and the prior state from the prior time step.

14:52 Right? Cell state is a function defined by set of weights W that depends on the input and the old state. And with an RNN type model, we use that same function and set of parameters at every time step. And when we actually train the model, as we'll see, those uh weights get updated per training step. So another way to get more intuition from this is also to look at an example in pseudo code. So let's go through that through the algorithm for the state update of these state-based models and get a better sense of how these networks work. So we start by initializing the state to something let's say 0000 0 and our prediction here is going to look at text text data we break it up into a sequence of words and our task is to predict the next word in the sentence.

15:55 So we take our input sentence and we take our initialized RNN with its initialized state and we then we iterate time step by time step word by word in that sentence and call a function of the RNN to generate a prediction and update the hidden state per word right per time step and at the end once we've stepped through all the words so far in the sentence we can get a prediction for the next word by taking the output of the RNN at that last time step. Right?

16:30 So this is really a core idea behind this idea of next word prediction. One way to approach it is by going step by step word by word through a sentence and predicting the and learning this relation uh kind of recursively over time. So as we kind of saw right with an RNN, we need to update both the state and predict an output. So to take this to the last level of concrete concreteness and and bring a little bit math into this, our relationship here is we're given this input vector. We first update the hidden state and we're going to apply a function to to do this. to break this down. It maybe looks a little intimidating at first with multiple variables here, multiple weights, but it's really very similar to the neural net operations that we saw in lecture one that Alexander introduced. Just like before, we have multiplications by weight matrices, right? Sets of weights that are applied to both the past hidden state and the input. And we take those, sum them together and apply a nonlinear activation function. Right? That's the tanh hyperbolic tangent function here.

17:51 And that gives our hidden state update. And to do the output prediction, we take a another weight matrix and transform the hidden state and predict the output. And importantly, right, these three sets of weight matrices are learned. we start them, we initialize them and over the course of the training of the RNN, we update the parameters of these weight matrices through gradient descent and the similar algorithms of training the the neural network that that we saw in lecture one.

18:26 So putting this all together, right, how does this come how does this come together? We have our RNN with this recurrent loop-like relation. And we saw that we can unroll this step by step across these time steps and visualize the RNN that way. When we do that, we break it down from time step zero to time step one, so on and so forth all the way up to time step t, whatever the length of the input is. And across all these um steps, we're making these predictions at these individual time steps. We can wait make the weight matrices explicit showing the weight matrix that maps input to the hidden state the hidden state weight matrix as well as the output. And we reuse these same weight matrices at every time step.

19:24 When we make a forward pass, a call through the model to actually generate the prediction because we're generating predictions at each time step, that means we can compute a loss at each individual time step and we can sum them together to get an overall loss that gives us the whole predictive loss over the entire sequence. So that gives us a broken down way of how this RNN forward pass a call to the model actually occurs in practice.

19:58 We can we can go through and look at how we could implement this from scratch in a framework like TensorFlow where we begin by initializing these weight matrices, initializing a class for the RNN and defining this forward pass computation and what we call the call function which is effectively how you call the neural network, right? How do you make a forward pass going from input to prediction and using that prediction to get um during training to compute a loss and update the loss.

20:34 And so again to reiterate this computation, right, we update the hidden state, we compute the output and return both both values back. In practice though, right, very rarely will you actually or maybe almost never build up a RNN unit from scratch. In fact, in both uh TensorFlow and PyTorch and common um ML frameworks, there are existing implementations built in where you can just use these RNN layers in practice in defining your neural network. And you'll get hands-on experience doing this in software lab one. uh starting today.

21:16 Okay. So to recap and put these elements together, we saw that with RNN's we can and sequence modeling in general, we have this idea of different types of tasks or problem formulations we can tackle. We can do classification tasks. We can do captioning tasks or text generation tasks. We can do sequencetosequence translation tasks. And that is really with a lot of these generative applications like in language modeling, machine translation, natural language processing, many of these can be uh formulated according to these many to many problems and you'll get hands-on experience with this.

21:59 So to make this you know a more concrete in terms of what this actually means in terms of a real use case or a problem in the real world. I want to focus on a very very pertinent sequence modeling problem which is the basis for basically all language models, large language models, small language models that we see today, which is this notion of next word prediction. And we're going to use next word prediction to think about and work through core design criteria that we want to build out in any sort of neural network that we build for a sequence modeling problem. And this is a really important task not only because of the relevance of next word prediction but in general in thinking about your own deep learning applications or work or research that may be relevant to you.

22:57 relating those problems back to design criteria in terms of the architectures you choose and the the approach you actually take. So let's say we have this sentence this morning I took my cat for a walk. The task of next word prediction is given some set of words let's say this morning I took my cat for a predict the next word in the sentence and so to do this the very first thing we have to consider whether we're using an RNN or any other sort of neural network is how are we actually going to represent this data language data to the model the way that we can formalize this is let's say we have a model and we input the the word deep and we want to use the neural network to predict the next word learning. What could be the issue here?

23:56 If we're just trying to pass in words to the model, will this actually work? If not, how can we make this work in terms of the data itself? >> Yes, >> you have to tokenize the words. >> You have to tokenize the words. Can you say more about what that means? >> If you do eual letters, the engine deep as a word is closer to a concept and so it's better to >> right. So it gets closer to this idea of can we embed something about the concept of the word to represent the represent to the model. It actually is a step earlier than that which is we can't straight pass in you know characters to to the model. They have to be represented numerically somehow right because ultimately all these neural networks are they're operating on you know numerical data. And so tokenization is one way that we can think about doing this. But regardless of what strategy we use, we have to process the the language data to a numerical input where we have a vector or an array or an index of numbers to the model that represents let's say the the sequence of words and get a prediction of something that represents the next word but is in numerical form. And then we can map this back to the language space to actually get what the word prediction was. And so to formalize this, how do we actually do this? This gets to this idea that you raised in your answer of transforming the words into some numerical representation and tokenization gives us one strategy to do that. So this is a concept of in general of of an embedding or an encoding and ideally we want this embedding to represent something about the semantic content of the words right because language has this very rich structure. So something like learning and education maybe are mapped closer together than learning and tennis.

26:11 Right? So the approach for doing this is to consider the complete vocabulary or the corpus of words that are present in our language and first start by mapping these words to individual unique indices. Then where do we go from there? A naive approach would be to just map an index to a fixed length vector where you have a single one hot bit flip, right? where that represents the identity of the word based on the index that's a one versus zeros elsewhere. But this is not very informative, right? Because of the fact that we're not capturing anything about the semantics and this rich structure that's present in language. And so a more intelligent way to go about this is to leverage some sort of embedding scheme where there is capture of these semantic relationships. And so this is in general learning an embedding or learning a tokenization strategy to capture this. And in language models today, there are very po popular schemes that basically effectively map chunks of of words and chunks of language that occur frequently together to certain parts of this embedding space so that we can capture those relationships in terms of co-occurrence of particular characters or words or chunks in the in the vocabulary.

27:46 So this is really the core idea. But the fundamental point that I want to convey is that in all cases in all examples that we see throughout this class, neural networks operate on numerical inputs. So regardless of your data modality, you have to think about a way to map that data into numerical input to be processed and uh represented in a way that the neural network can handle. Okay, so that's design criteria number one, right? We need a way to map to numerical inputs and ideally we want this mapping to capture something about the structure of language.

28:27 The second sort of design criteria that's critical for sequence modeling and this next word prediction task is being able to handle variable lengths of sequences. Short sentence, long medium sentence, long sentence, doesn't matter. we should be able to operate over all of these types of inputs. Feed forward networks break down here, right? Because of the fact that there's a fixed number of inputs. But something like a state-based model like an RNN where we're processing the data time step by time step is inherently designed to process these variable length inputs.

29:09 A final crit uh another criteria that we want to think about is capturing these dependencies and relationships that can occur in sequence data like language. Often times there's really core information up front in a sentence that informs the next word prediction super downstream in the sentence and we want to be able to capture this effectively. And finally, sequence data is sequential for a reason. That means that differences in the order can completely alter the, you know, what is actually represented in that sample. The food was good, not bad at all, means great, good food. But if we flip the order of the words in the sentence, we've completely flipped the meaning. The exact words are the same.

30:00 Order matters. And so RNN's are one way to start to get at tackling these design criteria because they can handle variable length sequences through this notion of the internal state. We have some way of tracking and preserving information about these dependencies. We're processing the data step by step. So we have a notion of order and we have this weight sharing that allows this buildup of memory over time in the RNN to be able to capture those dependencies.

30:35 Okay. So the final piece of RNN u content that we'll talk about is how we actually train RNN's in practice. And that's based on the core neural network training algorithm that Alexander introduced which is back propagation. So let's do a refresher of how we actually train these feed forward models. We have an input and we first make a forward pass through the network from input to output to generate a prediction. And then to actually compute the loss, update the parameters of the model, the weights in the model, we back propagate gradients back through the network where the algorithm was to take the derivative of the loss was with respect to the parameters or the weights in the in the network and then shifting the parameters in order to minimize this loss.

31:37 Now let's say we are going to extend this to the context of RNN's. As we saw with RNN's we have this stepbystep sequential processing. So we make we can predict outputs at each time step meaning we can get a loss at each time step and we can get an aggregate loss by summing the losses at these individual time steps. Now in order to back propagate and make updates to the network we need to do this over time as well. So instead of back propagating error through a single feed forward network at a single time step in RNN's we back propagate across these individual time steps and then across time steps from where we are currently back to the beginning of the sequence. And so this is the formulation of this algorithm for training RNN's called back propagation through time because error is flowing back through time from where we are currently in the sequence back to the beginning time step one or time step zero. And so taking a a closer look at how these gradients flow, we actually have to make these very repeated matrix multiplications uh with the weight matrix W to make because of these cell state updates requiring this passing of the weight matrix WH.

33:08 And in practice, this can get a little tricky because computing the gradient back with respect to the state at the very first time step zero involves many many repeated factors of wh of h and repeated gradient computations. In practice, if the values of this weight matrix are large, things can blow up very quickly, a problem that we call exploding gradients. And so often in practice you'll actually clip the values of the gradients to some maximum so that you can scale back big gradients. You can also have the inverse problem where if the weight matrix ends up being very small you have repeated multiplications of something very small and things shrink and then you get very very little signal to actually learn on and this is known as the vanishing gradient problem.

34:03 And there are as well practical tricks in terms of solutions and and um implementations that people use to mitigate this problem. Yes. Question. >> Is H0 considered a parameter? >> So H0 is neither an explicit parameter nor an input, right? It's this internal state uh variable if you will and it is dependent on the the weight matrix parameter and the input. So if you think about the input as being a variable h0 is a variable that depends on uh computation involving the weight matrices w.

34:48 Okay. So in practice, right, this vanishing gradient problem has real implications in particular with respect to our ability to track long-term dependencies in the data. And I won't spend too much time on this, but just to give you a sense, when we multiply many of these small gradient values or weight um matrix values together, it biases the model to try to capture the shorter term things because you know further back you're going to have more propagation of of error and more of this vanishing problem. And so while this allows for effective capture of dependencies that are relatively shortterm, it poses a problem for important longer term dependencies where the input length can grow very long in terms of number of time steps and becomes problematic for the RNN to actually keep track of these longer term dependencies. So in practice there has been a lot of architecture development around these RNN style models and in particular there's this notion of making the internal computation of the RNN more complex to better control the flow of information and the the state update equation itself. And one really pioneering solution to this was many years ago um a a group led by Jurgen Schmidhub Schmidh Huber introduced this mechanism called long short-term memory that was really a foundational advance in terms of uh better controlling for these state updates in architectures like RNN's.

36:39 So brief note on applications of these networks and in particular the one that you're going to experiment with in software lab one which is this multi-output generation problem focusing on this task of music generation and every year I like to highlight this example from a few years ago now but the capabilities of deep learning and AI for music generation have now become actually quite remarkable but still this is one of my favorite examples where the classical composer France Schubert had a very uh symphony that he called his unfinished symphony. And a few years back there was an effort to actually train a recurrentbased model on his music to predict and generate new music to finish his unfinished symphony. So this is a sample of that AI generated third movement.

37:40 a fourth [music] [music] which is pretty compelling I think from a musical perspective and actually remarkably I think the the quality has even continued to improve. You'll actually experiment with these same concepts in software lab one which I think is really cool and exciting. And so I'll leave the applications at that for the the purposes of time and hopefully give you enough of a teaser to motivate entries into software lab one competition.

38:21 Okay. So in the final 15 minutes or so, we're going to move beyond recurrent models and think really about taking a step back to again the design criteria and capabilities of that we desire with sequence modeling and thinking about what are the limitations of RNN's from a technical perspective and how this has motivated new advances in architectures for sequence modeling. So the first fundamental limitation of RNN's is that they have this encoding bottleneck that's imposed by this notion of the internal state H of T. This variable we are trying to pack as much information about the dependencies and the structure of the data into that internal state H of T and use this to carry forward time step by time step.

39:14 relatedly right when we process the data time step by time step this can be pretty slow we have to take the individual inputs the individual slices as individual slices and so it's not really paralyzable and as a result of these two elements especially that encoding bottleneck the RNN can really quickly run into memory issues in terms of how long of a dependency is it able to capture And so if we go back again to our fundamental goal of sequence modeling, we want to take a input that's represented as a sequence and learn some set of features that then map to a sequence of outputs. And RNN's use this notion of a recurrence relation and this stepbystep modeling to try to track these dependencies. But they have these three key bottlenecks that I mentioned.

40:15 Ideally, we don't have that encoding bottleneck and we're able to look at this sequence globally as a continuous stream of information, but still able to capture local relationships and dependencies in the data. Ideally, we don't have to h handle the the sequence time step by time step. we're able to parallelize very efficiently so that we can handle long bodies of input and we want to have this notion of long memory.

40:46 And so a thought experiment and a question is what if we eliminated the need for recurrence entirely? How could we possibly approach this? One naive approach would be to kind of squash everything together. take one vector input and take the the data from all the time points, concatenate them as one input vector, a single a single input and feed this into the model and calculate some feature vector and use that to generate an output which we hope makes sense and is sensible.

41:27 But yes, while we've eliminated this recurrence, we've eliminated this step-by-step processing, this is not going to be scalable, right? Because if we were to build out a dense network to do this, this quickly gets very very large as the sequence length grows. Importantly, we've lost the order information because those dependencies are gone. And because of that, there's no temporal dependency. So how can we really build out memory? And so that's because when we have these fully connected or concatenated inputs, there's no notion of how do we actually pick out and attend to the parts in that input that are going to be important.

42:12 And so several years ago now there was this breakthrough idea in this field of this notion of can we devise mathematically a way for the network to learn how to pick up and identify those dependencies locally and also globally that are going to be important in the sequence overall. And this is the notion of attention. And this is a very powerful mechanism for learning in sequence modeling applications and is really the backbone of the transformer architecture. So if you know GPT or other models like BERT, the T in all of these acronyms stands for transformer and the foundational mechanism behind these transformers is the attention operation. And it's to me a very very elegant and intuitive idea. And we're going to break it down both from the intuition point of view and also the math to show you how that works.

43:15 So the core intuition behind attention is can the neural network learn to identify what parts of an input within itself are most important to representing that input? And I think it's often easier to start with a visual example rather than a s, you know, a text example. But when we think about attention, right, we can immediately look at this picture and pick out the important parts. One naive way to do this would be to scan the data pixel by pixel, go across it. But all of us intuitively, we maybe don't necessarily do this. We don't, you know, eyeball left to right. we can immediately pick out the important parts. And so the core idea behind this is if we want to identify which parts to ident attend to, that's step one. And then we use that identification to extract the features or the actual content of the input that have high attention scores or high attention values. And it's really this first part of the problem that's the core which is very similar to this notion of search.

44:28 So if we were to do an example walkthrough, let's say many of you probably had this question of how can I learn more about AI and deep learning and neural networks. One way you could approach this in in addition to coming to this class is to go to the internet, go to something like YouTube and search for videos there that are relevant to this topic of neural networks and deep learning. And so YouTube, right, is a giant database of many, many videos. How do you actually find and attend to the relevant videos in this database?

45:05 You're going to go in and put a query in deep learning and we want to be able to extract some key information about each of these videos that are in this giant database. So all of these videos have a title. Think about it as the key. And ideally to find to complete the search problem, you want to find an overlap semantically between your search query and key the indices of these videos in these databases.

45:41 And so you're going to compute what we think of practically as some sort of waiting or an attention mask. How similar is each of these keys to your query? And we can do this step by step. In this example, a beautiful sea turtle may be very elegant and natural, but not similar to the query of deep learning. A video from six years ago about uh introduction to deep learning. Yes, relevant.

46:14 A video about the best fadeaway in all of basketball from the late and great Kobe Bryant. also very important but not relevant to the query. And so now we've identified right the relevant feature or the part of the data and we want to extract the value the video itself what that we want to pay attention to and this we call the value and because the search was implemented right with a good attention mechanism we've pulled out best deep learning course which of course is ours and I'm sure all of you can relate to this because right you're here sitting in this class and so really this is this is an intuition that explains this this what this attention operation is actually doing. So to formalize this in practice how self attention works in neural networks like transformers the goal is exactly the same. We want to identify and attend to parts of an input which can be a sequence of data that are going to be important. So breaking this down let's say we have this input sentence. He tossed the tennis ball to serve. First, we want to eliminate recurrence but preserve information but about order. If we're going to feed the data in all at once, we still want a way to encode or represent these positional relationships.

47:40 And so in practice with attention, there's something that's called a positional uh uh excuse me, a positional embedding that's used to represent the input that's given into an attention mechanism. And we're not going to go into great details about how this position aware encoding is actually implemented. But the core thing that I'd like for you to take away is that mathematically we can use this to preserve some information about the positional relationships in this sequence input.

48:17 The next is really this core step of these three components query, key and value. and they are the fundamental components of the attention mechanism. And so we're going to take that positional embedding which is representing our sequence input all at once. And in this case, in this example, we're going to look at self attention. Meaning in a individual sequence input, can we identify those core relationships in that sequence input itself?

48:51 To extract that, we're going to do three transformations of this embedding to get our query, our key, and our value. The first is a query, and it's going to be done by a learned layer that transforms the positional embedding into a query matrix. And similarly we will have another learned layer for the key matrix and a third learned layer for the value matrix.

49:21 Importantly note that the positional embedding is the same for all these three. But those three layers and therefore the three transformed outputs of query key and value are going to be different capturing different information. Now we need to actually compute the attention score. How similar is that query to the key? How can we identify those most important parts? The way we can do this is to compute a mathematical similarity between these two matrices.

49:57 And mathematically we can do that through an operation simply that is related to based on the dotproduct. Right? So if we have two vectors in space, let's say we represent the query vector in blue and the key vector in orange using the dotproduct, we can compute how aligned they are in space. And so we take the dotproduct between the query and the key and scale it to get this notion of a similarity metric. And this is also known as the cosine similarity. Right?

50:34 And so fundamentally right we can apply this same operation to matrices this dotproduct and this scaling to get us this similarity matri metric. If we visualize what this the result of this operation could mean remember we're trying to compute self attention meaning within the sequence he tossed the tennis ball to serve. What are the components of that sequence that relate more strongly to each other versus others? If we were to learn attention over this uh compute attention score over this input, we could look like we could get something similar to this where in this matrix we for each position, right? We compute a score along the diagonal is going to have the highest relation because the words are the same. But the words that are more um semantically related and relationally related should also have high scores. For example, between ball and tossed.

51:39 And so when we compute this dotproduct of the query and the key, apply the scaling, we finally apply a function the soft max to squash these values between zero and one. And this gives us our attention weight. And when you look at the the uh entries of an attention matrix, this gives you a notion of the relative importance of parts of that sequence of input relative to each other. And this attention waiting is finally what we use to actually extract the features out by multiplying this with the value to get the final output.

52:21 So putting this all together right the core idea of using this attention waiting to identify the parts of importance multiplying it with the value to extract those features puts us in a great place to now bring this forward this attention mechanism as a part of a neural network. So to recap, our goal again, identify and attend to the most important features in a sequence input. Architecturally, what this looks like is we take the sequence positional encoding. We have three copies. We have three learned neural network layers that allow for transformation of these encodings to the query key and value.

53:06 We can use this to compute the similarity score by computing these attention weights between the query and the key. Scaling it applying the softmax to get values between zero and one and finally extracting the features with high attention via the multiplication with the value uh matrix. And so together this operations that I'm showing in the right are the basis of what we call an attention head that can plug into a larger network architecture.

53:40 And so modern transformers use this attention operation to compute this very similar type of relation to learn these patterns of importance uh in sequential data. And this is really the core building block of the transformer architecture. What's really powerful is that it's very very parali parallelizable. And we can also put together multiple attention heads stacking them together within one network to identify and learn different patterns of importance. And so you can think of this intuitively as maybe we have multiple attention heads in a network where visually we can pick out Iron Man, but we can also pick out this building that, you know, he's hopefully not going to crash into and this alien spaceship looming in the background. Right? So this is just very I want to reemphasize that this is an intuitive example to ground you. But conceptually this idea of stacking these attention heads is very very practical in terms of building out these transformer architectures.

54:50 So today it's it's truly um remarkable how far this architecture has come in terms of the capabilities that we have seen as a result of not only that innovation but many innovations that have followed and it's not just in natural language and transformer LLM like GPT but also in other domains as well in biology in thinking about sequences like proteins or DNA and how We can learn relationships using transformer-based architectures and even in the visual domain where vision transformers have become perhaps the dominant architecture for looking at uh computer vision and visual data as we'll learn about in lecture three tomorrow.

55:41 So with that that concludes this lecture and to summarize the key points right we talked about sequence modeling as this broad class of tasks that are very relevant across domains we learned about RNN's and how they're well suited and how we can build out this recurrence relation train them using back propagation applications to music generation text generation translation beyond And finally, we talked about the limitations of RNN's in terms of the encoding bottleneck and how this has motivated advances in attention to model sequences without recurrence.

56:24 So with that, we're going to close the lecture portion and a couple of logistics notes and announcements. So the final hour of the class each day 3 to 4 pm is dedicated to an office hours style session where we will be available to ask answer your questions. You'll have time to work on the software labs. We also have our amazing TAs here to help you with practical questions getting started with the software labs and the labs are all available on this link via GitHub and the instructions for starting to execute them are there. And finally, John has been as as you meant earlier, has been very very gracious in hosting our kickoff social tonight at 5:00 pm. If you haven't registered already, please do so so we can help keep track of who is coming and it's just down the road at 1 Kendall Square and there'll be free food and drinks provided. So, hope to see you there and thank you so much for your attention.

57:31 [applause]

Summary

Ava's lecture on sequence modeling delves into the significance of recurrent neural networks (RNNs) and the attention mechanism in processing sequential data. She explains how RNNs maintain an internal state to capture dependencies over time, and discusses the limitations of RNNs that led to the development of transformer architectures, which utilize attention to enhance performance in tasks like language modeling.

- Sequence modeling is crucial for tasks involving data resolved in time, such as language processing and time series analysis.
- RNNs process sequential data by maintaining an internal state that updates with each time step, allowing them to capture temporal dependencies.
- The training of RNNs involves backpropagation through time, which can lead to issues like exploding and vanishing gradients.
- Attention mechanisms allow models to learn which parts of the input sequence are most relevant, improving the handling of long-range dependencies.
- Transformers, which leverage attention, eliminate the need for recurrence and can process sequences in parallel, enhancing efficiency and scalability.
- The lecture emphasizes the importance of tokenization and embedding in representing sequential data numerically for neural networks.
- Applications of RNNs and transformers span various domains, including natural language processing, music generation, and even computer vision.
© transcribe · For agents Built with care and craft by Gokul Rajaram