Transcript
0:04 >> Stanford University. >> About the full back propagation algorithm. And I promise you as the last bit of super heavy math- After that you can have a warm fuzzy feeling around most of the state of the art deep learning techniques. Both for natural language processing and even a little bit for computer vision a lot of other places. So, I know this can be a lot if you're not super familiar with multivariate calculus. And so, I'll actually describe backprop in four different ways today.
0:36 And hopefully bring most of you into the backprop the group of people who know back propagation really well. So 4 different descriptions of essentially the same thing. But hopefully, some will resonate more with some folks than others. And so to show you that afterwards we can have a lot more fun. Well, actually then the second sort of, well, not quite half, but maybe the last third, talk about the projects and encourage you to get started on the project.
1:07 Give you some advice on what the projects will likely entail if you choose to do a project instead of the last problem set. Maybe one small hint for problem set one. Again, it's super important to understand the math and dimensionality, and if you do that on paper, and then you still have some trouble in the implementation. It can be very helpful to essentially set break points and then print out the shape of all the various derivatives you may be computing, and that can help you in your debugging process a little bit.
1:41 All right, are there any questions around organization, problem sets, one, no? All right, my project, my office hours going to be a half hour after the class ends today. So if you have project questions, I'll be there after the class. All right, let's go to explanation number 1 for back propagation. And again, just to motivate you of why we want to go through this. Why do I torture some of you with all these derivatives? It is really important to have an actual understanding of the math behind most of deep learning.
2:15 And in many cases, in the future, you will kind of abstract the way backpropagation. You'll just kind of assume it works based on a framework, software package that you might use. But that sometimes leads you to not understand why your model might not be working, right? In theory, you say it's just abstracted away, I don't have to worry about it anymore. But really in practice, in the optimization you might run into problems. And if you don't understand the actual back propagation, you don't know why you will have these problems.
2:45 And so in addition to that, we kinda wanna prepare you to not just be a user of deep learning, but maybe even eventually do research. In this field and maybe think of and implement and be very, very good at debugging completely new kinds of models. And you'll observe that depending on which software package you'll use in the future, not everything is de facto supported in some of these frameworks. So if you want to create a completely new model that's sort of outside the convex Known things, you will need to implement, the forward and the backward propagation for a new sub-module that you might have invented.
3:20 So, you just have to trust me a little bit in why it's useful. Hopefully this helps. So last time we ended with this kind of neural network, where we had a single hidden layer. And we derive all the gradients for all the different parameters of this model, namely the word vectors here. The W, the weight matrix for our single hidden layer and the U for the simple linear layer here.
3:51 And we defined this objective function and we ended up writing, for instance, one such derivative here fully out where we have the indicator function whether we're in this regime or if it's zero. And if it's above zero, then this was the derivative. In here, I just rewrote the same derivative twice, essentially showing you that instead of having to recompute, this if you had basically stored during forward propagation the activations of this, this is exactly the same thing, so f(Wx + b) we defined as our hidden activation, a, then you could reuse those to compute derivatives.
4:34 Alright, now we're going to take it up a notch and add an additional hidden layer to that exact same model, and it's the same kind of layer but in out that we have 2, we have to be very careful here about our superscript which will indicate. The layers that we're in. So it's the same kind of window definition, we'll go over corpus, we'll select samples for our positive class and everything that doesn't for instance have an entity will be our negative class.
5:02 Everything else is the same, but we're adding one hidden layer to this. And so, let's go through the definition. We'll define x here, our Windows and our word vectors that we concatenated, as our first activations, our first hidden layer. And now to compute to intermediate representation for our second layer, just a linear part of that, we basically have here W1. A superscript matrix times x plus b1. And then to compute the activations A, superscript two of that will apply the element-wise nonlinearities, such as the sigmoid function.
5:39 All right, and then we'll define this here as z3, same idea but this could potentially have different dimensionalities, w1, w2 for instance don't have to have exactly the same dimensionality. Do the same thing again, element wise nonlinearity and we have the same linear layer at the top. All right, are there any questions around the definition of this here? >> >> The question is do those two element wise functions here have to be the same? And the answer is they do not.
6:08 And if fact this is something that you could cross-validate and try as different hyper-parameters of the model. We so far have only introduced to you the sigmoid. So let's assume for now, it's the same. But in a later lecture, I think next week, we'll describe a lot of other kinds of non-linearities. Yeah. How do you choose which of these functions. We'll go into all of that once we know that we have which or what the options are.
6:41 The best answer usually is, you let your data speak for yourself and you run experiments with a lot of different options. Once you do that, after a while you gain, again, certain intuitions. And you don't have to redo it every time. Especially if you have ten layers, you don't wanna go through the cross-product of five different nonlinearities. And then all the different variations. Usually, you get diminishing returns for some of those type of parameters. >> Question.
7:05 >> Yeah? >> Sorry, I didn't hear you. Right. So the question is,could we put the b into the w? And if that's confusing, you could essentially assume that b is this biased term here.
7:41 Is another element of this W matrix, if we add a single one to every activation that we have here. So, if a two frame, since we just added one here, then we could get rid of this bias term and we'd have an additional Row or column depending on what we have in W. So yes, we could fold b into W to simplify the notation, but then as we're taking derivatives we want to keep everything separate and clear.
8:09 And you'll usually back propagate through these activations, whereas you don't back propagate through b. So for this math, it's better to keep them separate. Yeah. So U transpose is our last, the question is what's U transposed? And U transpose is our last layer, if you will. But there's no non-linearity with it, and it's just a single vector. And so because by default here in the notation of the class we assume these are column vectors.
8:40 We transpose it so that we have a simple inner product. So it's just another set of parameters that will score the final activations to be high if they're a named entity, if there's a named entity of the center of this window. And scored low if not. That's correct. It is just a score that we're trying to maximize and we compute that final score with this inner product. And so these activations are now something that we compute in this pretty complex neural network function, yeah.
9:20 Here everything is a column vector, that's correct. All the x's are column vectors. So the question is, is there a particular reason of why we chose a linear layer as the last layer? And the answer here is to simplify the math a little bit. And because and to introduce to you another kind of objective function, not everything has to be normalized and basically summed to 1 as probabilities.
9:54 If you just care about finding one thing versus a lot of other things, like I just want to find named entities that are locations as center words. And if it's high, then that's likely one. And if it's low, then it's likely not a center location. Then that's all you need to do. And in some sense, you could add here a sigmoid after this, and then call it a probability, and then use standard cross entropy loss to, in your model.
10:20 There's no reason of why you shouldn't do it. That's something you have to do in the problem sets, trying to combine, we derive that, we help you derive the softmax and cross entropy pair optimization. And then we going through this and hopefully you can combine the two and you'll see how both work. But it's essentially a modeling decision and it's not wrong to apply a sigmoid here. And then call this a probability, instead of a score.
10:49 All right, so now, we have this two layer neural network, and we essentially did most of the work already to derive the final things here. We already knew how to derive our U gradients. And what used to be just W is not W superscript 2, but just because we add the superscript all the math is the same. So here, same derivation that we did for W, it's just now sitting on a2 instead of on just a.
11:20 And so what we did here, basically, follows directly to what we now have. It's the same thing, but we now have to be careful to add these superscripts depending on where we are in the neural network. And we'll have the same definition here when we multiply Ui and f prime of zi superscript 3, we'll just call that delta superscript 3 and subscript i, for the ith element. And this is going to give us the partial derivative with respect to Wij, the i jth element of the W matrix.
11:53 So this one we've already derived in all its glory. I'm just putting here again with the right superscripts. Now, the total function that we have is this one. And, again, we have here this same derivative, I just copied it over. And in matrix notation, we have to find this as the outer product here. That would give us the cross product, all the pairs of i and j to have the full gradient of the W2 matrix.
12:25 So this one was exactly as before, except that we now add the superscript a2 here. Now, in terms of the notation, we defined this delta i in terms of all these elements. And these are basically, if you think about it, two vectors. This Ui we could write as the full U. All the elements of the u vector. And f prime of zi we could write as f prime of z3 where we basically drop the index and assume this is just one vector of a bunch of element wise applications of this gradient function of this derivative here.
12:57 So we'll introduce now this notation which will come in very handy. And we call the Hadamard product or element-wise product. Sometimes you'll see it as little circles. Sometimes it's a circle with a cross or with a dot inside. Whenever you see these in backprop derivatives, it's usually means the same thing. Which we just element-wise multiply all the elements of the two vectors with one another. So this is how we'll define from now on this delta, the air signal that's coming in at this layer.
13:28 So the last missing piece for back propagation and to understand it is essentially the gradient with respect to W1, the second layer now, that we're moving through. Any questions around the Hadamard product, the outer product from the W? Yeah?
13:59 It is no longer what? Sorry it's. Associated, so yes. So the question is once you use the Hadamard product, how is this related to the matrix multiplication here or the vector, outer product? And so you basically first have to compute this. And then you have the full delta definition. And then you can multiply these and outer product to get the gradient.
14:30 Yeah. Sure, so the question is, could you assume that these are diagonal matrices? And yes, it's this kind of the same thing. But in terms of the multiplication you have to then make sure your diagonal matrix is efficiently implemented when it's multiplied with another vector. And as you write this out, if this is confusing, write out what it means to have a matrix times this.
15:02 And what if this is just a diagonal matrix? And what do you get versus just multiplying each of these elements with one another? So just write out the definitions of the matrix product and then you'll observe that you could think of it this way. But then really this f prime here, is just as a single vector why apply the derivative function to a bunch of zeros, in this case, zero, two so. All right, so the last missing piece, W1, the gradient for it.
15:37 And so the main thing we have to figure out now is what's the bottom layer's error message delta 2 that's coming in? And I'm not going to go through all the indices again. It would take a while and it's kind of repetitive. And it's very, very similar to what we've done in the last lecture. But essentially, we had already arrived at this expression. As the next lower update. And in our previous model, we would just arrive, that would be the signal that arrives at the word vectors.
16:07 So our final word vector update was defined this way. And what we now basically have to do is once more, just apply the chain rule because instead of having coming up at the word vectors. Instead, we're actually coming up at another layer. So basically, you can kind of call it a local gradient also, but it's when you multiply whatever error signal comes from the top layer, you multiply that with your local error signal, in this case, f prime here.
16:40 Then together, you'll get the update for either the weights that are at that layer, or the intermediate term for the gradient for lower layers. So that's what we mean by our signal. And it might help in the next definition, it might give you a better explanation of this in backprop number two. All right, so almost there. Basically, we apply the chain rule again. And if the chain rule for such a complex function is maybe less intuitive, so one thing that helped me many years ago, is to essentially assume all of these are scalars, just single variable.
17:18 And then derive all this, assuming it just, U, W, and x are all just single numbers. And then derive it, that will help you gain some intuition. And then you'll observe in the end that the final delta 2 is essentially similar to what we had derived in a very detailed way, which is W2 transposed times delta 3, and then Hadamard product times f prime of Z2 which is that layer here.
17:49 And this is basically it, if you understand these two equations, and you feel you can derive them now, then you will know all the updates for all standard multilayer neural networks. You will, in the end, always arrive at these two equations. And that is, if you wanna compute the error signal that's coming into a new layer, then you'll have some form of W, of the high layer transposed times the error signal that's coming in there.
18:21 Hadamard product with element y's derivatives here of f in the f prime. And in the final update for each W, will always be this outer product of delta error signal times the activation at that layer. And here, I include also our standard regularization term. And you can even describe the top and bottom layers this way.
18:51 And then lead to word vectors and the linear layer, but they just have a very simple delta. All right, now, for some of you, just like all right, now I understand everything, and it's great that I fully understand back propagation. But judging from Piazza and just from previous years, it's also quite a lot to wrap your head around. And so I will go through three additional explanations now, of this exact same algorithm. And there, we're going through much simpler functions not full neural networks, but much simpler kinds of functions.
19:27 But maybe for some, it will help to wrap their heads around sort of the general idea of these error signals through these simpler kinds of functions. So instead of having a crazy neural network with lots of matrices and hidden layers. We'll just kinda look at a simple function like this, and we'll arrive at a similar kind of idea. Namely, recursively applying and computing these error signals or local gradients as we move through a network. Now, the networks in this idea seen function as circuits are going to be much, much simpler.
20:02 And these are examples from another lecture on Git learning for convolutional neural networks and computer vision, and we're basically copying here some of their slides. And so let's take, for example, this very simple function, f of three variables. And this simple function is just x plus y times z. And let's assume we start with some random initial values for x, y, and z from which we start and wanna compute derivatives.
20:37 Now, just as before with a complex neural network, we can define intermediate terms but now, the intermediate terms are very, very simple. So we'll just take q, for instance, and we define q as x plus y, this local computation. And now, we can look at the partial derivatives here of q with respect to x and with respect to y. They're very simple, it's just addition, right, just one. And we can also define f now, in terms of q times z, where we use our intermediately defined function here.
21:12 And here, we're kind of simplifying q, it should be q is a function of x and y, but we just drop that. And we can also define our partials of f, our overall function with respect to q. Now, again, to connect that to what we looked at before. F could be our lost function, x, y, z could be parameters of this and we wanna, for instance, minimize our lost function. So now, what we want is, we want the final updates to update these variables.
21:44 So we'll start with at the very top. Just a df by df which is just 1, so it's not much there. We usually start with that. And now, we want to update and learn how do we update our z vectors? So we look at dfdz, and what is that? Well, we wrote down here all our different derivatives, so df by dz is just q.
22:16 And we define q as x + y, and x and y is minus 2 and 5. And so the gradient or the partial derivative here is just 3. All right, so far we're just very simple q times derivative z, that's it. All right, now, we can move also through this circuit. And are there questions around just the description of this circuit, of this function in terms of the circuit?
22:51 All right, so now, let's look at the dfdq which is the element here of the circuit, this node in the circuit description of this function. Now, the dfdq is again, quite simple we already wrote it right here. It's just z, and z is just minus 4. But now, the chain rule, we have to multiply and this is essentially a delta kind of error message.
23:22 We multiply what we have from the higher node in the circuit, but that's in this case, is just 1. And so the overall is just z times 1, and z is minus 4, z is minus 4. And now, we're going to move through this plus node to compute the next lower derivatives here. And this is, we end up at the final nodes here, the final leaf nodes if you will of this tree structure, and we wanna compute the dfdy.
23:54 Now dfdy, We basically wanna use the chain rule, and we're going to multiply what we have in the previous one, dfdq, which is the error signal coming from here times dqdy, which is the local error, the local gradient, it's not really the full gradient right, this is the local part of the gradient dqdy. So we multiply these two terms, the dfdq we wrote down here, that says z minus 4, times dqdy, as you wrote down here.
24:28 It's just one, so minus 4 times 1, we got minus 4. And we can do the same thing for x, again, apply the chain rule. All right, so in general, in this way of seeing all these functions as circuits, we basically always have some kind of input so each node in the circuit and we compute some kind of output. And what's important is we can compute our local gradients here directly during the forward propagation.
24:59 We don't need to know this local part of the gradient. We don't need to know what's up before. But in general, we will run this forward. We'll around some of these values. And then in back propagation, we get the gradient signals from any element upstream from each of these nodes in the circuits. And essentially then, use the chain rule and multiply all of these to compute the updates. All right, any questions around the definition of the circuits for simple functions?
25:33 It's very hard to take this kind of abstraction, and then get all the way to this full update. Therefore, a full near layer neural network, but it's very good to gain intuition of what's really going on, on a high level. >> All right, so now, let's go through a little more complex example of what this looks like. And I think of at the end of that you kind of gain some good intuition of how we basically do forward propagation, and recursively call these kinds of circuits to compute the full update.
26:15 So here, we have a little bit more of a complex function namely actually our sigmoid function that we had before. Usually when we have our sigmoid function, this was one activation of one hidden layer. In most cases, x was our input and w were the weights. So we defined this already, and now, let's assume we just want to compute the partial derivatives with respect to all the elements, w and x. And let's assume x and w are just, x is two-dimensional, and w is three-dimensional.
26:49 And we have here the bias term as just an extra element of w. So now, if you take this whole function, we're gonna now compute or define this as a circuit. That one description that's the most detailed description of this function as a circuit would look like this, where you basically recursively divide this function into all the separate actions that you might take. And you can compute gradients and the local gradients at each note in this kind of circuit.
27:21 So the last operation to compute the final output f here of this function, is 1 over whatever is in here. And so that's our last element of the circuit, and from the bottom it starts with multiplying these two numbers, multiplying these two numbers, and then adding to their summation this w2 install, all right? Are there any questions around the description of the circuit?
27:54 All right, so now, let's assume we start with these simple numbers here, so w2, w0 starts at 2, x0 starts at minus 1, minus 3, minus 2, and minus 3 here. So we just move forward through the circuit to compute our forward propagation, right? So this is a relatively simple concatenation of functions. And now, we wanna compute all our partial derivatives with respect to all these different elements here.
28:26 So we'll now go backwards and recursively backwards through this circuit, and apply the chain rule every time. So let's start the final value to the forward propagation numbers here in green, at the top the final value of this is 0.73. And again, the first delta derivative of just the function with itself, is just 1. And now, we hit this node in a circuit, and we want to now compute the derivative of this function, and the function's 1 over x.
28:59 And so the derivative is just minus 1 over x squared. x is 1.73, and so we basically compute minus 1 divided by 1.37, sorry, 1.37 squared. And then we multiply using a chain rule, the gradient signal here from the top that goes into this node. So now, you multiple these two, and you get the number minus 0.53.
29:35 Now, we're moved to the next node, so this node here, we just sum up a constant with the value x, and so the derivative of that is just 1. So we multiply, use the chain rule, multiply these two elements, the error signal or gradient signal from the top as it moves through this element of the circuit, which is just minus 0.3 times 1 so we get again minus 0.53, sorry.
30:12 Now, we move through the exponent. It's a little more interesting. So here, derivative of e to the x is just e to the x. And we have the incoming value which is minus 1, so that's our x. So we have e to the minus 1 times minus 0.53, the gradient signal from the higher node in this circuit. And we basically continue like this for a while, and compute the same for plus, similar to this plus and so on.
30:47 And that the end, we arrive right here. And our error signal is 0.2, and we have this multiplication here. And we know in multiplication, the partial of w0 times x0 partial with respect to x0 is just w0. And so we multiply 0.2 times the value here which is 2 and we get 0.4. And now, we have an update for this parameter after we've moved recursively through the circuit all the way to where it was used.
31:27 And this is essentially the same thing that we've done for the very complex neural network, but sort of one step at a time for a very simple function. Any questions around this sort of circuit description of the same back propagation at year. Namely reusing the derivatives, multiplying local error signals with the global error signals from higher Layers, where here, the layer definition's a bit stretched, it's very, very simple kinds of operations.
32:06 Yeah? That's right, so here, each time the sort of gradient, the local gradient times the global or above higher layer gradient signal. When you multiply them, you get an actual gradient. So they're not really gradients, right, they're sort of intermediate values of a gradient. Yep.
32:53 So the question is we're using this kind of circuit interpretation to compute derivatives and that's correct. If you were to just do standard math on this equation you would end up with something that looks exactly like this. And you would also have similar kinds of numbers. But we're making it a little more complicated, in some ways, to compute the derivatives here, of each of the elements of this function. We're kind of push the chain rule to its maximum by defining every single operation as a separate function.
33:32 And then computing gradients at every single separate function. And when you do that, even for this kind of simple function, you usually wouldn't write out this complex thing and take a derivative with respect to this node, which is just plus, cuz we all know how to do that. And usually we just move through this very quickly but the circuit definitions can help you understand the idea that at each node what you end up getting is the local gradient times the gradient signal from the top.
34:01 So in the end you get the exact same updates as if you had just taken the derivatives using the chain rule like this. And in fact, the definition of the circuit can be arbitrary too and sometimes it's a lot more work to write out all the different sub components of a function. So for instance, we know if we just described sigma of x as our sigmoid function, we could kind of combine all these different elements of the circuit as just one node in the circuit.
34:35 And we know, with this one little trick here, the derivative of sigma x with respect to x can actually be described in terms of sigma x. So we don't need to do any extra computation like we did internally here, take another exponent and so on. We actually can just know, well if that was our value here of sigma x then the derivative that will come out here is just 1- sigma x times sigma x. And so we could, in theory, also define our circuit differently, and in fact the circuits we eventually define are this whole thing is one neural network layer.
35:09 And internally we know exactly the kinds of messages that pass through such a layer, or the error signals, or again, elements of the final gradients. Yeah? That's a good question, sorry, yes. So the question is, we're talking about back propagation here, and what is forward propagation? Yeah, forward propagation just means computing the value of your overall function. The relationship between the two is forward propagation is what you compute, what you do at test time, to compute the final output of your function.
35:46 So, you want the probability for this node to be a location, or for this word to be a location, you'd do forward propagation to compute that probability. And the you do backward propagation to compute the gradients if you wanna train and update your model if you have a training data set and so on. That's right, the red numbers here at the bottom are all the partial derivatives with respect to each of these parameters. And here all the intermediate values that we use as that gradient flows through the circuit to the parameters that we might wanna update, great question.
36:30 All right, so essentially we recursively applied the chain rule as we moved through this graph. And we end up with a similar kind of intuition, as we did with the same, with just using math and multivariate calculus, to arrive at these final gradients, to update our parameters. All right, any questions around the circuit? Interpretation of back propagation, yeah.
37:03 So here w2 is our bias term, it doesn't depend on the values of x, we just add it, and w2 down here in the circuit. So that is the last element we add after adding these two multiplications. All right, so, now if that was too simple and you wanna get a little bit high level again, you can essentially think of these circuits also as flow graphs.
37:36 And circuit is the terminology that Andrej Karpathy used in 231 and Yoshua Bengio, for instance, another very famous researcher in deep learning uses the terminology of flow graphs, but, again, we have the very similar kind of idea. You start with some input x, you do forward propagation to compute some kind of value. You go through some intermediate variables y, and then, in back propagation, you compute your gradients going backwards in the reverse order to what you've done during forward propagation.
38:13 And so this is if you just have one intermediate value now if x, and this is something else important to know it for the circuits it's the same, if x modifies two paths in your flow graph you end up, based on the multiple variable Chain Rule. You have to sum up the local air signals for both from both of the paths. And in general, again you move backwards through them. So usually as long as you have some kind of directed basically graph or tree structure, you can always compute these flows and these elements of your gradient.
38:55 And in general, if x goes through multiple different elements in your flow graph, you just sum up all the partials this way. And so this is another interpretation much more high level without defining exactly what kinds of computation you have here at each node. But in general you can define these kind of flow graphs and each node is some kind of computational result. And each arc here is some kind of dependency, so you need, in order to compute this, you needed this.
39:26 And you can define more complex things where you have so called short circuit connections, we'll define those much later in the class, but in general, you move forward through your node. So this is a more realistic example where we may have some input x, we have some probability, or sorry some class y for our train data set. And in forward propagation, we'll move these through a sigmoid neural network layer here such as h is just sigma of Vx.
39:54 We dropped here The bias term. And so, you can also describe your v as part of this flow graph. You move through a next layer, and then you may have a softmax layer here, similar to the one that you derived in problem set one. And then you have your negative log likelihood, and you compute that final cost function for this pair xy, for this training element. And then back propagation again, you move backwards through the flow graph.
40:21 And you update your parameters as you move through the flow graph. Now, before I go through the last and final explanation, the good news is you won't actually have to do that for very complex neural networks. It would be close to impossible for the kinds of large complex neural networks to do this by hand. Many years ago, when I had started my PhD, there weren't any software packages with automatic differentiation.
40:55 So you did have to do that. And it slowed us down a little bit. But, nowadays, you can essentially automatically infer your back propagation updates based on the forward propagation. It's a completely deterministic process, and so can use symbolic expressions for your forward prop. And then have algorithms automatically determine your gradient, right? The gradients always exist for these kinds of functions. And so that will allow us to much faster prototyping. And you'll get introduced next week to a tensor flow, which is one such package that essentially takes all these headaches away from you.
41:34 But with this knowledge, you'll actually know what's going on under the hood of these packages. All right, any question around the flow graph interpretation of back propagation? Yes? It's actually in closed form. Yeah, it's not numerically solved. So sorry, the question was, the automatic differentiation, is it numeric or symbolic? It's usually symbolic. All right, now, for the last and final explanation of the same idea.
42:08 But combining the idea of the flow graph with the math that you've seen before, and hopefully that will help. So, let's bring back this complex two layer neural network. Now, how can we describe this at a much simplified kind of flow graph or circuit where we can combine in a lot of different elements instead of writing every multiplication, summation, exponent, negation, and so and out? This is the kind of flow graph that kind of yeah, kind of combines these two worlds.
42:42 So we assumed here we had our delta error signal coming from the simple score that we have. And let's say that our final, we want all the updates, essentially, to W(2) and W(1). Now W(2), as we move through this linear score, the delta doesn't change. And so the update that we get for W(2) here is just this outer product again. And that's kind of, as we move through this very high level flow graph, we basically now update W(2) once we get the error message from the layer above.
43:24 Now, as we move through W(2), this kind of circuit will essentially just multiply the affine, like as we move through this simple affine transformation this matrix vector product, we're just required to transpose the forward propagation matrix. And we arrived why this is before, but this is kind of the interpretation of this flow graph in terms of a complex and large realistic neural network.
43:56 And so notice also that the dimensions here line up perfectly. So the output here, we multiply this delta that has the dimensionality of the output. With the transpose, we get exactly the dimensionality of the input of this W. So it's quite intuitive, right? You have the linear transformation, affine transformation through this W as you move backwards to this W, you just multiply it with its transpose. And now, we are hitting this element wise nonlinearity.
44:29 And so as we update the next delta, we essentially have also an element wise derivative here of each of the elements of this activation. So as we're moving our error vector, error signal, or global parts of the gradient through these point-wise nonlinearities, we need to apply point-wise multiplications with the local gradients of the non-linearity.
45:01 And now we have this delta that's arrived at W(1). And so W1 we can now compute the final gradient with respect to W(1) as just the delta again times the activation of the previous layer, which is a(1) and we have this outer product. So this is combining the different interpretations that we've learned. We arrived through this through just multivariate calculus. And now this is the flow graph or circuit interpretation of what's going on.
45:32 Yes? >> If I mean point-wise non linearity, I mean coordinate wise, yes, they are the same. So, whenever we write f(z) here, and z was a vector of z1, z2, for instance, then we meant f(z1) and f(z2). And the same is true if we write it like this. And look at the partial derivatives.
46:05 Yeah? >> I know. >> That's just, from matrix. It is, yes, so the question is the delta here the same as in the definition of the two layer neural network? And it is, yeah.
46:35 So this delta here is this and you notice here that it's the same thing that we wrote before. We have W(2) transpose times delta(3). And then you have the Hadamard product with the element-wise derivatives here. All right, congratulations! You've done it. So now, understand the inner workings of most deep learning models out there.
47:05 And this was literally the hardest part of the class. I think it's gonna go all uphill from here for many of you. And everything from now on is really just more matrix multiplications and this kind of back propagation. It's really 90% of the state of the art models out there right now and top new papers that are coming out this year. You now can have a warm, fuzzy feeling, as you look through the forward propagation definitions.
47:30 All right, with that, let's have a little intermission and look at a paper. Take it away. >> Hi everyone. So yeah, so let's take a break from neural networks, and let's talk about this paper which came out from Facebook ARV search just this past summer. So text classification is a really important topic in NLP. Given a piece of text, we may wanna say, is this a positive sentiment or does it have negative sentiment? Is this spam or ham, or did JK Rowling actually write this?
48:03 And so this one's particular from a website and it's basing an example of sentiment analysis. And so if you recall from your problem set in problem four. An easy way to featurize a sentence is to just average out all the word vectors in a sentence. And that's basically what the model from this paper does. And so they use really low dimensional word vectors. Take the average of them, kind of you know you lose the ordering of it and then you get this low dimensional text vector which represents the sentence.
48:39 In order to kind of get some of the ordering back, they also use n-grams. And so now that we have the text vector that's kind of like in the hidden layer. We then feed it through a linear classifier which uses softmax compute the probability over all the predictive classes. The hidden representation is also shared by all the classifiers for all the different categories. Which helps the classifier use information about words learned from one category for another category.
49:09 And so will look a little bit more familiar to you now that you guys have gone through all the costs and whatnot. So we minimize the negative flaws likelihood over all the classes, and the model's trying to using stochastic gradient descent and a linear decaying learning rate. Another thing that makes it really fast is the use of the hierarchical softmax. And so by using this, the classes are organized in like this tree kind of fashion instead of just like in a list.
49:34 And so this also helps with the timing, so we go from linear time to logarithmic time. Because also the costs are organized in terms of how frequent they are. So in case, we have maybe like a lot of class, but less of one class. This helps kind of balance that out so NLP is really hot right now. So in here the depth is much smaller, so we can access that cost a lot faster. But maybe for some less popular topics, I just made some up here, that's not actually my opinion.
50:04 But they have a much deeper depth because they are much more infrequent. And so especially in this day and age when we're really crazy about neural networks, the question is like how well does this stack up against them? Because it uses a linear classifier, it doesn't really have all those layers for neural network. And as it turns out, this actually performs really well. It's not only really fast, but it performs just as well if not sometimes better than neural networks which is pretty crazy.
50:31 And so just a quick summary. FastText, which is what they call their model is often on par with deep learning classifiers. It takes seconds to train, instead of days, thanks to their use of low dimensional word vectors in the hierarchical softmax. And another side bit, is that it can also learn vector representations of words in different languages, with performs even better than word2vec. Thank you. >> >> All right, and you know what's awesome? Like this kind of equation you could totally derive all the gradients now too.
51:02 >> >> Just another day in the office. All right, so class project. This is for many, the most lasting and fun part of the class. But some people also don't have a research agenda or some kind of interesting data set, so you don't have to do the project. If you do a project, we want you to have a mandatory mentor.
51:33 The mentors that are pre-approved are all the PhD students, and Chris and me. So we wanna really give you good advice and we want you to meet your mentors frequently. So think I'll have 25, Chris has 25, and then I guess each of the PhD TAs also has at most 25 groups. It's a very large class. But yeah, so basically your class projects, if you do decide to do it, is 30% of your final grade.
52:04 And sometimes real paper submissions come out from these. It's really exciting, you get to travel. You get probably paid, depending on who you're working with. If you're a grad student and you write a paper, to go to some fun places in the world. And something that's really helpful for people's careers. Sometimes these papers, people get contacted from various companies once we put these papers up. If you do a really good job, it can have really lasting impact on the kinda work that you do.
52:35 So on the choice of doing assignment four, the final project. We don't wanna force you to do the final project, cuz some people just wanna learn the concepts and then move on with life. And it can be a little painful to try to come up with something. So there is a final project, and we will ask you to sort of define your project with your mentor. And then we might encourage you or discourage you from moving forward with that project.
53:00 Some projects might be too large in scope or too small in scope, and so on. And so do check with the TAs of whether the project is the right thing for you. If you do a project, and if you decide to do it you really have to start early. Ideally you will start meeting me today, or latest like next week or two weeks and or the other TAs. We write out a lot of the sort of organizational things on the website.
53:32 So let's look at the website really quick. It's now linked from our main page. So you can get a couple of different ideas from these top conferences. So one project idea and we'll go into that a little bit later, is to take one of these newest papers from the various groups or various conferences and just try to replicate the results. You will notice that despite having in theory, everything written in the paper, if it's a nontrivial model there's a lot of subtle detail.
54:01 And it's hard to squeeze all of those details in eight pages. So usually the maximum page them in so replicating sometimes, this paper is sufficient enough for most papers in most projects. So here, here's some very concrete papers that you can look at and to get adheres from others. And what's kind of interesting and new these days, this is by no means and exclusive list, there a lot more other interesting papers.
54:35 So again here there sort of pre proofed mentors for projects. You'll have to contact us through office hours. And if you do a project in your project proposal, you have to write out who the mentor is. A lot of other mentors, we'll actually list probably next week now. A list of potential projects that are coming from people who spend all their time thinking about deep learning and NLP. So if you don't have an idea, but you really do wanna do some interesting novel research project, we'll post that link internally.
55:09 So that not the whole world sees it, but only the students in this class. Cuz sometimes, the PhD students have some interesting novel idea. They don't want it to get scooped and have some other researchers do that idea, but they do wanna collaborate with students and youths. So we'll keep those ideas under wraps here. So yeah, this is your project proposal. You have to define all these things, and we'll go through that now in some of the details here.
55:36 And then you have a final submission, you have to write a report. And then we'll also have a poster presentation, where all the projects are basically being described. You'll have to print a little poster, and we'll walk around. It's usually quite fun. Maybe we'll even come up with a prize for best poster, and best paper, and so on. All right, so these are the organizational, Tips. Posters and projects by the way I have maximum of three people.
56:04 If you have some insanely, well thought out plan, we may make an exception and go to four. But the standard default is three. So the exception kind of has to be mailed to the TAs or Aston Piazza. Any questions around the organizational aspects of the project? Groups. You can do groups of one, two, or three. So it doesn't have to be three. The bigger your group, the more we expect from the project. And you have to also write out exactly what each person in the project has done.
56:38 You can actually use any kind of open source library and code that you want. It's just a realistic research project. But if you just take Kaldi, which is a speech recognition system, and you say I did speech recognition. And then really all you did was download the package and run it, then that's not very impressive. So the more you use, the more you also have to be careful and say exactly what parts you actually implemented.
57:11 And in the code, you also have to submit your code, so that we understand what you've done and the results are real. So this year we do want some language in there. Some natural human language. Last year I was a little more open. It could be the language of music and so on now.
57:41 But this year it's. So we've got to have some natural language in there, yeah. But other than that, that can be done quite easily so we'll go through the types of projects you might want to do. And if you have a more theoretically inclined project where you really are just faking out some clever way of doing a sarcastic ready to sent or using different kinds of optimization functions. An optimizers that we'll talk about leading the class to then as long as you at least applied it in one experiment to a natural language processing data set that would still be a pretty cool project.
58:18 So you can also apply it to genomics data and to text data if you wanna have a little bit of that flavor. But there is gonna be at least one experiment where you apply it to a text data set. All right, so now let's walk through the different kinds of projects that you might wanna consider, and what might be entailed in such project to give you an idea. Unless there are any other questions around the organization of the projects, deadlines and so on.
58:51 So, let's start with the kind of simplest and all the other ones are sort of bonuses on top of that simple kind of project. And this is actually, I think generally, good advice, not just for a class project, but in general, how to apply a deep learning algorithm to any kind of problem, whether in academia or in industry, or elsewhere. So, let's assume you want to apply an existing neural network to an existing task.
59:22 So in our case, for instance, let's take summarization. So you want to be able to take a long document and summarize into a short paragraph. Let's say that was your goal. Now step one, after you define your task, is you have to define your dataset. And that is actually, sadly, in many cases in both industry and in academia, an incredibly time intensive problem. And so, the simplest solution to that is you just search for an existing academic dataset.
59:54 There's some people who've worked in summarization before. The nice thing is if you use an existing data set, for instance, from the Document Understanding Conference, DUC here, then other people have already applied some algorithms to it, you'll have some base lines, you know what kind of metric or evaluation is reasonable versus close to random. And so on, cuz sometimes that's not always obvious. We don't always us just accuracy for instance. So in that case, using an existing academic data set gets rid of a lot of complexity.
60:28 However, it is really fun if you actually come up with your own kind of dataset too. So maybe you're really excited about food, and you want to prowl Yelp, or use a Yelp dataset for restaurant review, or something like that. So, however, when you do decide to do that, you definitely have to check in with your mentor, or with Chris and me, and others. Because I sadly have seen several projects in the last couple of years where people have this amazing idea.
60:58 I'm excited, they're excited. And then they spent 80% of the time on their project on a web crawler getting not blocked from IP addresses, writing multiple IP addresses, having multiple machines, and crawling. And so on, then they realize, all right, it's super noisy. Sometimes it's just the document they were hoping to get and crawl, it's just a 404 page. And now they've filtered that. And then they realize HTML, and they filter that. And before you know it, it's like, they have like three more days left to do any deep learning for NLP.
61:31 And so, it has happened before so don't fall into that trap. If you do decide to do that, check with us and try to, before the milestone deadline. For sure have the data set ready so you can actually do deep learning for NLP, cuz sadly we just can't give you a good grade for a deep learning for NLP class if you spend 95% of your time writing a web crawler and explaining your data set. So in this case, for instance, you might say all right, I want to use Wikipedia.
61:59 Wikipedia slightly easier to crawl. You can actually download sort of already pre-crawled versions of it. Maybe you want to say my intro paragraph is the summary of the whole rest of the article. Not completely crazy to make that assumption, but really you can be creative in this part. You can try to connect it to your own research or your own job if your a student, or just any kind of interest that you have. Song lyrics come up from time to time it's really fun NLP combine with language of music with natural language and so on.
62:32 So you can be creative here, and we kind of value a little bit of the creativity this is like a task of data set we had never seen before and you actually gain some interesting Linguistic insights or something. That is the cool part of the project, right. Any questions around defining a data set? All right, so then you wanna define your metric. This is also super important.
63:02 For instance, you have maybe have crawled your dataset and let's say you did something simpler like restaurant star rating classification. This is a review and I want to classify if this a four star review or a one star review or a two or three. And now you may have a class distribution where this is one star, this is two stars, three and four, and now the majority are three. Maybe that you troll kind of funny and so really most of the reviews are three star reviews.
63:37 So this is just like number of reviews per star category. And maybe 90% of the things you called are in the third class. And then you write your report, you're super excited, it was a new data set, you did well, you crawled it quickly. And then all you give us is an accuracy metric, so accuracy is total correct divided by total. And now, let's say your accuracy is 90%. It's 90% accurate, 90% of the cases gives you the ride star rating.
64:08 Sadly, it just always gives three. It never gives any other result. You're essentially overfit to your dataset and your evaluation metric was completely bogus. It's hard to know whether they basically could have implemented a one line algorithm that's just as accurate as yours which is just, no matter what the input, return three. So hard to give a good grade on that and it's a very tricky trap to fall into. I see it all the time in industry and for young researchers and so on.
64:38 So in this case, you should've used, does anybody know what kind of metric you should've used? F1, that's right. So, and we'll go through some of these as we go through the class but it's very important to define your metric well. Now, for something as tricky as summarization, this isn't where you're really just like, this is the class, this is the final answer. You have to actually either extract or generate a longer sequence. And there are a lot of different kinds of metrics you can use.
65:07 BLEU's n-gram overlap or Rouge share which is a Recall-Oriented Understudy for Gisting Evaluation which essentially is just a metric to weigh differently how many n-grams are correctly overlapping between a human generated summary. For instance, your Wikipedia paragraph number one, and whatever output your algorithm gives. So, Rouge is the official metric for summarization in different sub-communities and NOP have their own metrics and it's important that you know what you're optimizing.
65:39 So, the machine translation, for instance, you might use BLEU scores, BLEU scores are essentially also a type of n-gram overlap metric. If you have a skewed data set, you wanna use F1. And in some cases, you can just use accuracy. And this is generally useful even if you're in industry and later in life, you always wanna know what metric you're optimizing. It's hard to do well if you don't know the metric that you're optimizing for, both in life and deep learning projects.
66:05 All right so, let's say you defined your metric now, you need to split your dataset. And it's also very important step and it's also something that you can easily make sort of honest mistakes. Again, in advantage of taking pre-existing academic dataset is that in many cases, it's already pre-split but not always. And you don't wanna look at your final test split until around 1 week before the deadline.
66:36 So, let's say you have downloaded a lot of different articles and now you basically have 100% of some articles you wanna summarize. And normal split would be take 80% for training, you take 10% for your validation and your development. So, oftentimes this is called the validation split, or the development split, or dev split, or various other terms. And 10% for your final test split. And so, the final one, you ideally get a sense of how your algorithm would work in real life, on data you've never seen before, you didn't try to chew on your model like, how many layers should I use, how wide should each layer be?
67:18 You'll try a lot of these things, we'll describe these in the future. But it's very important to correctly split and why do I make such a fuss about that? Well, there too, you might make mistakes. So let's say, you have unused text and let's say, you crawled it in such a way there's a lot of mistakes that you can make if you try to predict the soft market for instance, don't do that, it doesn't work. But in many cases, you might say, or there some temporal sequence.
67:51 And now, you basically have all your dataset and the perfect thing to do is actually do it like this, you take 80% of let's say, month January to May or something and then, your final test split is from November. That way you know there's no overlap. But maybe you made a mistake and you said well, I crawled it this way, but now I'm just randomly sample. So, as sample an article from here, and one from here, and one from here.
68:18 And then the random sample goes to the 80% of my training data. And now, the test data and the development data might actually have some overlap. Cuz if you're depending on how you chose your dataset maybe the another article which just like a slight addition, like some update to an emerging story. And now the summary is almost exact same but the input document just changed a tiny bit. And you have one article in your training set and another one in your test set.
68:50 But the test set article is really only one extra paragraph on an emerging story and the rest is exactly the same. So now you have an overlap of your training and your testing data. And so in general, if this is your training data and this should be your test data. It should be not overlapping at all. And whenever you do really well, you run your first experiment and you get 90 F1. And things look just too good to be true sadly in many cases they are and you made some mistake where maybe your test set had some overlap for instance, with your training data.
69:27 It's very important to be a little paranoid about that when your first couple of experiments turn out just to be too good to be true. That can mean either your training, your task is too simple, or you made a mistake in splitting and defining your dataset. All right, any questions around defining a metric or your dataset, yeah?
70:00 So, if we split it temporally, wouldn't we learn a different distribution? That is correct, we would learn a different distribution, these are non-stationary. And that is kinda true for a lot of texts, but if you, ideally, when you built a deep learning system for an LP you want it to built it so that it's robust. It's robust to sum such changes over time. And you wanna make sure that when you run it in a real world setting, on something you've never seen before, you've shipped your software, it's doing something, it will still work.
70:32 And this was the most realistic way to capture how well it would work in real life. Would it be appropriate to run both experiments as in both where you subsample randomly, and then you subsample temporally for your? You could do that, and the intuitive thing that is likely going to happen is if you sample randomly from all over the place, then you will probably do better than if you have this sort of more strict kind of split.
71:05 But running an additional experiment will rarely ever get you points subtracted. You can always run more experiments, and we're trying really hard to help you get computing infrastructure and Cloud compute. So you don't feel restricted with the number of experiments you run. All right, now, number 5, establish a baseline. So, you basically wanna implement the simplest model first.
71:37 This could just be a very simple logistic regression on unigrams or bigrams. Then, compute your metrics on your train data and your development data, so you understand whether you're overfitting or underfitting. If, for instance, you're training Metric. Let's say your loss is very, very low on training. You do very well on training, but you don't do very well on testing, then you're in an over fitting regime. If you do very well on training and well on testing, you're done, you're happy.
72:07 But if your training loss can't be lower, so you're not even doing well on your training, that often means your model is not powerful enough. So it's very important to compute both the metrics on your training and your development split. And then, and this is something we value a lot in this class too. And it's something very important for you in both research and industries like you wanna analyze your errors carefully for that baseline. And if the metrics are amazing and there are no errors, you're done,.
72:36 Probably a problem was too easy and you may wanna restart unless it's really a valuable problem for the world. And then maybe you can just really describe it carefully and you're done too. All right, now, any questions around establishing your baseline? It is very important to not just go in and add lots of bells and whistles that you'll learn about in the next couple of weeks in this class and create this monster of a model. You want to start with something simple, sanity check, make sure you didn't make mistakes in splitting your data.
73:04 You have the right kind of metric. And in many cases, it's a good indicator for how successful your final project is if you can get this baseline In the first half of the quarter. Cuz that means you figured out a lot of these potential issues here. And you kind of have your right data set. You know what the metric is, you know what you're optimizing, and everything is good. So try to get to this point as quickly as possible.
73:30 Cuz that is also not as interesting, and you can't really use that much knowledge from the class. Now then it gets more interesting. And now you can implement some existing neural network model that we taught you in class. For instance, this Window-based model if your task is named entity recognition. You can compute your metric again on your train AND dev set. Hopefully you'll see some interesting patterns such as usually train neural nets is quite easy in a sense that we lower the loss very well.
74:02 And then we might not generalize as well in the development set. And then you'll play around with regularization techniques. And don't worry if some of the stuff I'm saying now is kind of confusing. If you want to do this, we'll walk you through that as we're mentoring you through the project. And that's why each project has to have an assigned mentor that we trust. All right, then you analyze your output and your errors again. Very important, be close to your data.
74:27 You can't give too many examples usually ever. And this is kind of the minimum bar for this class. So if you've done this well and there's an interesting dataset, then your project is kind of in a safe haven. Now again it's very important to be close to your data. Once you have a metric and everything looks good, we still want you to visualize the kind of data, even if it's a known data set. We wanted you to visualize it, collect summary statistics.
74:54 It's always good to know the distribution if you have different kinds of classes. You want to, again very important, look at the errors that your model is making. Cuz that can also give you intuitions of what kinds of patterns can your deep learning algorithm not capture. Maybe you need to add a memory component or maybe you need to have longer temporal kind of dependencies and so on. Those things you can only figure out if you're close to your data and you look at the errors that your baseline models are making.
75:23 And then we want you to analyze also different hyperparameters. A lot of these models have lots of choices. Did we add the sigmoid to that score or not? Is the second layer 100 dimensional or 200 dimensional? Should we use 50 dimensional word vectors or 1,000 dimensional word vectors? There are a lot of choices that you make. And it's really good in your first couple projects to try more and gain that intuition yourself. And sometimes, if you're running out of time, and only so much, so many experiments you can run, we can help you, and use our intuition to guide you,.
75:54 But it's best if you do that a little bit yourself. And once you've done all of that, now you can try different model variants, and you'll soon see a lot of these kinds of options. We'll talk through all of them in the class. So now another kind of class project is you actually wanna implement a new fancy model. Those are the kinds of things that will put you into potentially writing an academic paper, peer review, and at a conference, and so on.
76:23 The tricky bit of that is you kinda have to do all the other steps that I just described first. And then, on top of that, you know the errors that you're making. And now you can gain some intuition of why the existing models are flawed. And you come up with your own new model. If you do that, you really wanna be in close contact with your mentor and some researchers, unless you're a researcher yourself, and you earned your PhD.
76:50 But even then, you should chat with us from the class. You want to basically try to set up an infrastructure such that you can iterate quickly. You're like, maybe I should add this new layer type to this part of my model. You want to be able to quickly iterate and see if that helps or not. So it's important and actually require a fair amount of software engineering skills to set up efficient experimental frameworks that allow you to collect results.
77:17 And again you want to start with simple models and then go to more and more complex ones. So for instance, in summarization you might start with something super simple like just average all your word vectors in the paragraph. And then do a greedy search of generating one word at a time. Or even greedily searching for just snippets from the existing article in Wikipedia and you're just copying certain snippets over. And then stretch goal is something more advanced would be lets you actually generate that whole summary.
77:48 And so here are a couple of project ideas. But, again, we'll post the whole list of them with potential mentors from the NOP group and the vision group and various other groups inside Stanford. Sentiment is also a fun data set. You can look at this URL here for one of the preexisting data sets that a lot of people have worked on. All right, so next week we'll look at some fun and fundamental linguistic tasks like syntactic parsing.
78:15 And then you'll learn TensorFlow and have some great tools under your belt. Thank you.