Andrej Karpathy: CS231n Lecture 10: Recurrent Neural Networks

On Recurrent Neural Networks: LSTMs and Captioning

CS231n Winter 2016 Lecture 10 · February 2017
Video thumbnail: Andrej Karpathy on Recurrent Neural Networks: LSTMs and Captioning
1 hr 10 mintalk131K views across 1 upload14,087-word transcriptWatch on YouTube ↗

What Karpathy talks about

Andrej Karpathy gave this CS231n Lecture 10 talk in February 2017 on recurrent neural networks, image captioning, and LSTMs, explaining sequence modeling, text generation, and vanishing gradients.

Also known as: CS231n Winter 2016: Lecture 10: Recurrent Neural Networks, Image Captioning, LSTM; Lecture 10: Recurrent Neural Networks, Image Captioning, LSTM; CS231n Lecture 10.

Karpathy introduces recurrent neural networks as a flexible way to model sequences, from character-level language models to translation, video, and image captioning. He explains the core recurrence, how the same weights are reused at every time step, and how backpropagation works through time.

He then walks through a NumPy character-level RNN, showing how training, sampling, and truncated backpropagation work in practice. He uses memorable examples like Shakespeare text generation, math LaTeX, and source code to illustrate what RNNs learn and where they struggle.

The lecture closes with image captioning and then LSTMs and GRUs as the practical answer to vanishing gradients. Karpathy emphasizes why additive memory paths help gradients flow and why these architectures became the standard for sequence modeling.

Karpathy speaks throughout, with occasional student questions during the lecture.

In his words

“They are a lot of fun to play with.”
at 0:00
“We can actually operate over sequences: sequences at the input/output or both at the same time.”
at 2:12
“This is what allows us to process, you know, variably sized inputs.”
at 12:15
“RNNs are very nice, ah, but the raw RNN does not actually work very well, so you use LSTMs or GRUs instead.”
at 1:07:54

Key points

  • RNNs let neural nets process variable-length sequences at input, output, or both.
  • The same recurrence function and weights are reused at every time step.
  • Character-level language models are a clear way to understand sequence prediction and training.
  • Backpropagation through time accumulates gradients across repeated uses of the same parameters.
  • Truncated backpropagation is used in practice because long sequences are too expensive to unroll fully.
  • RNNs can generate surprisingly structured text, code, and even math from raw character data.
  • LSTMs and GRUs solve the vanishing-gradient problem much better than vanilla RNNs.

Full transcript: Andrej Karpathy, CS231n Lecture 10: Recurrent Neural Networks (2017)

Test, test. Okay, it works. Okay, good. We should get started soon. So today, we'll be talking about recurrent neural networks, which is one of my favorite topics, one of my favorite models to play with and put into neural networks just everywhere. They are a lot of fun to play with. In terms of administrative items, recall that your midterm is on Wednesday. This one's day—you can tell that I'm really excited. I don't know if you guys are excited. You don't look very excited to me. But the 7/3 will be out due this Wednesday. It's—sorry—will be out in Wednesday. It's due two weeks from now, on Monday. But I think since we're shifting it, I think to Wednesday, we plan to have released it today, but we're going to be shifting it to roughly Wednesday, so

we'll probably defer the deadline for it by a few days. And assignment two, if I'm not mistaken, was due on Friday, so if you're using three late days, then you'd be handing it in today. Hopefully not too many of you are doing that. Are people done with the 7/2? Or how many people are done? Okay, most of you. Okay, good, great. Okay, so we're doing well. So currently in the class, we're talking about convolutional neural networks. Last class specifically, we looked at visualizing and understanding convolutional neural networks. So we looked at a whole bunch of pretty pictures and videos, and we had a lot of fun trying to interpret exactly what these convolutional networks are doing, what they're learning, how they're working, and so on. And so we debugged this through several ways that you maybe can recall from last lecture. Actually, over the weekend, I stumbled by some other

visualizations that are new. I found these on Twitter, and they look really cool. I'm not sure how people made these because there's not too much description to it, but it looks really cool. This is turtles and tarantula, and then this is a chain and some kind of a dog. And so the way you do this, I think, is something like Deep Dream; that's, again, optimization into images. But they're using a different regularizer on the image, so in this case, I think they're using a bilateral filter, which is this kind of a fancy filter. So if you put that regularization on the image, then my impression is that these are the kinds of visualizations that you achieve instead. So that looks pretty cool, but yeah, I'm not sure exactly what's going on. I guess we'll find out soon. Okay, so today we're going to be talking about recurrent neural networks. What's nice about recurrent neural networks is that

they offer a lot of flexibility in how you wire up your neural network architectures. So normally when you're working with neural—let's here in the case on the very left here—where you're given a fixed-sized input vector here in red, then you process it with some hidden layers in green, and then you produce a fixed-sized output vector in blue. So, say, an image comes in which is a fixed-sized image, and we're producing a fixed-sized vector which is the classes, of course. When the recurrent neural networks, we can actually operate over sequences: sequences at the input/output or both at the same time. So, for example, in the case of image captioning—and we'll see some of it today—you’re given a fixed-sized image and then, through a recurrent neural network, we're going to produce a sequence of words that describe the content of that image. So that's going to be a sentence that is the caption for that image. In a case of sentiment classification in the NLP, for example,

we're consuming a number of words in sequence and then we're trying to classify whether the sentiment of that sentence is positive or negative. In the case of machine translation, we can have a recurrent neural network that takes a number of words in, say, English, and then it's asked to produce a number of words in French, for example, as a translation. So we'd feed this into a recurrent neural network in what we call sequence—the sequence kind of set up—and so this recurrent network would just perform translation on arbitrary sentences in English into French. And in the last case, for example, we have video classification, where you might want to imagine classifying every single frame of video with some number of classes, but crucially, you don't want the prediction to be only a function of the current time step, the current frame of the video, but also all the frames that have come

before it in the video. And so recurrent neural networks allow you to wire up an architecture where the prediction at every single time step is a function of all the frames that have come in up to that point. Now, even if you don't have sequences at input or output, you can still use recurrent neural networks, even in the case on a very left, because you can process your fixed-size inputs or outputs sequentially. So, for example, one of my favorite examples of this is from a paper from DeepMind from a while ago, where they were trying to transcribe house numbers. And instead of just having this big image feed into a convnet and try to classify exactly what house numbers are in there, they came up with a recurrent neural network policy where there's a small convnet and it's steered around the image spatially with a recurrent neural network, and so the recurrent network learned to basically read out house numbers from left to

right sequentially. And so we have fixed-sized input we're processing sequentially. Conversely, we can think about this as also a well-known paper called DRAW. This is a generative model, so what you're seeing here are samples from the model where it's coming up with these digit samples. But crucially, we're not just predicting these digits at a single time. We have a recurrent neural network, and we think of the output as a canvas, and the kernel goes in and paints it over time. And so you're giving yourself more chance to actually do some computation before you actually produce your output, so it's a more powerful kind of a form of processing data.

What are question over there? So every one of these arrows is kind of like a dependency in terms of, I guess, we'll see specifics of exactly what this means. For now, arrows just indicate functional dependence. So things, yeah, so things are a function of things before, and we'll go into exactly what that looks like in a bit. Okay, so of this one. So these are generated house numbers. So the network looked at a lot of house numbers and it came up with a way of

painting them. And so these are not in a training data, these are made-up house numbers from the model. None of these are actually in the training set; these are made up. Yeah, they look quite real but they're actually made up from the model. So a recurrent neural network is basically this thing here, a box in green, and it has a state and it basically receives through time. It receives input vectors, so at every single time step we can feed in an input vector into the RNN and it has some state internally and then it can modify that state as a function of what it receives at every single time step. And so they're all, of course, being weights inside the RNN, and so when we tune those weights, the RNN will have different behavior in terms of how its state evolves as it receives these inputs. Now usually we can

also be interested in producing an output based on the RNN state. So we can produce these vectors on top of the RNN, but so you'll see show pictures like this, but I just like to know that the RNN is really just the block in the middle where it has a state that it can receive vectors over time and then we can base some prediction on top of its state in some applications. Okay, so concretely, the way this will look like is the RNN has some kind of a state which here I'm denoting as a vector H, and but this can be also a collection of vectors or just a more general state, and we're going to base it as a function of the previous hidden state at previous iteration time t minus 1 and the current input vector x_t, and this is going to be done through a function which I'll call a recurrence

function f. And that function will have parameters W, and so as we change those Ws, we're going to see that the RNN will have different behaviors. And then, of course, we want some specific behavior for the RNN, so we're going to be training those weights on data. So you'll see examples of that soon. For now, I'd like you to note that the same function is used at every single time step. We have a fixed function f of weights W, and we apply that single function at every single time step, and that allows us to use the recurrent network on sequences without having to commit to the size of the sequence because we apply the exact same function at every single time step, no matter how long the input or output sequences are. So in a specific case of a recurrent neural network, a vanilla recurrent neural network, the simplest way you can set this up, the simplest recurrence you can use, is what I'll refer to as a vanilla RNN. In this

case, the state of a recurrent neural network is just a single hidden state H, and then we have a recurrence formula that basically tells you how you should update your hidden state H as a function of the previous hidden state and the current input x_t. In particular, in the simplest case we're going to have these weight matrices W_hh and W_xh and they're going to basically project both the hidden state from the previous time step and the current input, and then those are going to add, and then we squish them with a tanh, and that's how we update the hidden state at time t. So this recurrence is telling you how H will change as a function of its history and also the current input at this time step. And then we can base prediction—we can base predictions on top of H, for example using just another matrix projection on top of the hidden

state. So this is the simplest complete case in which you can wire up a neural network. Okay, so just to give you an example of how this will work: so right now I've just talked about x, h, and y in abstract forms in terms of vectors. We can actually endow these vectors with semantics, and so one of the ways in which we can use a recurrent net is in the case of character-level language models, and this is one of my favorite ways of explaining RNNs because it's intuitive and fun to look at. So in this case, we have character-level language models using RNNs, and the way this will work is we will feed a sequence of characters into the recurrent neural network, and at every single time step we'll ask the recurrent neural network to predict the next character in a sequence—so a prediction, an entire distribution for what it thinks should come next in the sequence that it has seen so far. So suppose that in this

very simple example we have the training sequence "hello," and so we have a vocabulary of four characters that are in this data set: h, e, l, and o, and we're going to try to get a recurrent neural network to learn to predict the next character in a sequence on this training data. And so the way this will work is we'll set up—we'll feed in every one of these characters one at a time into a recurrent neural network. So you'll see me feed in h at the first time step, and here the x-axis is the time. So we'll feed in an h, then we'll feed in an e, l, l, and here I'm encoding characters using what we call a one-hot representation, where we just turn on the bit that corresponds to that character's order in the vocabulary. Then we're going to use the recurrence formula that I've shown you where at every single time step—suppose we start off with h as all zero and then we apply this recurrence to compute the hidden state vector at every single time—

Step using this fixed recurrence formula. So, suppose here we have only three numbers in the hidden state. We're going to end up with a three-dimensional representation that basically, at any point in time, summarizes all the characters that have come until then. And so, we have—do we apply this recurrence at every single time step, and now we're going to predict at every single time step what should be the next character in a sequence. So, for example, since we have four characters in this vocabulary, we're going to predict four numbers at every single time step. So, for example, in the very first time step, we fed in the letter "h," and the RNN, with its current setting of weights, computed these unnormalized log probabilities here for what it thinks should come next. So, it thinks that "h" is 1.0 likely to come next, it thinks that "e" is 2.2 likely, "l" is

Negative three likely and O is 4.1 likely right now in terms of unknown wise lock probabilities. Of course, we know that in this training sequence, we know that each should follow H. So in fact, this 2.2, which I'm showing in green, is the correct answer in this case, and so we want that to be high and we want all these other numbers to be low. And so every single time step we have basically a target for what next character should come in the sequence, and so we just want all those numbers to be high and all the other numbers to be low. And so that's, of course, encoded in the gradient signal of the loss function and then that gets backpropagated through these connections. So another way to think about it is that every single time step, we basically have a softmax classifier. So every one of these is a softmax classifier over the next character, and at every single point we

Know what the next character should be, and so we just get all those losses flowing down from the top, and they will all flow through this graph backwards through all the arrows. We're going to get gradients on all the weight matrices, and then we'll know how to shift the matrices so that the correct probabilities are coming out of the Arnon. So we'd be shaping those weights so that the correct behavior, the Arlen has the correct behavior as you're feeding characters. So you can imagine how we can train this over data. Are there any questions about this diagram? Good. Yeah, thank you. So at every single time step, we're flying, as I mentioned, the same recurrence, the same functions always. So we have a single W X agent every time step, we have a single W H Y at every time step, and the

Same W_h applied at every time step here, so we've used W_X_h, W_h_y, W_h_h four times in this diagram. And in backpropagation, when we backpropagate through, you'll of course have to account for that because we'll have all these gradients adding up to the same weight matrices, because it has been used at multiple time steps. And this is what allows us to process, you know, variably sized inputs, because that's every time step we're doing the same thing, so not a function of the absolute amount of things in your input. Okay, question: what are common things for initializing the first h_0? I think setting it to zeros is quite, quite a common set. H in the beginning to zero. Good. Does the order in which we receive the

Dataset matter, yes, because—so are you asking if I plugged in these characters in a different order? Yeah, so if you see, if this was a longer sequence, the order in this case— in this case, always doesn't matter because at every single point in time, if you think about it functionally, like this hidden state vector at this time step is a function of everything that has come before it, right? And so this order just matters for as long as you're feeding it in. We're going to go through some—us through some specific examples, which I think will clarify some of these points. Okay, so let's look at a specific example. In fact, if you want to train a character-level language model, it's quite short. So I wrote a gist that you can find on GitHub where this is 100 lines.

Implementation in NumPy for a character-level RNN that you can go through. I'd actually like to step through this with you so you can see concretely how we could train a recurrent neural network in practice. And so I'm going to step through this code video, and so we're going to go through all the blocks. In the beginning, as you'll see, the only dependence here is NumPy. We're loading in some text data, so our input here is just a large collection of a large sequence of characters—in this case, a text input txt file. And then we get all the characters in that file and we find all the unique characters in that file. And then we create these mapping dictionaries that map from characters to indices and from indices to characters. So we basically order our characters. So say we've read in a whole bunch of file and a whole bunch of data and we have a hundred characters or something like that.

And we've ordered them in a sequence, so we associate indices to every character. Then, here we're going to do some initializations. First, our hidden size is a hyperparameter, as you'll see with recurrent neural networks. So here, I'm choosing it to be 100. Here we have a learning rate. Sequence length here is set to 25. This is a parameter that you'll become aware of with RNNs. Basically, the problem is if our input data is way too large, say like millions of time steps, there's no way you can put an RNN on top of all of it, because you need to maintain all of this stuff in memory so that you can do backpropagation. So, in fact, we won't be able to keep all of it in memory and do backprop through all of it. So we'll go in chunks through our EMPA data. In this case, we're going through chunks of 25 at a time. So, as you'll see in a bit, we have this entire data set, but we'll be going in chunks of 25.

Characters at a time, and at every time we're just going to backpropagate through 25 characters at a time because we can't afford to do backpropagate for longer because we'd have to remember all that stuff. And so we're going in chunks here of 25. And then we have all these W matrices that here I'm initializing randomly, and some biases, so WX, H, HH, and hy, and those are all of our hi, our parameters that we're going to train with backprop. Okay, now I'm going to skip over the last function here, and I'm going to skip to the bottom of the script here. We have a main loop and I'm going to go through some of this main loop now. So there are some initializations here of various things to 0 in the beginning, and then we're looping forever. What we're doing here is I'm sampling a batch of data, so here is where I actually take a batch of 25 characters out of this data set, so

that's in the list inputs. And the list inputs basically just has 25 integers corresponding to the characters. The targets, as you'll see, is just all the same characters but offset by 1 because those are the indices that we're trying to predict at every single time step. So the inputs and targets are just lists of 25 characters. Targets is offset by 1 into the future. So that's where we sample basically a batch of data here. This is some sampling code. So at every single point in time as we're training this RNN, we can of course try to generate some samples of what it currently thinks characters should actually—what these sequences look like. So the way we use character, no, low-level RNNs at test time is that we're going to seed it with some characters, and then

this RNN basically always gives us the distribution of the next character in a sequence. So you can imagine sampling from it and then you feed in the next character again and you sample from the distribution and keep feeding it in. So you keep feeding all the samples into the RNN and you can just generate arbitrary text data. So that's what this code will do, and it calls the sample function. So we'll go into that in a bit. Then here I'm calling the loss function. The loss function receives the inputs, the targets, and it receives also this H prime. H prime is short for hidden state vector from the previous chunk, so we're going in batches of 25, and we are keeping track of what is the hidden state vector at the end of your 25 letters so that we can, when we feed in the next batch, we can feed that in as the initial H at that time step. So we're making sure that the hidden states are basically correctly propagated from

batch to batch through that H prime variable. But we're only backpropagating those 25 time steps, so we feed that into loss function and we get the loss and the gradients on all the weight matrices and all the biases. And here I'm just printing the loss, and then here's a parameter update where the last function told us all the gradients, and here we are actually performing the update, which you should recognize as an AdaGrad update. So I have all these cached things and all these cached variables for the gradient squared, which I'm accumulating, and then performing the AdaGrad update, go. So I'm going to go into the last function and what that looks like now. The last function is this block of code. It really consists of forward and a backward method, so we're computing the forward pass and then the backward pass in green. So I'll go through those two

steps. So in the forward pass you should recognize basically we get those inputs, the targets, we're iterating, receive these 25 indices and we're now iterating through them from 1 to 25. We create this X input vector which is just zeros and then we set the one-hot encoding. So whatever the index in the inputs, we turn that bit on for with the one. So we're feeding in the character with a one-hot encoding. Then here I'm computing the recurrence formula using this equation. So h_s at t here, h_s and all these things. I'm using dictionaries to keep track of everything at every single time step, so we compute the hidden state vector and the output vector using the recurrence formula and these two lines, and then over there I'm computing the softmax function. So I'm normalizing this so that we get probabilities, and then

your loss is negative log probability of the correct answer. So that's just a softmax classifier loss over there. So that's the forward pass, and now we're going to backpropagate through the graph. So in the backward pass we go backwards through that sequence from 25 all the way back to 1, and maybe you'll recognize—I don't know how much detail level to go in here—but you'll recognize that I'm backpropagating through a softmax, I'm backpropagating through the activation functions, I'm backpropagating through all of it, and I'm just adding up all the gradients and all the parameters. And one thing to note here especially is that these gradients on weight matrices like Whh, I'm using a plus equals because at every single time step all of these weight matrices get a gradient and we need to accumulate all of it into all the weight matrices because we keep using all these weight matrices at the same, at every time step.

And so we just backprop into them over time, and that gives us the gradients. And then we can use that in loss function, perform the parameter update. And then here we have finally a sampling function, so here is where we try to actually get the RNN to generate new text data based on what it has seen in a training data and based on the statistics of the characters and how they follow each other in the training data. So we initialize with some random character and then we go for until we get tired and we compute the recurrence formula, get the probability distribution, sample from that distribution, re-encode it in one-hot k-1 representation, and then we feed it in the next time step. So we keep iterating this until we actually get a bunch of text. So is there

Any question over just like the rough layout of how this works? Good. That's right. Yeah, that's right. Exactly. So we have basically 25 softmax classifiers at every batch, and we backprop all of those at the same time. And they'll all add up in the connections going

backwards. That's right. Okay, good. Do we use regularization here? You'll see that I probably do not. Yeah, I guess I skipped it here, but you can. In general, I think sometimes I tried regularization, and I don't think it's as common to use it in recurrent nets as outside. Sometimes it gave me worse results, so sometimes I skip it. But it's kind of a hyperparameter. Good. Yeah, that's right. So yeah, that's right. So in the sequence of 25 characters here, we are very low level on character level, and we don't actually care about words.

We don't know that words exist, as just characters, indices. So this RNN in fact doesn't know anything about characters or language or anything like that. It's just indices and sequences of indices, and that's what we're modeling. Yeah, good. Can we use spaces as the limiters or something like that instead of just constant batches of 25? I think he maybe could, but then it kind of just, you have to make assumptions about language. We'll see soon why you wouldn't actually want to do that because you can plug anything into this, and we'll see that we can have a lot of fun with that. Okay, so let me show you now what we can do. We can take a whole bunch of text. We don't care where it came from. It's just a sequence of characters, and we feed it into the RNN, and we can train the Arnon

to create text like it. And so, for example, you can take all of William Shakespeare's works. You can calculate all of it. It's just a giant sequence of characters, and you put it into the recurrent neural network, and you try to predict the next character in a sequence for William Shakespeare proponents. And so when you do this, of course, in the beginning, the recurrent neural network has random, random parameters, so it's just producing a garble. At the very end, so it's just random characters. But when you train the RNN, we'll start to understand that, okay, there are actually things like spaces, there's words, it starts to experiment with quotes, and it basically learned some of the very short words like here or on and so on. And then, as you train more and more, this just becomes more and more refined and the network learns that when you open a quote, you should close it later.

Or that the sentences end with a cut with a dot. It learns all of this stuff statistically, just from the raw patterns, without actually having to hand-code anything. And in the end, you can sample entire infinite Shakespeare based on this on a character level. So just to give you an idea about what kind of stuff comes out: "Alas, I think he shall become approached, and the DameWare little strain would be attained into being never fed, and who is but the chain and subject of his death? I should not sleep." This is the kind of stuff that you would get out of this recurrent network. Good. Yeah, thank you. So you are bringing up a very subtle point, which I'd like to get back to in a bit. Yeah? Okay. So we can run this on Shakespeare, but we can run this

on basically anything. So we were playing with this with Justin, I think, like roughly a year ago. And so Justin took, he found this book on algebraic geometry. And this is just a large LaTeX source file, and we took that LaTeX source file for this algebraic geometry and fed it into the RNN, and the RNN can learn to basically generate mathematics. So this is a sample. So basically this Ireland just spits out LaTeX, and then we compile it. And of course, it doesn't work right away. We had to tune it a tiny bit, but basically the Ireland, after we tweaked some of the mistakes that it has made, you can compile it, and you can get generated mathematics. And so you'll see that it basically creates all these proofs. It puts in stupid little squares at the ends of proofs. It creates lemmas, and so on. Sometimes

the RNN also tries to create diagrams, to varying amounts of success. My best, my favorite part about this is that on the top left, the proof here, it's emitted the Sarna was just lazy, but otherwise this stuff is quite indistinguishable, I would say, from actual algebraic geometry. So let X be ML0 scheme of X. Okay, I'm not sure about that part, but otherwise the gestalt of this looks very good. So you can throw arbitrary things at it. So I tried to find the hardest arbitrary thing that I could throw at the character-level RNN. I decided that source code is actually very difficult. So I took all of Linux source, which is just all the like C code. You concatenate it, and you end up with, I think, 700 megabytes of just C code and header files. And then you just throw it

into the Arnon, and then it can learn to generate code. And so this is generated code from the Arnon, and you can see that basically it creates function declarations. It knows about inputs syntactically. It makes very few mistakes. It knows about variables and sort of how to use them. Sometimes it indents the code. It creates its own bogus comments. Like syntactically, it's very rare to find that it would open a bracket and not close it, and so on. This actually is relatively easy for the Ireland to learn. And so some of the mistakes that it makes actually is that, for example, it declares some variables that it never ends up using, or it uses some variables that it never declared. So some of this high-level stuff is still missing, but otherwise it can do code just fine. It also knows how to recite the GNU GPL license character by character. That has

Learned from data, and it knows that after the GNU GPL license there are some include files, there's some macros, and then there's some code. So that's basically what it has learned. Good. Yeah, so what I intended just to show you is very small, just a toy thing to show you what's going on. Then there's a CharM, which is a more kind of a maturing Plantasia and Torch, which is just MiniCharD and scaled up and runs on GPU, and so you can play with that yourself. And so this, in particular, was a — we'll go into this by the end lecture. It's a three-layer LSDM, and so we'll see what that means. It's a more complex kind of form of a recurrent neural network. Okay, just to give you an idea about how this works, so this is from a paper that we played with a lot last year, and we were basically trying

to pretend that we're neuroscientists and we, you know, threw a careful level RNN on some test text. And so the RNN is reading this text. In the snippet of code we're looking at a specific cell in the hidden state of the RNN, or coloring the text based on whether or not that cell is excited or not. Okay, so you can see that many of the hidden state neurons are not very interpretable. They kind of fire enough in kind of weird ways, because some of them have to do quite low-level character-level stuff, like how often does it come after H, and stuff like that. But some of the cells are quite interpretable. So, for example, we find cells like a quote detection cell. This cell just turns on when it sees a quote, and then it stays on until the quote closes. And so this quite reliably keeps track of this, and it just comes

out from backpropagation. The RNN just decides that the character-level statistics are different inside and outside of quotes, and this is a useful feature to learn. And so it dedicates some of its hidden states to keeping track of whether or not you're inside a quote. And this goes back to your question, which I wanted to point out here, that this RNN was trained on, I think, a sequence length of 100. But if you measure the length of this quote, it's actually much more than 100. I think it's like 250. And so we worked on — we only backpropagated up to 100, and so that's the only place where this cell can actually like learn itself, because it wouldn't be able to spot the dependencies that are much longer than that. But I think basically this seems to show that you can train this character-level detection cell as useful on sequences

less than 100, and then it generalizes properly to longer sequences. So this — so this cell seems to work for more than 100 steps, even if it was only trained — even if it was only able to spot the dependencies on less than 100. This is another dataset here. This is, I think, Leo Tolstoy's War and Peace. This is in this dataset — there's a newline character at every single, roughly 80 characters in. So after every 80 characters, roughly, there's a newline. And there's a quote, there's a line length tracking cell that we found, where it starts off at like 1, and then it slowly decays over time. And you might imagine that a cell like this is actually very useful in predicting that newline character at the end, because this RNN needs to count 80 time steps so that it knows when a newline character is likely to come next.

Okay, so the line tracking tells, we found cells that actually respond only if sight if statements. We found cells that only respond inside quotes and strings. We found cells that get more excited the deeper you nested expression, and so all kinds of interesting cells that you can actually find inside these RNNs that completely come out just from the backpropagation. And so that's quite magical, I suppose. Good. Yeah, so there's a sin this LSDM. I think there were 2,100 cells, so you just kind of like go through them. So most of them look like this, but I would say roughly five percent of them you spot something interesting, so you just go through it manually.

Oh no, sorry, so we are completely running the entire RNN intact, but we're only looking at a single hidden state, the firing of one single cell in the RNN. So running the RNN normally, but we're just kind of recording from one cell in the hidden state, if that makes sense. So this cell, just the entire RNN, I'm only visualizing one part of the hidden state. Basically, there's many other hidden, still hidden cells that evolve in different ways, and they're all evolving at different times, and they're all doing different things inside the RNN hidden state. Good, we'll go into — are you asking about

multi-layer RNNs and so on? We'll go into that in a bit. This is, I think, multi-layer RNN, but you can get similar results with one layer. Good. Yes, so these hidden state cells were always between negative one and one. They're an output of a tanh, and this is from an LSTM, which we haven't covered yet. But the firing of these cells is between negative one and one, so that's the scale that sets this picture. Okay, cool. Okay, so RNNs are pretty cool, and you can actually train these sequence models over time. About roughly one year ago, several people have kind of realized that you can actually — is this a very neat application in the context of computer vision to perform image captioning. So in this context, we're taking a single image, and we'd like to describe it with a sequence of words, and these RNNs are very good at

Understanding how sequences develop over time. So in this particular model that I'm going to describe, this is actually work from roughly a year ago, happens to be my paper. I have pictures from my paper, so I'm going to use those. So we're feeding an image into a convolutional neural network, and then you'll see that this full model is actually just made up of two modules. There's the ConvNet that is doing the processing of the image, and there's a recurrent net which is very good at modeling sequences. And so if you remember my analogy from the very beginning of the course, where this is kind of like playing with Lego blocks, we're going to take those two modules and stick them together. That corresponds to the arrow in between. And so what we're doing effectively here is we're conditioning this RNN generated model. We're not just telling it to sample text at random, but we're conditioning that

generative process by the output of the convolutional network, and I'll show you exactly how that looks like. So suppose I'm just going to show you what the forward pass of the neural net is. So suppose we have a test image and we're trying to describe it with a sequence of words. So the way this model would process the image is as follows: we take that image and we plug it into a convolutional neural network. In this case, this is a VGG net. So we go through a whole bunch of conv, max pool, and so on until we arrive at the end. Normally, at the end, we have this softmax classifier which is giving you a probability distribution over, say, 1,000 categories of ImageNet. In this case, we're going to actually get rid of that classifier, and instead, we're going to redirect the representation at the top of the convolutional network into the recurrent neural network. So we begin the generation of the RNN with

a special start vector. So the input to this RNN was, I think, 300 dimensional, and this is a special 300-dimensional vector that we always plug in at the first iteration. It tells the RNN that this is the beginning of the sequence, and then we're going to perform the recurrence formula that I've shown you before for vanilla recurrent neural networks. So normally, we compute this recurrence which we've seen already, where we compute wxh times x plus whh times h, and now we want to additionally condition this recurrent neural network not only on the current input and the current hidden state, which we initialize with zero, so that term goes away at the first time step, but we additionally condition just by adding w_ih times v. And so this v is the top of the ConvNet here, and we basically this added interaction added

weight matrix W which tells us how this image information merges into the very first time step of the recurrent neural network. Now, there are many ways to actually play with this recurrence, many ways to actually plug in the image into the RNN, and this is only one of them and one of the simpler ones perhaps. And at the very first time step here, this y_0 vector is the distribution over the first word in a sequence. So the way this works, you might imagine, for example, is you can see that these straw textures in a mass hat can be recognized by the convolutional network as a straw-like stuff, and then through this interaction w_ih, it might condition the hidden state to go into a particular state where the probability of the word "straw" could be slightly higher, right? So you might imagine that the straw-like textures can influence the probability of "straw," so one of the numbers inside y

0 to be higher because there are straw textures in there. And so the RNN from now on has to kind of juggle two tasks: it has to predict the next character in the sequence in this case, and it has to remember the image information. So we sample from that softmax and, supposedly, the most likely word that we sampled from that distribution was indeed the word "straw." We would take "straw," and we would try to plug it into the recurrent neural network on the bottom again. And so in this case, I think we were using word-level embeddings, so the "straw" word is associated with a 300-dimensional vector which we're going to learn. We're going to learn a 300-dimensional representation for every single unique word in the vocabulary, and we plug in those 300 numbers into the RNN and forward it again to get a distribution over the second word in the sequence inside y_1. So we get all these probabilities, we sample

from it again. Suppose that the word "hat" is likely now; we take hat's 300-dimensional representation, plug it in, and get the distribution over there, and then we sample again, and we sample until we sample a special end token, which is really the period at the end of the sentence. And that tells us that the RNN is now done generating. And at this point, the RNN would have described this image as "a straw hat." Period. Okay. So the number of dimensions in this y vector is the number of words in your vocabulary plus one for the special end token, and we are always feeding in these 300-dimensional vectors that correspond to different words and a special start token, and then we always just backpropagate through the whole thing at a single time. So you initialize this at random, or you can initialize your VGG net with pre-trained from Internet, and

then the recurrent neural networks tell you the distributions, and then you encode the gradient, and then you backprop through the whole thing as a single model, and you just train that all jointly, and you get a captioner, image captioner. Lots of questions? Okay, good. Yes. These 300-dimensional embeddings are just independent of the image, so every word has 300 numbers associated with it, so we're going to backpropagate into it. So you initialize it randomly, and then you can backpropagate into these vectors x, right? So those embeddings will shift around; they're just parameters. Another way to think about it is it's, in fact, equivalent to having a one-hot representation for all the words, and then you have a giant W matrix where

Every single time you multiply W with that one-hot representation, and if that W has 300 output size, then it's effectively plucking out a single row of W, which ends up being your embedding. So it's kind of equivalent. Just think of it if you don't like those embeddings, just think of it as a one-hot representation, and you can think of it that way. Go ahead. Yes, the model learns to output the end token. Yes, so in the training data, the correct sequence that we expect from the RNN is the first word, second or third word, end. So every single training example will sort of have a special end token in it. Go ahead. H2R: You again receiving the same output from the ABG net, or are you asking like—

It twice? Yeah, thank you. So the question is like, where? So in this example, we're only plugging in the image at the very first time step. We're not plugging it in at the other time steps. You can wire this up differently where you plug it into every single state. It turns out that that actually works worse. So it actually works better if you just plug it in the very first time step, and then the RNN has to juggle these both tasks. It has to remember the image, what it needs to remember through the RNN, and it also has to produce all these outputs, and somehow it wants to do that. There's some hand-waving reasons I can give you after class for what that's true. You just have to basically be careful, enjoy the confident output when you give it the start, okay? And then give it the subsequent tokens in the labels and the end token into the next generation.

The next current event? I think you're kind of not quite—so at training time, a single instance will correspond to an image and a sequence of words. And so we would plug in those words here, and we will plug in that image, and we— Yes, so like, um, so you see it: training time, you have all those words plugged in on the bottom, and you have the image plugged in, and then you unroll this graph, and you have your losses and your backprop, and then you can do batches of images if you're careful. And so if you have batches of images, they sometimes have different length sequences in the training data. You have to be careful with that because you have to say that, okay, I'm willing to process batches of up to, you know, 20 words maybe, and then some of those sentences will be shorter and longer, and you need to, in your code—

You know, worry about that, because some, some, some sentences are longer than others. We have way too many questions. I have stuff too. Good. Yeah, thank you. So we backpropagate everything completely jointly, end-to-end training. So you can pre-train with ImageNet and then you put those weights there, but then you just want to train everything jointly, and that's a big advantage actually, because we can figure out what features to look for in order to better describe the images at the end. Okay, so when you train this in practice, we train this on image-sentence datasets. One of the more common ones is called Microsoft COCO. So just to give you an idea of what it looks like, it's roughly a hundred—

thousand images and five sentence descriptions for each image. These were obtained using Amazon Mechanical Turk, so you just ask people, "Please give us a sentence description for an image," and you record all of it, and then that's your dataset. And so when you train this model, the kinds of results that you can expect are roughly what is kind of like this. So this is our end describing these images. So this, it says that this is a man in black shirt playing guitar, or a construction worker in orange safety vest working on the road, or two young girls are playing with Lego toy, or a boy is doing backflip on the wakeboard. And of course, that's not a wakeboard, but it's close. There are also some very funny failure cases, which I also like to show. This is a young boy holding a baseball bat. This is a cat staying on the couch with the remote control, that's—

a woman holding a teddy bear in front of a mirror. I'm pretty sure that the texture here probably is what happened, made it think that it's a teddy bear. And the last one is a horse standing in the middle of a street. There's road, so there's no horse obviously, so I'm not sure what happened there. So this is just the simplest kind of model that came out last year. There were many people who tried to work on top of these kinds of models and make them more complex. I just like to give you an idea of one model that is interesting just to get an idea of how people play with this basic architecture. So this is a paper from last year where, if you noticed, in the current model, we only feed in the image a single x time at the beginning. And one way you can play with—

this is that you can actually unroll the recurrent neural network to look back to the image and reference parts of the image while it's describing the words. So as you're generating every single word, you allow the RNN to actually make a lookup back to the image and look for different features of what it might want to describe next. And you can actually do this in a fully trainable way, so the RNN not only creates these words but also decides where to look next in the image. And so the way this works is not only does the RNN output your probability distribution of the next word in a sequence, but this conv net gives you this volume. So say, in this case, we forwarded the ConvNet and got a 14 by 14 by 512 activation volume, and at every single time step, you don't just omit that—

Distribution, but you also emit a 512-dimensional vector that is kind of like a lookup key of what you want to look for next in the image. And so actually, I don't think this is what they did in this particular paper, but this is one way you could wire something like this up. And so this vector is emitted from the RNN just like a regular output; it's just predicted using some weights. And then this vector can be dot-producted with all these 14 by 14 locations. So we do all these dot products and we achieve, or we compute, basically a 14 by 14 compatibility map. And then we put a softmax on this, so basically we normalize all this so that it all—you get this, what we call an attention over the image. So it's a 14 by 14 probability map over what's interesting for the RNN right now in the image. And then we use this

probability mask to do a weighted sum of these guys with this saliency. And so this RNN can basically emit these vectors of what it thinks is currently interesting for it, and it goes back, and you end up doing a weighted sum of different kinds of features that the LSTM wants to, or the RNN wants to, look at at this point in time. And so, for example, the RNN is generating stuff and it might decide that, okay, I'd like to look for something object-like now. It emits a vector of 512 numbers of object-like stuff. It interacts with comments with the cognate activation volume, and maybe some of the object-like regions of that comb or of that activation volume light up in the saliency map in this 14 by 14 array. And then you just end up basically focusing your attention

on that part of the image through this interaction. And so you can basically just do lookups into the image while you're describing the sentence. And so this is something we refer to as soft attention and will actually go into this in a few lectures. So we'll go into a couple of cover things like this where the RNN can actually have selective attention over its inputs as it's processing the input. And so that's—so I just wanted to bring it up roughly now just to give you a preview of what that looks like. Okay, now if we want to make our lens more complex, one of the ways we can do that is to stack them up in layers. So this gives you, you know, more deep stuff, usually works better. So the way we stack this up, one of the ways at least you can stack recurrent neural networks—and there's many ways—but this is just one of them that people use in practice is you can straight up just

plug RNNs into each other. So the input for one RNN is the hidden state vector of the previous RNN. So in this image, we have the time axis going horizontally, and then going upwards we have different RNNs. And so in this particular image, there are three separate recurrent neural networks, each with their own set of weights, and these recurrent neural networks are just feed into each other. Okay, and so this is all always trained jointly; there's no train first one, second, third one. That's all just a single computational graph that we backpropagate through. Now, this recurrence formula at the top, I've rewritten it slightly to make it more general, while still we're still doing the exact same thing as we did before. This is the same formula. We're taking a vector from below in depth and a vector from

before in time. We're concatenating them and putting them through this W transformation, and it's washing them at the 10 H. So if you remember, if you are slightly confused about this, there's there was a W_x h times x plus W_h h times H—you can rewrite this as a concatenation of X and H multiplied by a single matrix, right? So it's as if I stacked X and H into a single column vector, and then I have this W matrix where basically what ends up happening is that your W_x h is the first part of this matrix and W_h h is the second part of your matrix. And so this kind of formula can be rewritten into a formula where you stack all your inputs and you have a single W transformation. So the

same formula. Okay, so that's how we can stack these RNNs, and then they're now indexed by both time and by layer at which they occur. Now, one way we can also make these more complex is not just by stacking them, but by actually using a slightly better recurrence formula. So right now, so far, we've seen this very simple recurrence formula for the vanilla recurrent neural network. In practice, you will actually rarely ever use formula like this. A basic recurrent network is very rarely used. Instead, you'll use what we call an LSTM or a long short-term memory. So this is basically used in all the papers now, so this is the formula you'd be using also in your projects if you were to use recurrent neural networks. What I'd like you to notice at this point is it's everything is exactly the same as with a

RNN. It's just that the recurrence formula is a slightly more complex function. Okay, we're still taking the hidden vector from below in depth, like your input, and from before in time, the previous hidden state. We're concatenating them, putting them through a W transform, but now we have this more complexity in how we actually achieve the new hidden state at this point in time. So we're just being a little bit more complex in how we combine the vector from below and before to actually perform the update of the hidden state. It's just a more complex formula. So we'll go into some of the details of exactly what motivates this formula and why it might be a better idea to actually use an LSTM instead of RNN. Good. Yeah, you'll see sigmas and tanh, and it makes sense. Trust me, we'll go through it just click right now.

Mm-hmm. So if you look for LSTM online, you can look for LSTM when you go in Wikipedia or you go to Google Images, you'll find diagrams like this, which is really not helping, I think anyone. The first time I saw an LSTM really scared me. Like, this one really scared me. I wasn't really sure what's going on. I understand LSTMs, and I still don't know what these two diagrams are. So, okay, so I'm going to try to break down LSTM. It's kind of a tricky thing to put into a diagram. You really have to kind of step through it. So, lecture format is perfect for analysis. And, okay, so here we have the LSTM equations, and I'm going to first focus on the first part here on the top, where we take these two vectors from below and from

before. So, x and h, h subscript s are previous hidden state, and x is dean. But we map them through that transformation W. And now, if both x and h are of size n, so there's n numbers in them, we're going to end up producing four n numbers, okay, through this W matrix, which is 4n by 2n. So we have these four n-dimensional vectors i, f, o, mg. They're short for input, forget, output, and g. I'm not sure what that's short for; it's just g. And so, the i, f, and o go through sigmoid gates and g goes through a tanh gate. Now, the way this—let's see—so the way this actually works, the LSTM, basically the best way to think about it is—one thing I forgot to mention, actually, in the previous slide is, normally, recurrent neural network

just has the single h vector at every single time step, and LSTM actually has two vectors at every single time step: the hidden vector, and this is what we call c, the cell state vector. So, at every single time step, we have both h and c in parallel, and the c vector here shown in yellow. So, we basically have two vectors every single point in space here. And what LSTMs are doing is they're basically operating over this cell state. So, depending on what's before you and below you, and that is your context, you end up operating over the cell state with these i, f, o, g elements. And the way to think about it is—I'm going to go through a lot of this. Ok, so basically, the way to think about this is think of i and o as just binary

either 0 or 1. We want them to be—we want them to have an interpretation of a gate, like think of it as either zeros or ones. We, of course, make them later sigmoids because we want this to be differentiable so that we can back propagate through everything. But just think of i, f, and o as just binary things that we're computing based on our context. And then what this formula is doing here, see, you can see that based on what these gates are and what g is, we're going to end up updating this c value. And in particular, this f is called the forget gate that will be used to shut a—to reset some of the cells to zero. So the cells are best thought of as counters, and these counters basically we can either reset them to 0 with this f interaction. This is an element-wise

multiplication there. I think my laser pointer is running out of battery. That's unfortunate. So, with this f interaction, if f is zero, then you can see that will zero out a cell, so we can reset the counter. And then we can also add to a counter. So we can add through this interaction i times g, and since i is between 0 and 1 and g is between negative 1 and 1, we're basically adding a number between negative 1 and 1 to every cell. So, at every single time step, we have these counters in all the cells. We can reset these counters to 0 with the forget gate, or we can choose to add a number between negative 1 and 1 to every single cell. Ok, so that's how we perform the cell update, and then the hidden update ends up being a squashed cell, so tanh of c squashed

cell that is modulated by this output gate. So, only some of the cell state ends up leaking into the hidden state as modulated by this vector o. So we only choose to reveal some of the cells into the hidden state in a learnable way there. So there are several things to kind of highlight here. One, maybe most confusing part here is that we're adding a number between negative 1 and 1 with i times g here, but that's kind of confusing because if we only had a g there instead, then g is already between negative 1 and 1. So why do we need i times g? What is that actually giving us when all we want is to increment a cell by a number between negative 1 and 1? And so, that's kind of like a subtle part about an LSTM. I think one answer is that if you think

about g, it's a function of—it's a linear function of your context. No one has... it has layers. A laser pointer by any chance, right? Ok. Oh, man. Ok. So g is a function of your... g goes through a tanh. Okay, so g is a linear function of your previous context squashed by tanh, and so if we were adding just g instead of i times g, then that would be kind of like a very simple function. So by adding this i in there, and having multiplicative interaction, you're actually getting a richer function that you can actually express in terms of what we're adding to our cell state as a function of the previous h's. And another way to think about this is that we're basically decoupling these two concepts of how

Much do we want to add to a cell state, which is G, and then do we want to add to a cell state, which is I? So I is like, do we actually want this operation to go through, and G is what do we want to add? And by decoupling these two, that also may be dynamically has some nice properties in terms of how this LSTM trains. But we just end up, that's like the Ostian formulas, and I'm going to actually go through this in more detail as well. But maybe I should go through it now. Okay, so think about this as cells flowing through and now the first interaction here is the F dot C. So F is an output of a sigmoid off of that, and so F is basically gating your cells with a multiplicative interaction. So if F is a zero, you will shut off the cell and

reset the counter. This I times G part is basically giving you a camper as basically adding to the cell state, and then the cell state leaks into the hidden state, but it only leaks through tanh, and then that gets gated by O. So the O vector can decide which parts of the cell state to actually reveal into the hidden, hidden cell. And then you'll notice that this hidden state not only goes to the next iteration of the LSTM, but it also actually flows up to higher layers because this is the hidden state vector that we actually end up plugging into further LSTMs above us, or that goes into a prediction. And so when you unroll this, basically the way it looks like it's kind of like this, which now I have a confusing diagram of

my own. That's, I guess where we ended up with, but you get your input vectors from below, you have your hidden state from before, the tanh determines your gates: F, I, G, N, O—they're all n-dimensional vectors. And then they end up modulating how you operate over the cell state, and this cell state can, once you actually reset some counters and once you add numbers between negative one and one to your counters, the cell state leaks out, some of it leaks out in a learnable way, and then it can either go up to the prediction, or it can go to the next iteration of the LSTM going forward. And so that's the... so this looks ugly, so we're going to... So basically the question that's probably on your mind is why did we go through all of this? There's something, why does this look at this

particular way? I should like to note at this point that there are many variants to an LSTM, and I'll make this point by the end of the lecture. People play a lot with these equations, and we've kind of converged on this as being like a reasonable thing, but there's many little tweaks you can make to this that actually don't deteriorate your performance by a lot. You can remove some of those gates, like maybe the input gate, and so on. It turns out that this tanh of C, that can be just C, and it works just fine normally, but with a tanh of C it works slightly better sometimes, and I don't think we have very good reasons for why. And so you end up with a bit of a monster, but I think it actually kind of makes sense in terms of just these counters that can be reset to zero or you can add small numbers between negative one and one to them, and so it's kind of like a nice, it's actually relatively simple now to understand

exactly why this is much better than an RNN. And we have to go to a slightly different picture to draw the distinction. So the recurrent neural network, it has some state vector, right, and you're operating over it and you're completely transforming it through this recurrence formula, and so you end up changing your hidden state vector from time step to time step. You'll notice that the LSTM instead has these cell states flowing through, and what we're doing effectively is we're looking at the cells, and some of it leaks into the hidden state based on the hidden state we're deciding how to operate over the cell, and if you ignore the forget gates, then we end up basically just tweaking the cell by additive interaction here. So there's some stuff that looks like a function of the cell state and then whatever it

is, we end up additively changing the cell state instead of just transforming it right away. So it's an additive instead of a transformative interaction or something like that. Now, this should actually remind you of something that we've already covered in the class. What does it remind you of? Residual nets, right? Yeah, so in fact, like this is basically the same thing as we saw with ResNets. So normally with the ConvNet we're transforming representation. ResNet has these skip connections here, and you'll see that basically ResNets have this additive interaction, so we have this X here, now we do some computation based on X, and then we have an additive interaction with X. And so that's the basic block of a ResNet, and that's, in fact, what happens with an LSTM as well. We have these additive interactions,

where here the X is basically your cell, and we go off, we do some function, and then we choose to add to this cell state. But the LSTM is, unlike ResNet, have also these forget gates that we're adding, and these forget gates can choose to shut off some parts of the signal as well. But otherwise it looks very much like a ResNet. So I think it's kind of interesting that we're converging on very similar kind of looking architecture that works both in ConvNets and in recurrent neural networks, where it seems like dynamically somehow it's much nicer to actually have these additive interactions that allow you to actually backpropagate much more effectively. So to that point, think about the backpropagation dynamics between RNN and LSTM especially in the LSTM. It's very clear that if I inject some gradient signal, that sometimes that's

Here, so if I inject gradient signal at the end of this diagram, then these plus interactions are just like a gradient superhighway here, right? Like, these gradients will just flow through all the addings, addition interactions, right? Because addition distributes gradients equally, so if I plug in a gradient at one point in time here, it's just going to flow all the way back. And then, of course, the gradient also flows through these F's, and they end up contributing their gradients into the gradient flow. But you'll never end up with what we referred to, with our n ends problem, called vanishing gradients, where these gradients just die off, go to zero as you backpropagate through. And I'll show you an example concretely of why this happens in ViT. So, in an RNN, we have this vanishing gradient problem. I'll show you why that happens in an LSTM because of this.

Superhighway of just additions: these gradients at every single time step that we inject into the LSTM from above just flow through the cells, and your gradients don't end up vanishing at this point. Maybe I take some questions? Are there any questions about what's confusing here about LSTM? And then after that, I'll go into why RNNs have vanishing gradients problem. Good? Yeah. So this—oh, you're asking by the oh vector? Is that important? Turns out that I think that one specifically is not super important. So there's a paper I'm going to show you called an LSTM: Space Odyssey. They really played with this, take stuff out, put stuff in. There's also like these peephole connections you can—you can add. So this cell state here—

—that can be actually put in with the hidden state vector as an input. So people really play with this architecture, and they've tried lots of iterations of exactly these equations, and what you end up with—almost everything works about equal, some of it works slightly worse sometimes. So it's very kind of confusing in this way. I also show you a paper where they treated these update equations as just a—they built trees over the update equations, and then they did this like random mutation stuff, and they tried all kinds of different graphs and updates you can have. And most of them work about, some of them break, and some of them work about the same. But nothing, like, really that's much better than an LSTM. Any other questions? Well, LSTMs, let's go into why recurrent neural networks have—

—terrible backward flow. I'll show you a cute video also. So this is showing the vanishing gradients problem in recurrent neural networks with respect to LSTMs. So what we're showing here is we're looking at—we're unrolling a recurrent neural network over many periods, many time steps, and then we're injecting gradient at, say, the 128th time step, and we're backpropagating the gradient through the network, and we're looking at what is the gradient for, I think, the inputs to hidden matrix one of the weight matrices at every single time step. So remember that to actually get the full update through the batch, we actually end up adding all those gradients here. And so what's—

—what's being shown here is that as you backtrack, we've only injected gradient at one twentieth time steps. Everything we do backprop backwards through time, and this is showing the slices of that backpropagation. What you're seeing is that the LSTM gives you lots of gradients throughout this backpropagation, so there's lots of information that is flowing through. And this RNN just instantly dies off; that just the gradient, we say vanishes, just just becomes tiny numbers. There's no gradient. So in this case, I think in the case in about eight time steps or so, like ten time steps. And so all this gradient information that we've injected did not flow through the network, and you can't learn very long dependencies because all the correlation structure has been just died down. And so we'll see why this happens dynamically in a bit. There's some comments here which are—

—also funny. This is like YouTube or something again. Okay, anyways. Okay, so let's look at a very simple example here where we have a recurrent neural network that I'm going to unroll for you. In this recurrent neural network, I'm not showing any inputs; we're only have hidden state updates. So here, I'm initializing a weight W_hh, which is the hidden state to hidden interaction, and then I'm going to basically forward a recurrent neural network. This is a vanilla recurrent net for some T time steps here; I'm using T = 50. So what I'm doing is W_hh times the previous hidden time step, and then ReLU on top of that. So this is just a forward pass for an RNN and ignoring any input vectors coming in.

Is just W_hh times h threshold, W_hh times h threshold, and so on. So that's the forward pass, and then I'm going to do backward pass here. We're injecting a random gradient here at the last time step, so the 50th time step I inject some gradient which is random, and then I go backwards and I backprop. So when you backprop through this, right, you have to backprop through a ReLU here. I'm using a ReLU. You have to backprop through the W_hh multiply, then through ReLU, W_hh multiply, and so on. And so the thing to note here is so here I'm undoing the ReLU, here's where I'm backpropagating through the ReLU. Oh, I'm just fresh holding anything that where the inputs were less than zero. And here I am backpropagating the W_hh times h operation where we actually multiply it by the W_hh.

H matrix before we do the non-linearity. And so, there's something very funky going on when you actually look at what happens to these dH/dS, which is the gradient on the HS as you go backwards through time. It has a very kind of funny structure that is very worrying, as you look at like how this gets chained up in the loop. Like, what are we doing here with these two time steps taking that product of all zeros? Yeah, so I think that sometimes steps maybe the outputs, the railways were all dead and so you may have killed it, but that's not really the issue. The more worrying issue

is, well, that would be an issue as well, but I think one worrying issue that people definitely spot as well is you'll see that we're multiplying by this W_HH matrix over and over and over again. Because in the forward pass, we multiply by W_HH at every single iteration. When we backpropagate through all the hidden states, we end up backpropagating this formula W_H times HS, and the backprop turns out to actually be that you take your gradient signal and you multiply it by the W_HH matrix. And so we end up multiplying by W_HH, the gradient gets multiplied by W_HH, then thresholded, then multiplied by W_HH, thresholded, and so we end up multiplying by this matrix W_HH fifty times. And so the issue with this

is that the gradient signal basically... okay, two things can happen. Like, if you think about working with a scalar value, suppose that these were scalars, not matrices. If I take a number that's random and then I have a second number and I keep multiplying the first number by the second number again and again and again, what does that sequence go to? There's two cases, right? If I keep multiplying with the same number, either I die or it just goes completely... yes. So if your second number was exactly one, yeah, that's the only case where you don't actually explode, but otherwise really bad things are happening: either we die or we explode. And here we have matrices, we don't have a single number, but in fact the same thing happens. The generalization of it happens if the spectral radius of this W_HH matrix is

which is the Hutt than the largest eigenvalue of that matrix. If it's greater than one, then this gradient signal will explode. If it's lower than one, the gradient signal will completely die. And so basically, since the RNN has this very weird because of this recurrence formula, we end up with this very just terrible dynamics and it's very unstable and it just dies or explodes. And so in practice, the way this was handled was we can control the exploding gradient. One simple hack is if your gradient is exploding, you clip it, because people actually do this in practice. It's like a very patchy solution, but if your gradient is above 5 in norm, then you clamp it to 5 element-wise or something like that. So you can do that, it's called gradient clipping. That's how you address the exploding gradient problem and then your

conduct your recurrence don't explode anymore. But the gradients can still vanish in a recurrent neural network, and LSTMs are very good with the vanishing gradient problem because of these highways of cells that are only changed with additive interactions where the gradients just flow, they never die down. If you're... because you're multiplying by the same matrix again and again or something like that. So that's roughly why these are just better dynamically. So we always use LSTMs and we do do gradient clipping usually, so because the gradients in LSTMs can potentially explode still but they don't usually vanish. Good, thank you. So here I'm using ReLU. People sometimes use tanh in vanilla recurrent

neural networks as well. For LSTMs it's not clear where you would plug in. It's not clear in this equation like exactly how you would plug in a ReLU and where. Maybe instead of the tanh, I'm not sure. So instead of tanh, we would use g here really, but then these cells would only grow in a single direction, right? So maybe then you can't actually end up making it smaller, so that's not a great idea I suppose. Yeah, so there is basically there's no clear way to plug in a ReLU here. So yeah, one thing I'll note is that in terms of these super highways of gradients, this

viewpoint actually breaks down when you have forget gates because when you have forget gates where we can forget some of these apps with a multiplicative interaction, then whenever a forget gate turns on and it kills the gradient, then of course the backward flow will stop. So these super highways are only kind of true if you don't have any forget gates. But if you have a forget gate there, then it can kill the gradient. And so in practice when we play with LSTMs, where we use all stems, I suppose sometimes people when they initialize the forget gate, they initialize it with a positive bias because that biases that forget gate to turn on, to be always kind of turned off I suppose in the beginning. So in the beginning the gradients flow very well and then the LSTM can learn how to shut them off at once to the later on. So

people play with that bias on that forget gate sometimes. And so the last night here I wanted to mention that LSTMs media many people have basically played with this quite a bit. So there's a Search Space Odyssey paper where they try various changes to the architecture. There's a paper here that tries to do this search over a huge number of potential changes to the LSTM equations, and they did a large search and they didn't find anything that works substantially better than just an LSTM. So yeah, and then there's this GRU, which also is relatively actually popular and I would actually recommend that you might want to use this. A GRU is a change on the LSTM. It also has these additive interactions but what's nice about it is that it's a shorter

Smaller formula, and it only has the single H vector. It doesn't have an H and a C; it only has an H. So implementation-wise, it's just nicer to remember just a single hidden state vector in your forward pass, not two vectors, and so it's just a smaller, simpler thing that seems to have most of the benefits of an LSTM. But so it's called GRU, and it almost always works about equal with LSTM in my experience. And so you might want to use it, or you can use the new LSTM—they both kind of do about the same. And so summary is that RNNs are very nice, ah, but the raw RNN does not actually work very well, so you use LSTMs or GRUs instead. What's nice about them is that we're having these additive interactions that allow gradients to flow much better, and you don't get a vanishing gradient problem. We still have to worry a bit about the

Exploding gradient problem. So, it's common to see people clip these gradients sometimes, and I would say that better, simpler architectures are really trying to understand how come, like, there's something deeper going on with the connection between resonance and LSTMs, and there's something deeper about these additive interactions that I think we're not fully understanding yet, and exactly why that works so well, in which parts of it work well. And so, I think we need a much better understanding, both theoretical and empirical, in the space, and it's a very wide open area of research. And so, yeah, so it's, uh, for LSTM, that's the end of a class room with an LSTM. People still clip gradients that can, I suppose, still explode, so it's not.

It's clear why they would, but you keep injecting gradient into the cell state, and so maybe that gradient can sometimes get larger. So, it's common to clip them, but I think not as maybe important, maybe as an RNN, and I'm not 100 percent sure about that one. Good, your logical basis. I have no idea how. Being it's interesting, yeah. Okay, I think we should end the class here, but I'm happy to take more questions here.

From YouTube captions, lightly punctuated for readability. Timestamps link to the video above. Errors are possible.

Cite this recording

Andrej Karpathy, “CS231n Lecture 10: Recurrent Neural Networks (2017)”, Conference/meetup talk, February 2017. Transcript and video: Thinkers Archive, https://thinkersarchive.com/andrej-karpathy/recurrent-neural-networks-cs231n-lecture-10-2016/

Quoting a passage? Link to its exact moment: every timestamp above has its own anchor (for example https://thinkersarchive.com/andrej-karpathy/recurrent-neural-networks-cs231n-lecture-10-2016/#t-132).

Search every transcript
Open the full search page →