================================================================= TITLE: Deep learning 4 DATE: 2024-11-29 (November 29, 2024 at 12:42 PM) DURATION: 03:13:53 (11633 seconds) WORDS: 26,462 words SPEECH ID: 26XPVAWX6ZUBFBLP ================================================================= --- AI SUMMARY --- neural network, logistic regression, sigmoid function, hidden layer, output layer, activation functions, gradient descent, backpropagation, matrix multiplication, random initialization, tanh function, ReLU function, leaky ReLU, vectorization, training examples --- FULL TRANSCRIPT --- [119:17:20] 0: Before diving into the technical details, I want to in this video to give you a quick overview of what you'll be seeing in this week's videos. So if you don't follow all the details in this video, don't worry about it. We'll delve into technical details in the next few videos, but for now, let's give a quick overview of how you implement in your network. Last week, we had talked about logistic regression, and we saw how this model corresponds to the following computation graph, where you then put the features x in parameters w and b then allows you to compute z, which is only used to compute a, and we were using a interchangeably with this output, Y, hat, and then you can compute the loss function L. A neural network looks like this, and as I'd already previously alluded, you can form a neural network by stacking together a lot of little sigmoids. Whereas previously this node corresponds to two steps of calculations. The first is compute the z value. Second is to compute this a value. In this neural network, this stack of nodes will correspond to a z like calculation like this, as well as an A like calculation like that, and then that note will correspond to another Z and another a like calculation. So the notation which we should use later will look like this. First, we'll input the features x, together with some parameters w and b, and this will allow you to compute z1 so new notation that we'll introduce is that we'll use superscript square bracket one to refer to quantities associated with this stack of nodes. It's called a layer. And then later, we'll use superscript square bracket two to refer to quantities associated with that node. Really, that's called another layer of the neural network. And the superscript square brackets, like we have here, are not to be confused with the superscript round brackets, which we use to refer to individual train examples. So whereas X superscript round bracket, I refer to the I train example, superscript square bracket one and two refer to these different layers, layer one and layer two in this network. But so going on, after computing z1 similar to logistic regression, there'll be a computation to compute a one, and that's just sigmoid of z1 and then you compute z2 using another linear equation, and then compute a two. And a two is the final output of the neural network, and will also be used interchangeably with Y axis. So I know there was a lot of details, but the key intuition to take away is that, whereas for logistic regression, we had this Z followed by a calculation and this neural network here, we just do it multiple times as a Z followed by an A calculation, and Z followed by an A calculation, and then you finally compute the loss at the end. And you will remember that for logistic regression, we've had this backward calculation in order to compute derivatives, also computing da, Bz and so on. So in the same way, a neural network will end up doing a backward calculation that looks like this, in which you end up computing [1095:11:59] 0: D, A, 2d, c2, [1108:31:59] 0: the Nazis are compute d, [1122:40:00] 0: w2, d2, [1142:08:00] 0: and so on. In this sort of right to left backward calculation with degrade arrows. So that gives you a quick overview of what a neural network looks like. It's basically taking logistic regression and repeating it twice. I know there was a lot of new notation, laws, new details. Don't worry about it. We didn't follow everything. We'll go into the details more slowly in the next few videos. So let's go on to the next video. We'll start to talk about the neural network representation. [1315:22:40] 0: See we draw a few pictures of neural networks in this video, we'll talk about. Out exactly what those pictures means, in other words, exactly what those little neural networks we've been drawing represent. And we'll start with focusing on the case of neural networks with what's called a single hidden layer. Here's a picture of a neural net. Let's give different parts of these pictures some names we have the input features, x1 x2 x3 stacks up vertically, and this is called the input layer of the neural network. So maybe not surprisingly, this contains the inputs to the neural network. Then there's another layer of circles, and this is called a hidden layer of the neural network. I'll come back in a second to say what the word hidden means but the final layer here is formed by in this case, just one node, and this single node layer is called the output layer, and is responsible for generating the predicted value y. Hat in a neural network that you train with supervised learning, the training set contains values of the inputs x as well as the target outputs. Y. So the term hidden layer refers to the fact that in the training set, the true values for these nodes in the middle are not observed. That is, you don't see what they should be in the training set. You see what the inputs are. You see what the output should be for the things in a hidden layer are not seen in the training set. So that kind of explains the name hidden layer just means you don't see it in the training set. Let's introduce a bit more notation. Whereas previously we were using the vector x to denote the input features, an alternative notation for the values of the input features will be a super square vector zero, and the term a also stands for activations and it refers to the values that different layers of the neural network are passing on to the subsequent layers. So the input layer passes on the value x to the hidden layer. So we're going to call that called the activations of the input layer, a superscript zero the next layer, the hidden layer, will, in turn generate some set of activations which I'm going to write as a superscript square bracket one. So in particular, this first unit, or this first node, will generate the value a superscript square bracket one, subscript one. This second node will generate the value now with a subscript two, and so on. And so a superscript square bracket one. This is a four dimensional vector. Or if you want in Python, if this is four by one matrix or four column vector, which looks like this, and it's four dimensional because in this case, we have four nodes or four units, or four hidden units in this hidden layer, then finally, the output layer will generate some value of a two, which is just a real number, and so Y hat is going to take on the value of a two. So this is analogous to how in logistic regression we have Y hat equals a and in logistic regression we only had that one output layer. So we didn't use the superscript square brackets, but with a neural network, we're now going to use the superscript square bracket to explicitly indicate which layer it came from. One funny thing about notational conventions in neural networks is that this network you just see here is called a two layer neural network, and the reason is that when we count layers in neural networks, we don't count the input layer, so the hidden layer is layer one and the output layer is layer two. In our notational convention, we're calling the input layer layer zero. So technically, maybe there are three layers in this neural network, because there's the input layer, the hidden layer and the upper layer. But it's in conventional usage. If you read research papers on the nail screen, course, you see people refer to this particular neural network as a two layer neural network, because we don't count input layer as an official layer. Finally, something that we'll get to later is that the hidden layer and the output layers will have parameters associated with it. So the hidden layer will have associated with it parameters w and b, and going to write superscript square bracket one to indicate that these are parameters associated with layer one with the hidden layer. We'll see later that W will be a four by three matrix, and B will be a four by one vector in this example, where the first coordinate four comes from the fact that we have four nodes or four hidden units in the layer. And three comes from the fact that we have three input features. We'll talk later about the dimensions of these matrices, and it might make more sense at that time. But then similarly, the output layer as associated with it also parameters W, superscript square bracket two and B, superscript square bracket two. And it turns out the dimensions of these are one by four and one by one, and these one by four is because the hidden layer has four hidden units. The output layer has just one unit. We go over the dimension of these matrices and vectors in a later video, so you've just seen what a two layer neural network looks like. That is a neural network with one hidden layer in the next video. Let's go deeper into exactly what this neural network is computing. That is how this neural network inputs x and goes all the way to computing its output Y hat, [2748:31:59] 0: in the last video, you saw what a single hidden layer neural network looks like in this video. Let's go through the details of exactly how this neural network competes itself. What you see is that it's like logistic regression, but repeated a lot of times. Let's take a look. So this is what a two layer neural network looks let's go more deeply into exactly what this new network computes. Now we've said before that logistic regression, the circle in logistic regression, really represents two steps of computation. First, you compute z as follows, and then second, you compute the activation as a sigmoid function of z. So in your network just does this a lot more times. Let's start by focusing on just one of the nodes in the building there. Let's look at the first node in the building there. So grayed out the other nodes for now. So similar to logistic regression on the left, this node in the building there does two steps of computation. Right The First step, and think of as the left half of this node. It computes z equals W, transpose X plus B. And the notation we'll use is these are all quantities associated with the first hidden layer. So that's why we have a bunch of square brackets there. And this is the first note in the hidden layer. So that's why we'll have a subscript one over there. So first it does that, and then the second step is it computes a one, one equals sigmoid of z1, one like so. So for both Z and a, the notational convention is that a, l, I, D, L here in superscript square vectors refers to layer number, and the I subscript here refers to the nodes in that layer. So the note we've been looking at is layer one, that is the hidden layer nodes one. So that's why the superscript and subscripts were both one to one. So that little circle, that first node in the neural network, represents carrying out these two steps of computation. Now let's look at the second node in the neural network, or the second node in the hidden layer of the neural network, similar to the logistic regression meaning on the left. This little circle represents two steps of computation. The first step is to compute z. This is still rare. One analysis. Second node equals W, transpose X plus B, A, two equals sequence. Feel free to pause the video if you want, but you can double check that the superscript in subscript notation is consistent with what we have written here above in purple. So we'll talk through the first two hidden units in the neural network. Hidden units C and four also represent similar computations. So now let me take this pair of equations and this pair of equations, and let's copy them to the next slide. So here's our neural network, and here's the first and here's the second equations they were worked on previously, for the first and the second hidden units, if you then go through and write out the corresponding equations for the third and fourth building units, you get the following. And just make sure this notation is clear. This is the vector w1, one. This is a vector transpose times x. So that's what the superscript T there represents. Is the vector transpose. Now, as you might have guessed, if you're actually implementing a neural network, doing this with a full root seems really inefficient. So what we're going to do is take these four equations and vectorize. So we're going to start by showing how to compute z as a vector. It turns out you could do it as follows. Let me take these w's and stack them into a matrix. Then you have w1 one, transpose. So that's a row vector. This is a column vector. Transpose gives you a row vector. Then w1 two transpose, w1 three transpose. W1 four transpose. And so this, by stacking those 4w vectors together, you end up with a matrix. So another way to think of this is that we have four logistic regression units there, and each of the logistic regression units has a corresponding parameter vector w, and by stacking those four vectors together, you end up with this four by D matrix. So if you then take this matrix and multiply it by your input features, x1, x2 x3 you end up with by how matrix multiplication works. You end up with w1, one transpose x w1, w2, one transpose X, W, v1, transpose x, w4, one transpose x, and then let's not forget the B's. So we now add to this vector in 112, [3993:46:40] 0: so that's basically This. This. 31234, [4020:32:00] 0: and so you see that each of the four rows of this outcome correspond exactly to each of these four rows as each of these four quantities. So in other words, we've just shown that this thing is therefore equal to z1, one z1, 2z, 1d z1, four, right as defined here. And maybe not surprisingly, we're going to call this whole thing the vector z1 which is taken by stacking up these individuals of Z's into a column vector. When we're vectorizing, one of the rules of thumb that might help you navigate this is that when we have different nodes in a layer, we'll stack them vertically. So that's why, when you had z1 one through z1 four, those corresponded to four different nodes in the hidden layer. And so we stacked these four numbers vertically to form the vector z1 and to use one more piece of notation, this four by three matrix here, which we obtained by stacking the lower case, you know, w1, one, two, and so on. We're going to call this matrix W, Capital One, and similarly, this vector, we're going to call B superscript, one square vector. So this is a four by one vector. So now we've computed Z using this vector matrix notation. The last thing we need to do is also compute these values of a and so probably won't surprise you to see that we're going to define a one as just stacking together those activation values, a one, one through a one, four. So just take these four values and stack them together in a vector called a one, and this is going to be a sigmoid of z1 where this now has been implementation of the sigmoid function that takes in the four elements of z and applies the sigmoid function elements wise to it. So just to recap, we figured out that z1 is equal to w1 times the vector x plus the vector b1 and a one is sigmoid times z1 let's just copy this to the next slide, and what we see is that for the first layer of the neural network, given an input x, we have that z1 is equal to w1, times x plus b1 and a one is sigma c1 and the dimensions of this are four by one equals this was a four by three matrix times a, three by one vector plus a four by one vector b, and this is four by one, same dimensions. And remember that we said x is equal to a zero, right? Just like Y hat is also equal to b2 so if you want, you can actually take this x and replace it with a zero, since a zero is if you want just an alias for the vector input features x. Now, through a similar derivation, you can figure out that the representation for the next layer can also be written similar where what the output layer does is it has associated with it, so the parameters w2 and b2 so w2 in this case, is going to be a one by four matrix, and b2 is just a real number as one by one. And so z2 is going to be a real number, which is right as a one by one matrix. It's going to be a one by four thing times a was four by one, plus b2 is one by one. And so this gives you just a real number. And if you think of this loss output unit as just being analogous to logistic regression, which have parameters w and b. W really plays an analogous role to w2 transpose. Or w2 is really W transpose and b is equal to BT. So if you were to cover up the lack of this network and ignore all that for now, then this is just this last output unit is a lot like logistic regression, except that in celebrating parameters as w and b, writing them as w2 and b2 with dimensions, one by four and one by one. So just to recap, for logistic regression, to implement the output or to implement prediction, you compute z equals W, transpose x plus b and a y, hat equals a, equals sigmoid of z. When you have a neural network with one hidden layer, what you need to implement to compute this output is just these four equations, and you can think of this as a vectorized implementation of computing the output. First these four logistic regression units in the hidden layer, that's what this does, and then this logistic regression in the output layer, which is what this does. I hope this description made sense, but the takeaway is to compute the output of this neural network, all you need is those four lines of code. So now you've seen how, given a single input feature vector at you can, with four lines of code, compute the outputs of this neural network, similar to what we did for logistic regression. We'll also want to vectorize across multiple train examples, and we'll see that by stacking up training examples in different columns and matrix. Just slight modifications of this, you also similar to what you saw in the distribution, be able to compute the output of this neural network, not just on one example at a time, but on your, say, your entire training set at a time. So let's see the details of that in this video. I [5443:01:20] 0: in the last video, you saw how to compute the prediction on the neural network given a single training example. In this video, you see how to vectorize across multiple training examples, and the outcome will be quite similar to what you saw for logistic regression, whereby stacking up different training examples in different columns of a matrix, you'll be able to take the equations you had from the previous video and with very low modification, change them to make the neural network compute the outputs of all the examples, pretty much all at the same time. So let's see the details of how to do that. These were the four equations we had in the previous video, how you compute z1, A, one, z2, and a two. And they tell you how, given an input feature, vector, x, you can use them to generate a two equals y, hat for a single training example. Now, if you have m training examples, you need to repeat this process for, say, the first training example, x, superscript round brackets, one to compute y, hat one, that's the prediction on your first training example. Then x2 use that to generate prediction y, hat two, and so on down to M to generate prediction y, hat M and so, in order to write this activation function notation as well, I'm going to write this as a two square bracket, round bracket one. This is a two two and a 2m M. So this notation a square bracket to round bracket i. The round bracket i refers to training example, I, and the square bracket two refers to layer two. Okay, so that's how the square bracket and round bracket indices work. And so this suggests that if you have an unvectorized implementation and want to compute the predictions for all your training examples, you need to do for i equals one to m, then basically implement these four equations, right, z1, i equals W, 1x, I, one i equals sigma, z1, I [6132:26:40] 0: so in one i z2, I equals, [6198:40:00] 0: w2, A, one, i, s, b2, and a, two, I equals, say for z2, i right. So basically, these four equations on top, but adding the superscript [6252:21:20] 0: round bracket i [6265:41:20] 0: to all the variables that depending on the training example. So adding the superscript round bracket i to XC and k, if you want to compute all the outputs on your end. For examples, what we'd like to do is vectorize this whole computation so as to get rid of this volume. And by the way, in case, it seems like I'm getting a lot of nitty gritty line algebra, it turns out that being able to implement this correctly is important in a deep learning error. And we actually chose notation very carefully for this course, to make these vectorization steps as easy as possible. So I hope that going through this nitty gritty will actually help you to more quickly get correct implementations of these algorithms working. All right. So let me just copy this whole block of code to the next slide, and then we'll see how to vectorize this. So here's we had from the previous slide with a for loop going over all any training examples. So recall that we defined the matrix x to be equal to our training examples stacked up in these columns. So take the training examples, stack them columns, so this becomes a n or maybe NX by m dimension matrix. I'm just going to give away the punch line and tell you what you need to implement in order to vectorize implementation. This. Group. Turns out what you need to do is compute capital z1, [6691:44:00] 1: equals W, 1x plus b1 [6720:48:00] 0: capital A one equals sigmoid of z1, [6743:12:00] 0: then capital z2 equals w2, times a, one, [6789:25:20] 0: two and then a two equals sigmoid, O, [6815:17:20] 0: z2, so if you want the analogy is that we went from lower case vector x's to just capital case x matrix by stacking up the lowercase x's in different columns, if you do the same thing for the z's. So for example, if you take z1 one, z1 two and so on, these are all column vectors up to z 1m, right? So that's this first quantity, but all m of them, and stack them in columns. Then this gives you the matrix z1 and similarly, if you look at, say, this quantity, you take a one, one, A, one, two and so on, in a, 1m and stack them up in columns. Then this just as we went from lowercase x to capital case x and lowercase E to capital case z, this goes from the lowercase a, which are vectors, to this capital A one, yes over there. [7140:58:40] 0: And similarly, for [7152:42:40] 0: z2 and a two, [7164:26:40] 0: they're also ok by taking these vectors and stacking them horizontally, taking these vectors and stacking them horizontally in order to get Z, capital z2 and capital A, two. One other property of this notation that might help you to think about it is that these matrices say z and a horizontally, we're going to index across training samples. So that's why the horizontal index corresponds to different training samples in the sweeper, left to right, you're scanning through the training site. And vertically, this vertical index corresponds to different nodes in the neural network. So for example, this node, this value at the top most top left most corner, the matrix corresponds to the activation of the first hidden unit on the first training example. One value down corresponds to the activation in the second hidden unit on the first training example, then the third hidden unit on the first training example, and so on. So as you scan down, this is indexing into the hidden units number you whereas if you move horizontally, then you're going from the first hidden unit in the first train example, to now the first hidden unit, second training example, the third train example, and so on, until this node here corresponds to the activation of the first hidden unit in the final training example, the M training example. So the horizontal the matrix A goes over different training examples. And vertically, the different indices in the matrix A corresponds to different ignorance. [7651:17:20] 2: So and a [7676:10:40] 0: similar intuition holds true for the matrix z as well, as well as for x, where horizontally corresponds to different training samples, and vertically, it corresponds to different features, different input features which are really different nodes input linear of the neural network. So with these equations, you now know how to implement a neural network with vectorization. That is vectorization across multiple examples. In the next video, I want to show you a bit more justification about why this is a correct implementation of this type of vectorization. It turns out the justification will be similar to whether you have seen for logistic regression. Let's go on to the next video. [7906:13:20] 0: In the previous video, we saw how, with your training examples stacked up, you can derive a vectorized implementation of for propagation in your network capability of justification. The wide equations we wrote down is a correct implementation of vectorizing across multiple examples. So let's go through part of the. The forward propagation calculation for a few examples. Let's say that for the first train example, you end up computing this [8050:40:00] 1: x1, plus b1, [8068:32:00] 0: and then for the second train example, you end up computing this [8101:36:00] 0: two plus b1 [8121:09:20] 0: and then, for the third training example, computing this three plus b1 so just to simplify the explanation on this slide, I'm going to ignore B. So let's just say, you know, for the to simplify this justification a little bit that b is equal to zero, but the argument I'm going to lay out will work with just a little bit of a change, even when b is non zero. This just simplified the description on this slide a bit. Now w1 is going to be some matrix, right? So I have some number of rows in this matrix. So if you look at this calculation x1 [8299:12:00] 0: what you have is that [8312:32:00] 0: w1 times [8327:44:00] 0: x1 gives you some column vector, which means draw like like this. And similarly, if you look at this vector x2 you have that w1 times x2 [8419:01:20] 0: gives some other [8433:09:20] 0: column vector, right? That gives you this, I guess, z1, two. And finally, if you look at x3 you have w1 times x3 gives you some third column vector. That's this z1 three. So now if you consider the training set capital X, which we formed by stacking together all of our training examples. So the matrix capital X is formed by taking the vector x1 and stacking it vertically with x2 [8616:48:00] 0: and then also [8627:12:00] 0: x3 this is if we have only three training examples more, there'll be a keep stacking horizontally like that. But if you now take this matrix x and multiply it by w, then you end up with, if you think about how matrix multiplication works, you end up with the first column being these same values that had drawn up there in purple. The second column will be those same four values, and the third column will be [8770:34:40] 0: those orange values, what they turn out to be. [8789:30:40] 0: But of course, this is just equal to the 2z, one, one [8823:28:00] 0: expresses a column vector [8836:00:00] 0: followed by z1 two expressed as a column vector followed by z1 three also expresses a column vector. And this is maybe a feature examples, if you have more examples, then there'll be more columns. And so this is just [8916:53:20] 0: a matrix, capital, z1, [8931:17:20] 0: so I hope this gives a justification the Y when we had previously w1, times x, I equals z1 I we're looking at single chain example at a time when you took the different training examples and stack them up in different columns, then the corresponding result is that you end up with disease also stacked up in columns. And I won't show we can convince yourself if you want that with Python broadcasting. If you add that in these values of b, the values are still connected. What actually ends up happening is you end up with Python broadcasting, you end up adding vi [9135:28:00] 0: individual to each of the columns. [9155:12:00] 0: So on this slide, I've only justified that z1, [9181:09:20] 0: equals W, 1x [9192:37:20] 0: plus b1, is a correct vectorization of the first step of the four steps we have in the previous slide. But it turns out that a similar analysis allows you to show that the other steps also work out using very similar logic, where, if you stack the inputs and columns, then after the equation, you get the corresponding outputs also stacking columns. Finally, let's just recap everything we talked about in this video. If this is your neural network, we said that this is what you need to do if you were to implement for propagation. One. Training example, [9344:16:00] 0: I equals 1z, N, [9353:52:00] 0: and we said, let's stack up the training examples in columns like so. And for each of these values, z1, A, one, z2, A, two, let's stack up the corresponding columns as follows. So this example in cap, a, one, but this is true for z1, A, one, z2 and a two. Then what we showed on the previous slide was that this line allows you to vectorize this across all m examples the same time. And it turns out with a similar reasoning, you can show that all of the other lines are correct vectorizations of all four of these lines of code. And just as a reminder, because x is also equal to [9571:38:39] 0: a zero, because you remember [9586:34:40] 0: that the input feature of vector x was equal to a zero, so x i equals a, zero i. Then there's actually a certain symmetry to these equations, where this first equation can also be written z1, equals w1, [9675:12:00] 0: A, zero plus b1, [9688:16:00] 0: and so you see that this pair of equations and this pair of equations actually look very similar, but just of all of the indices advanced by one. So this kind of shows that the different layers of a neural network are roughly doing the same thing, or just doing the same computation over and over. And here we have a two layer neural network, where we go to a much deeper neural network in next week's videos, you see that even deeper neural networks are basically taking these two steps and just doing them even more times in your senior year. So that's how you can vectorize your neural network across multiple train examples. Next we've so far, I've been using the sigmoid function throughout that neural networks. Turns out that's actually not the best choice. In the next video, let's delve a little bit further into how you can use different what's called activation functions, of which the sigmoid function is just one possible choice. So [9979:49:20] 0: when you build your neural network, one of the choices you get to make is what activation function to use in the hidden layers as well as at the output unit of your neural network. So far, we've just been using the sigmoid activation function, but sometimes other choices work much better. Let's take a look at some of the options in the forward propagation steps for the neural network. We had these two steps where we use the sigmoid function here, so that sigmoid is called an activation function. And here's the familiar sigmoid function, A equals one over one plus e is negative z. So in the more general case, we can have a different function, g of z, which we write here, where g could be a nonlinear function that may not be the sigmoid function. So for example, the sigmoid function goes between zero and one, an activation function that almost always works better than the sigmoid function is the tan h function, or the hyperbolic tangent function. So this is z, this is a, this is A equals tan h of z, [10328:58:40] 0: and this goes between plus one [10347:06:39] 0: and minus one. The formula for the tan h function is e to the z minus e to the negative z over their sum. And it's actually mathematically a shifted version of the sigmoid function. So as a you know, sigmoid function just like that, but shifted so that it now crosses a 00 point and rescale so it goes between minus one and plus one. And it turns out that for hidden units, if you let the function g [10514:29:20] 0: of z [10527:17:19] 0: be equal to tan h of z, this almost always works better than the sigmoid function, because with values between plus one and minus one, the mean of the activations that come out of your hidden layer are closer to having a zero mean. And so just as sometimes when you train a learning algorithm, you might center the data and have your data have zero mean using a tannic instead of a sigmoid function, kind of has the effect of centering your data so that the mean. Your data is closer to zero, rather than maybe 0.5 and this actually makes learning for the next layer a little bit easier. We'll say more about this in the second course, when we talk about optimization algorithms as well. But one takeaway is that I pretty much never use the sigmoid activation function anymore. The tanh function is almost always strictly superior. The one exception is for the output layer, because if y is either zero or one, then it makes sense for y hat to be a number that you want to output that's between zero and one, rather than between minus one and one. So the one exception where I would use the sigmoid activation function is when you're using binary classification, in which case you might use the sigmoid activation function for the output layer. So P of z2 here is equal to sigma of z2 and so what you see in this example is where you might have a tan h activation function for the hidden layer and sigmoid for the output layer. So the activation functions can be different for different layers, and sometimes to denote that the activation functions are different for different layers, we might use these square bracket superscripts as well to indicate that g of square bracket one may be different than g of square bracket two, right again, square bracket one superscript refers to this layer, and superscript square bracket two refers to the upper layer. Now, one of the downsides of both the sigmoid function and the tan h function is that if z is either very large or very small, then the gradient or the derivative or the slope of this function becomes very small. So if z is very large or Z is very small, the slope of the function, you know, ends up being close to zero. And so this can slow down gradient descent. So one other choice that is very popular in machine learning is what's called the rectified linear unit. So the value function looks like this, and the formula is A equals max of zero, comma z, so the derivative is one, so long as z is positive and derivative, or the slope is zero when z is negative. If you're implementing this, technically, the derivative when z is exactly zero is not well defined. But when you implement this in the computer, the odds that you get exactly Z equals 0000000, is very small, so you don't need to worry about it. In practice, you could pretend to derivative when z is equal to zero. You can pretend it's either one or zero, and the code will work just fine. So the fact that it's not differentiable, the fact that so here are some rules of thumb for choosing activation functions. If your output is 01, value, if you are using binary classification, then the sigmoid activation function is a very natural choice for the output layer, and then for all other units, [11544:16:00] 0: value or the [11564:32:00] 0: rectified linear unit [11585:57:20] 0: is increasingly the default choice of activation function. So if you're not sure what to use for your hidden layer, I would just use the ReLU activation function. That's what you see most people using these days, although sometimes people also use the tan h activation function. One disadvantage of the value is that the derivative is equal to zero when z is negative. In practice, this works just fine, but there is another version of the value called the leaky value. We'll give you the formula on the next slide, but instead of it being zero, when z is negative, it just takes a slight slope, like so. So this is called the leaky value. This usually works better than the ReLU activation function, although it's just not used as much in practice. Either one should be fine, although you had to pick one. I usually just use the ReLU. And the advantage of both the ReLU and the leaky ReLU is that for a lot of the space of z, the derivative of the activation function, the slope of the activation function is very different from zero. And so in practice, using the ReLU activation function, your neural network will often learn much faster than using the tan h or the sigmoid activation function, and the main reason is that there's less of this effect of the slope of the function going to zero, which slows down learning. And I know that for half of the range of z, the slope of ReLU is zero. But in practice. Is Enough of your hidden units will have z greater than zero, so learning can still be quite fast for most training examples. So let's just quickly recap the pros and cons of different activation functions. Here's a sigmoid activation function. I would say, never use this, except for the output layer if you're doing binary classification. Almost never use this. And the reason I almost never use this is because the tan h is pretty much strictly superior. So the tan h activation function is this, and then the default, the most commonly used activation function is the value which is this, so you're not sure what else you use, use this one, and maybe you know, feel free also to try the leaky value where might be 0.0 1z comma, Z, right? So A is the max of 0.01 times z and z. So that gives you this bend in the function. And you might say, you know, why is that constant 0.01, well, you can also make that a another parameter of the learning algorithm. And some people say that works even better. But I hardly see people do that so, but if you feel like trying it in your application, you know, please feel free to do so, and you can just see how it works and how well it works, and stick with it if it gives you a good result. So I hope that gives you a sense of some of the choices of activation functions you can use in your network. One of the themes we'll see in deep learning is that you often have a lot of different choices in how you go to the neural network, ranging from number of hidden units to the choice of activation function to how you initialize the weights, which you'll see later. A lot of choices like that. And it turns out that it's sometimes difficult to get good guidelines for exactly what will work best for your problem. So throughout these two poses, I keep on giving you a sense of what I see in the industry in terms of what's more or less popular. But for your application, with your applications idiosyncrasies, it's actually very difficult to know in advance exactly what will work best. So a common piece of values would be, if you're not sure which one of these activation functions work best, you know, try them all and evaluate on like a holdout validation set, or like a development set, which we'll talk about later, and see which one works better, and then go with that. And I think that by testing these different choices for your application, you'd be better at future proofing your neural network architecture against the idiosyncrasies are problem as well as evolutions of the algorithms, rather than, you know, if I were to tell you always use a value activation and don't use anything else that that just may or may not apply for whatever problem you end up working on, you know, either either in the near future or in the distant future. All right, so that was a choice of activation functions, and you see the most proper activation functions. There's one other question, which is, why do you even need to use an activation function at all? Why not just do away with that? So let's talk about that in the next video. See why neural networks do need some sort of nonlinear activation function. [12950:13:20] 0: Why does your neural network need a non linear activation function? Turns out that for your neural network to compute interesting functions, you do need to pick a nonlinear activation function. Let's see what so here's the forward prop equations for the neural network. Why don't we just get rid of this? Get rid of the function g and set a one equals zero. Alternatively, you could say that g of is equal to z, right? Sometimes this is called the linear activation function. Maybe a better name for it would be the identity activation function, because it just outputs whatever was input for the purpose of this. What if a two was just equal to z2 it turns out, if you do this, then this model is just computing y, y, hat as a linear function of the input features x, to take the first two equations. If you have that A, one is equal to z1, [13220:53:19] 0: is equal to [13230:13:20] 0: W, 1x plus b, [13248:21:20] 0: and then a two is equal to z2, [13268:26:40] 0: is equal to w2, a one plus b. [13307:38:39] 0: Then, if you take this definition of a one and plug it in there, you find that a two. Is equal to w2 times W, 1x plus b1, so this is one plus b2, [13404:32:00] 0: and so this simplifies to [13419:12:00] 0: w 2w 1x [13437:09:20] 0: plus w2 [13471:49:19] 0: so this is just, [13492:53:20] 0: let's call this w prime B prime. [13508:37:20] 0: So this is just equal to w prime, x plus b prime. If you were to use linear activation functions, or we also call them identity activation functions, then the neural network is just outputting a linear function of the input. And we'll talk about deep networks later, neural networks with many, many layers, many, many heavy layers. And it turns out that if you use a linear activation function, or alternatively, you don't have an activation function, then no matter how many layers your neural network has always do it if it's just computing the linear activation function. So you might as well not have any some of the cases that briefly mentioned. It turns out that if you have a linear activation function here and a sigmoid function here, then this model is no more expressive than standard logistic regression without any hidden layer. So I won't bother the pre time, but you could try to do so if you want. But the take home is that a linear hidden layer is more or less useless, because the composition of two linear functions is itself a linear function. So unless you throw a non linearity in there, then you're not computing more interesting functions. Even as you go deeper, there is just one place where you might use a linear activation function, g of z equal z, and that's if you are doing machine learning on a regression so y is a row number. So for example, if you're trying to predict housing prices, so y is a it's not 01 but it's a real number, anywhere from $0 is the price of house up to however expensive, right? Houses can houses, you know, potentially millions of dollars so however, however much houses cost in a data set, why takes on these real values, then it might be okay to have a linear activation function here so that your output Y hat is also a real number, go anywhere from minus infinity to plus infinity, [14120:58:39] 0: but then the hidden units [14131:38:39] 0: should Not using activation functions value or tan edge or leaky value, or maybe something else. So the one place you might use a linear activation function is usually in the output layer. But other than that, using a linear activation function in the heading layer, except for some very special circumstances relating to depression, using the ring activation function. Oh, and of course, they're actually predicting housing prices, as you saw in the brief one video, because housing prices are all non negative, perhaps even then, you can use a revenue activation function so that your outputs y hat are all greater than or equal to zero. So I hope that gives you a sense of why having a non linear activation function is a critical part of neural networks. Next we're going to start to talk about gradient descent, and to do that to set up our discussion for gradient descent. In the next video, I want to show you how to estimate or how to compute, the slope or the derivative of individual activation functions. So let's go on to the next video next year to [14493:51:59] 0: find out when you implement back propagation for your neural network, you need to really compute the slope of the derivative of the activation functions. So let's take a look at our choices of activation functions and how you can compute the slope of these functions. Here's the familiar sigmoid activation function, and so for any given value of z, maybe this value of z, this function, will have some slope or some derivative corresponding to a low line there. You know, the height over width of this little triangle here. So if g of z is the sigmoid function, then the slope of the function. Function is D, dz, g of z. And so we know from calculus that this is the slope of g of x at z. And if you are familiar with calculus and know how to take derivatives, if you take the derivative of the sigmoid function, it is possible to show that it is equal to this formula. And again, I'm not going to do the calculus steps, but if you're familiar with calculus, feel free to pause the video and try to prove this yourself. And so this is equal to just p of z times one minus g of z. So let's just sanity check that this expression makes sense. First, if z is very large, let's say z is equal to 10, then g of z will be close to one. And so the formula we have on the left tells us that d, d z, g of z does be close to g of z, which is equal to one times one minus one, which is therefore very close to zero. And this isn't the correct because z is very large the surface of zero. Converse, d of z is equal to minus 10. So it says way out there, then g of z is close to zero. So the formula on the left tells us dvz, g of z will be close to g of z, which is zero times one minus zero. So it is also very close to zero. Finally, if z is equal to zero, then g of z is equal to one half. That's a sigmoid function right here. And so the derivative is equal to one half times one minus one half, which is equal to one quarter. And that actually is, turns out to be the correct value of the derivative, or the slope of this function, where z is equal to zero. Finally, just to introduce one more piece of notation, sometimes, instead of writing this thing, the shorthand for the derivative is g prime of z. So g prime of z in calculus, the little dash on top is called prime. So G prime of z is the shorthand for the in calculus for the derivative of the function of g with respect to input variable z. And then in a neural network, we have A equals V of Z right equals this. Then this formula also simplifies to a times one minus a. So sometimes the implementation, you might see something like g prime of z equals a times one minus A, and that just refers to the observation that g prime, which just means the derivative is equal to this over here. And the advantage of this formula is that if you've already computed the value for a then, by using this expression, you can very quickly compute the value for the slope for g prime All right, so that was the sigmoid activation function. Let's now look at the tan h activation function, similar to what we had previously the definition of d dz, g of z is the slope of g of g, of z, at a particular point of z. And if you look at the formula for the hyperbolic tangent function, and if you know calculus, you can take derivatives and show that this simplifies to this formula. And using the shorthand we had previously when we call this g prime of z, so if you want, you can suddenly check that this formula makes sense. So for example, if z is equal to 10, tan h of z will be very close to one. This goes from plus one to minus one, and then P, prime of z, according to this formula, will be about one minus one squared, so zero. So that was if z is very large, the slope is close to zero. Conversely, if z is very small, say z is equal to minus 10, then tan h of z will be close to minus one, and so V pi of z will be close to one minus negative one squared. So it's close to one minus one, which is also close to zero. And if I was equal to zero, then tan h of z is equal to zero, and then the slope is actually equal to one which is which is actually the slope when z is equal to zero. So just to summarize, if A is equal to E of z, so if a is equal to this tan h of z, then the derivative Q, prime of z is equal to one minus Az. Square. So once again, if you already computed the value of a you can use this formula to very quickly compute the derivative as well. Finally, here's how you compute the derivatives for the value and weak value activation functions for the value g of z is equal to max of zero, comma z, so the derivative is equal to turns out to be zero. If z is less than zero and 1z is greater than zero, undefined, technically undefined, if z is equal to exactly zero. But if you implementing this in software. It might not be 100% mathematical, right? It'll work just fine if z is exactly zero, if you set the derivative to equal to one, or if it said to be zero, it doesn't matter if you have experiment optimization, technically, g prime then becomes what's called a sub gradient of the activation function g of z, which is why gradient descent still works. But you can think of it as that the chance of z being, you know, zero point, exactly 00000, is so small that it almost doesn't matter what you set the derivative to be equal to when z is equal to zero. So in practice, this is what people for, the derivative of z. And finally, if you are training a new network with the VP value activation function, then g of z is going to be max of say, 0.0 1z comma z. And so g prime of z is equal to 0.01 so z is less than zero, and one if Z is greater than zero, and once again, the gradient is technically not defined. Your z is exactly equal to zero. But if you implement a piece of code that says the derivative or that says g prime to either 0.01, or to one, either way, it doesn't really matter, but z is exactly zero, your code will work. So onto these formulas, you should either compute the slopes or the derivatives of your activation functions. Now you have this building block. You're ready to see how to implement gradient descent for your neural network. Let's go on to the next video to see that i [16745:46:40] 0: All right, I think this will be an exciting video. In this video, you see how to implement gradient descent for your neural network with one hidden layer. In this video, I'm going to just give you the equations you need to implement in order to get back propagation or to get good in the same working and then you look at what [16841:20:00] 2: happened in similar [16892:48:00] 0: cases if you offer this one, I'll give some more intuition about why these but this is a status agreement, [16941:41:20] 3: really fascinating. So what's your prediction? Ultimately, dA is going to look at this? The judge wants more [16970:29:20] 4: time to look at it. Are we going to see the Mendez? Physical [16991:01:20] 0: equations are the accurate equations, or the correct equations for computing the gradients you need for the neural network. So your neural network with a single hidden layer for now, we'll have parameters, w1 b1 w2 and b2, and so as a reminder, if you have NX or alternatively, n zero input features and n1 hidden units and n2 output units. In our example, so far, going at n2 equals one. Then the matrix w1 will be n1 by n zero. B1 will be an n1 dimensional vector, as you can write down as an n1 by one dimensional matrix, the column vector, the dimensions of w2 will be n2 by n1 and the dimension of b2 will be n2 by one, where, again, so far, we've seen examples where n2 is equal to one, where you have just one, a single, hidden B, so you also have a cause. Function for your neural network. And for now, I'm just going to assume that you're doing binary classifications. So in that case, the cost of your parameters as follows, is going to be one over m of the average of that loss function. And so l here is the loss when your neural network predicts y. Hat, this is really into when the ground truth variable is equal to y. If you do binary classification, the loss functions can be exactly what you use for logistic regression earlier. So to train the parameters in your algorithm, you need to perform creating descent and training a neural network is important to initialize the parameters randomly routed into all zeros. We'll see later why that's the case. But after initializing the parameter to something, each loop of grading descent would compute the predictions. So you basically compute y hat i for i equals one through M, say, and then you need to compute the derivative. So you need to compute d, w1 and that's you see the derivative of the cost function with respect to the parameter w1 you need to compute another variable, we call D b1 which is the derivative, or the slope of the cost function with respect to the variable b1, and so on, similarly for the other parameters, w2 and b2 and then finally, the gradient descent update would be to update w1 as w1 minus alpha, the learning rate times d, w1, b1, is updated as b1 one is the learning rate times d, b1, and similarly for w2 and b2 and sometimes I use colon equals and sometimes equals either notation works fine. And so this would be one iteration of gradient descent, and then you repeat this some number of times until your parameters look for the conversion. So in previous videos, we talked about how to compute the predictions, how to compute the outputs, and we saw how to do that in a vectorized way as well. So the key is to know how to compute these partial derivative terms. DW one, DV one, as well as the derivatives DW two and DB two. So what I'd like to do is just give you the equations you need in order to compute these derivatives, and I'll defer to the next video, which is an optional video to go greater into depth about how we came up with those formulas. So let me just summarize, again, the equations, the forward propagation. So you have z1, equals W, 1x, plus b1, and then a, one equals the activation function in that layer [18147:43:59] 0: applied other than y, C, z1, and [18161:20:00] 0: z2 equals w2, A, [18178:56:00] 0: one plus b2, and then finally, this is all vectorized across the training set. A, two is equal to b2 of z2, again, for now, if we assume you're doing binary classification, then this activation function really should be the sigmoid function census for that angle. So that's the forward propagation, or the vector right forward complication for your neural network. Next, let's compute the derivatives. So this is the back propagation step going to compute d, z2, equals a, two minus the ground truth y. And just just as a reminder, all this is vectorized across examples. So the matrix y is this one by m matrix that lists all of the N examples stacked horizontally. Then it turns out DWT is equal to this. In fact, these first three equations are very similar to gradient descent for logistic regression. [18541:25:19] 0: And just a little detail this [18556:37:19] 0: np dot sum is a Python non pi command for sum across one dimension of a matrix, in this case, something horizontally. And what keep dims does is it prevents Python from outputting one of those funny rank one purveys, where the dimensions was no n comma. So by having keep dims equals true. This ensures. That Python outputs for DB two a vector that is n by one. In fact, technically this will be, I guess, n2 by one. In this case, it's just a one by one number. So maybe it doesn't matter, but later on, we'll see when it really matters. So so far, what we've done is very similar to logistic regression, but now, as you compute, continue to run back propagation, you would compute this [18819:49:20] 0: times g1, prime of z1, [18843:33:20] 0: so this quantity, p1 prime is the derivative of what it was the activation function you use for the hidden layer and for the output layer. I assume that you're doing binary classification with the sigmoid function. So those are really baked into that formula for dz, two and this times is an element wise product. So this here is going to be an n1 by m matrix, and this here, this element wise derivative thing, is also going to be an n1 by m matrix. And so this times there is an element wise product of two matrices. Then finally, DW one is equal to that. Equal to that, and d b1, [19050:45:20] 0: is equal to this. D, [19071:33:20] 0: P, W, some, d, z1, [19089:57:20] 0: this is equals one, [19122:45:20] 0: equals true. So whereas previously, the keep dims maybe matter less if n2 is equal to one. So this is just a one by one thing. It's just a real number here DB one will be a n1 by one vector. And so you want Python, you want np dot sum to output something of this dimension, rather than a funny rank one array of that dimension, which could end up messing up some of your data calculations. The other way would be to not have to keep the parameters, but to explicitly call V shape to reshape the output of np dot sum into this definition, [19311:17:20] 0: which you would like dB. [19325:09:20] 0: So that was for propagation in I guess four equations, and back propagation in, I guess six equations. I know I just wrote down these equations, but in the next optional video, let's go over some intuitions for how the six equations for the back propagation algorithm were derived. Please feel free to watch that or not. But either way, if you implement these algorithms, you will have a correct implementation of forward prop and backprop, and you'll be able to compute the derivatives you need in order to apply gradient descent to learn the parameters of the neural network. It is possible to implement this algorithm and get it to work without deeply understanding the calculus. A lot of successful deep learning practitioners do so, but if you want, you can also watch the next video, just to get a bit more intuition about the derivations of these of These equations, you'll see like nurses [19654:13:20] 4: before it's too late. Expensive car attacks. Unlike [19693:46:40] 2: other companies, [19715:06:39] 4: they don't just send you to The cheapest place in town. I [19745:14:40] 4: trust call [19753:20:00] 4: 808 422, [19860:05:20] 0: computation, drop, [19900:37:20] 0: how those equation i [19932:10:40] 0: this will derive this video is completely optional. I. [20037:36:00] 0: You know, so feel free to watch it or not. You should be able to do the whole world either way. So recall that when we talked about logistic regression, we had this forward pass where we compute z, then A and then the loss, and then to take derivatives, we had this backward path where we could first compute da and then go on to compute dz, and then go on to compute dw and dB. So the definition for the loss was L of a comma, y equals negative y log a minus one minus y times log one minus a. So if you're familiar with calculus, and you take the derivative of this with respect to a, that would give you the formula for da, so dA is equal to that. And if you actually figure out the calculus, you can show that this is Y over a negative y over a plus one minus y over one minus A just kind of derive that from calculus by taking derivatives of this. It turns out, when you take another step backwards to compute dz. We then worked out that dz is equal to a minus y. I did explain why previously, but it turns out that, from the chain rule of calculus, dz is equal to da times g prime of z, where, here, g of z equals sigmoid. Of z is our activation function for this output unit in logistic regression, right? So just remember, this is still logistic regression, X, 2x z, and then sigma unit, and then that gives us a gives us y hat. So here the activation function was a sigmoid function. And as an aside, only for those of you familiar with the chain rule of calculus. The reason for this is because a is equal to sigmoid of z, and so partial of l respect to z is equal to partial of l respect to A times dA, dz. But since A is equal to sigmoid of z, this is equal to d dz, g of z, right, which is equal to g prime of z. So that's why this expression which is dz in our code is equal to this expression which is da in our code, times g prime of z. And so this is just that. So that last derivation would make sense earlier from you're familiar with calculus, and specifically the chamber from calculus, but if not, I'll try to explain the intuition wherever is needed. And then finally, Having computed dz, from which is regression, we will compute dw, which it turns out, was dz times x, and DB, which is just dz when you have a single training example. So that was logistic regression. So what we're going to do when computing back propagation for a neural network is a calculation a lot like this, but only we'll do it twice, because now we have not x going to an output unit, but x going to a hidden layer and then going to an output unit. And so instead of, instead of this computation being sort of one step, as we have here, we'll have two steps here, right in this kind of a neural network with two layers. So in this two layer neural network that is for the input layer, hidden layer and an output layer, remember the steps of a computation. First you compute z1 using this equation, and then compute a one, and then you compute z2 and notice z2 also depends on the parameters, w2 and b2 and then, based on z2 compute a two, and finally, that gives you the loss. So what back propagation does is it will go backward to compute da two, and then dz two, it can go back to compute DW two and DB two, go back to compute da one, [21285:46:40] 0: dz one and so on. [21308:10:40] 0: We don't need to take derivatives with respect to the input x, since the input x for supervised learning is. X, so we're not trying to optimize x, so we won't bother to take derivatives, at least for supervised learning, with respect to x. And so I'm going to skip explicitly computing da two, if you want. You can actually compute da two and then use that to compute dz two. But in practice, you can collapse both of these steps into one, into one steps, you end up that dz two is equal to a two minus Y, same as before. And you have also going to write DW two and DB two. Down here below, you have that dw two is equal to VZ two times a, one transpose and DB two equals dz two. So this step is quite similar to for logistic regression, where we had that dw was equal to dz times x, except that now a one place the row of x, and there's an extra transpose there, because the relationship between the capital matrix W and our individual parameters W was there's a transpose there, right? Because W is equal to a row vector in the case of logistic regression, with a single output. DW two is like that, whereas w here was a column vector. So that's why there's an extra transpose for a one, whereas we didn't for x here for logistic regression. So this completes half of back propagation. And then again, you can compute da one, if you wish. Although in practice, the computation for da one and dz one are usually collapsed into one step. And so what you should actually implement is that dz one is equal to w2 transpose times dz two, and then times an element wise product of p1 prime of z1, and just to do a check on the dimensions, right? If you have a neural network that looks like this, right? Of course, why? If so, if you have n zero, and x equals n zero input features and one hidden units, and n2 so far, and n2 in our case, just one output unit, then the matrix w2 is n2 by n1 dimensional, z2 and therefore d z2 are going to be. N2 by one dimensional is really going to be a one by one when we're doing binary classification and z1 and therefore also dz one are going to be n1 by one dimensional. Right? Note that for any variable, foo, foo and D foo always have the same dimension. So that's why W and DW always have the same dimension. And similarly, for B and DB and Z and dz and so on. So to make sure that the dimensions of this all match up, we have that dz one is equal to w2 transpose times dz two. And then this is an element wise product times g1, prime of z1, so matching the dimensions from above, this is going to be n1 by one is equal to w2 transpose, weak transpose of this. So this is going to be n1 by n2 dimensional. Dz two is going to be n2 by one dimensional. And then this, this is the same dimension as z1 so this is also n1 by one dimensional element wise product. So the dimensions do make sense, right? N1 by one dimensional vector can be obtained by n1 by n2 dimensional matrix times n2 by n1 because the product of these two things gives you an n1 by one dimensional matrix. And so this becomes the element wise product of two and one by one dimensional vectors. And so the dimensions do match up. One tip when implementing a backprop, if you just make sure that the dimensions of your matrices match up. So if you think through what are the dimensions of your various matrices, including w1, w2, z1, z2, A, one, a, two, and so on. And just make sure that the dimensions of these matrix operations may match up sometimes, that will already eliminate quite a lot of bugs and background. All right. Okay, so this gives us dz one, and then finally, just to wrap up, DW one and DB one, we should write them here, I guess. But since I'm writing out the space, I'll write them on the right of the slide. DW one and DB one are given by the following formulas. This is going to equal to dz one times X transpose, and this is going to be equal to dz. And you might notice the similarity between these equations and these equations, which is really no coincidence, because x plays the row of a zero, so x transpose is a zero transpose. So those equations are actually very similar, all right. So that gives a sense for how back propagation is derived. And we have six key equations here for dz, two, DW two, DB two. Dz one, dw, one, and DB one. So let's just take these six equations and copy them over to the next slide. Here they are, and so far, we've derived back propagation for API training on a single training example at a time. But it should come as no surprise that rather than working on a single example at a time, we would like to vectorize across different training examples. So we remember that for forward propagation, when we're operating on one example at a time, we had equations like this, as well as, say, a one equals g1, of z1, and in order to vectorize, we took, say, the Z's and stacked them up in columns like this Z, 1m and called this capital Z. And then we found that by stacking things up in columns and defining the capital uppercase version of these, we then just had z1, equals W, 1x plus b and a one equals g1, of z1, right, and we defined the notation very carefully in this course to make sure that stacking examples in the different columns of a matrix makes all this work out. So it turns out that if you go through the map carefully, the same trick also works for back propagation. So the vectorized equations are as follows. First, if you take these D Z's for different cleaning examples and stack them as the different columns in the matrix, and same for this, and same for this, then this is the vectorized implementation. And then here's the definition for or here's how you can compute DW too. There is this extra one over m because the cost function J is this one over m of sum from i equals one through m of the losses. And so when computing derivatives, we have that extra one over m term, just as we did when we were computing the weight updates for logistic regression, and then that's the update you get for DB. Two, again, some of the DZ, and then with the one over M and then dz one is computed as follows, once again, this is an element wise product. Only, whereas previously, this was we saw on the previous slide that this was an n1 by one dimensional vector. Now this is a n1 by m dimensional matrix, and both of these are also n1, by m dimensional. And so that's why that asterisk is a element wise product. And then finally, the remaining two updates, perhaps shouldn't look too surprising. [23830:24:00] 0: So I hope that gives you some intuition for how the back propagation algorithm is derived in all of machine learning. I think the derivation of the back propagation algorithm is actually one of the most complicated pieces of math I've seen, and it requires knowing both linear algebra as well as the derivative matrices to really derive it from scratch, from those principles. If you are an expert in matrix calculus using this process, you might derive the algorithm yourself, but I think that there are actually plenty of deep learning practitioners that have seen the derivation at about the level you've seen in this video, and are already. Both have all the right intuitions, and they will implement this algorithm very effectively. So if you are an expert in calculus, do see if you can derive the whole thing from scratch. It is one of the very hardest pieces of math, one of the very hardest derivations that I've seen in all of machine learning. But either way, if you implement this, you know this will work, and I think you have enough intuition is to tune and get it to work so that there's just one last detail I want to share with you before you implement your neural network, which is how to initialize the weights of your neural network. And it turns out that initializing your parameters not to zero, but randomly, turns out to be very important for training your neural network. In the next video, you'll see why. When you [24224:42:40] 0: train your neural network, it's important to initialize the weights randomly. For logistic regression, it was okay to initialize the weights to zero, but for a neural network of initialize the weights, the parameters to all zero, and then apply gradient descent. It won't work. Let's see why. So you have here two input features, so n zero is equal to two and two hidden units, so n1 is equal to two, and so the matrix associated with the hidden layer of w1, is going to be two by two. Let's say that you initialize it to all zeros, 0002, by two matrix. And let's say b1 is also equal to 00. Turns out initializing the bias terms B to zero is actually okay, but initializing W to all zeros is a problem. So the problem with this form of initialization is that for any example you give it, you will have that a one, one and a one two will be equal, right? So this activation and this activation would be the same because both of these hidden units are computing exactly the same function. And then when you compute back propagation, it turns out that d z1 one and d z1 two will also be the same kind of by symmetry, right? Both of these hidden units would initialize the same way. Technically, for what I'm saying, I'm assuming that the outgoing weights are also identical, so that w2 is equal to 00, but if you initialize the neural network this way, then this hidden units and this hidden unit are completely identical. So they're completely sometimes you say they're completely symmetric, which just means that they're computing exactly the same function. And by kind of a proof by induction, it turns out that after every single iteration of training, your two hidden units are still computing exactly the same function. This is also to show that dw will be a matrix that looks like this, where every row takes on the same value. So we perform a weight update. So you perform, when you perform a weight update, w1 gets updated as w1 minus alpha times DW. You find that w1, after every iteration, will have the first row equal to the second row. So it's possible to construct a proof by induction that if you initialize all the weights, all the values of W to zero, then because both hidden units start off computing the same function, and both hidden units have the same influence on the output unit. Then after one iteration, that same statement is still true, the two hidden units are still symmetric, and therefore, by induction, after two iterations, three iterations and so on, no matter how long you train the neural network, both hidden units are still computing exactly the same function. And so in this case, there's really no point to having more than one hidden unit, because they're all computing the same thing. And of course, for larger neural networks, let's say you have three features and maybe a very large number of hidden units, a similar argument works to show that with a neural network like this, because it won't draw on all the edges. If you initialize the weights of zero, then all of your hidden units are symmetric, and no matter how long you run gradient descent, they'll all continue to compute exactly the same function. So that's not helpful, because you want the different hidden units to compute different functions. The solution to this is initialize your parameters randomly. So here's what you do. You can set w1, equals NP, dot, random dot, Rand N. This generates a Gaussian random variable, two, two, and then usually you multiply. This by a very small number, such as 0.01 so you initialize it to very small random values, and then B. It turns out that B does not have this symmetry problem, what's called the symmetry breaking problem. So it's okay to initialize B to just zeros, because so long as w is initialized randomly, you start off with the different hidden units computing different things, and so you no longer have this symmetry breaking problem. And then, similarly, for w2 you can initialize that randomly, and b2 you can initialize that to zero. So you might be wondering, you know, where did this constant come from, and why is it 0.01 why not put the number 100 or 1000 turns out that we usually prefer to initialize the weights to very, very small random values. Because if you're using a, say, tan h or sigmoid activation function, or if you have a sigmoid, even just at the output layer, if the weights are too large, then when you compute the activation values, remember that z1, is equal to w 1x plus B, and then a one is the activation function applied to z1 so if W is very big, z will be very big, or at least some values of z will be either very large or very small. And so in that case, you're more likely to end up at these flat parts of the tan h function or the sigmoid function, where the slope or the gradient is very small, meaning that gradient descent would be very slow, and so learning was very slow. So just to recap, if W is too large, you're more likely to end up, even at the very start of training with very large values of z, which causes your tan h, your sigmoid activation function, to be saturated, thus slowing down learning. If you don't have any sigmoid or tan h activation functions throughout your neural network, this is less of an issue, but if you're doing binary classification and your output unit is a sigmoid function, then you just don't want the initial parameters to be too large. So that's why multiplying by 0.01 would be something reasonable to try or any other small number. And same for w2 this can be a random, dot random. I guess this would be one by two in this example, times 0.0 1s, there. So finally, it turns out that sometimes there can be better constants than 0.01 when you're training a neural network with just one hidden layer. This is a relatively shallow neural network with about too many hidden layers, setting it to 0.01 will probably work, ok, but when you're training a very, very deep neural network, then you might want to pick a different constant than 0.01 and in next week's material, we'll talk a little bit about how and when you might want to choose a different constant than 0.01 but either way, it will usually end up being a relatively small number. So that's it for this week's videos. You now know how to set up a neural network with a hidden layer, initialize the parameters, make predictions using forward prop as well as compute derivatives in the blend gradient descent using backprop, so that you should be able to do the quizzes as well as this week's programming exercises. Best of luck with that. I hope you have fun with programming exercise and look forward to seeing you in the week four materials. You [26400:00:00] 0: I wish you right before I met you, actually, I had been working on neuroscience, and my undergraduate advisor, Jerry Kane at Stanford, encouraged me to take your intro to AI class. I didn't know that, so I had always thought that AI was a good idea, but [26556:53:20] 0: I was welcome to the fourth week of this class. By now, you've seen forward propagation and back propagation in the context of a neural network with a single hidden layer, as well as logistic regression, and you've learned about vectorization and when it's important to initialize the weights randomly. If you've done the past couple weeks homeworks, you've also implemented and seen some of these ideas work. Yourself. So by now you've actually seen most of the ideas you need to implement a deep neural network. What we're going to do in this week is take those ideas and put them together so that you'll be able to implement your own deep neural network. Because this week's programming exercise is longer and just has a bit more work. Gonna keep the videos for this week shorter as you can get through the videos a little bit more quickly and then have more time to do a significant coding exercise at the end, which I hope will lead you having built a deep neural network that you feel proud of. So what is a deep neural network? You've seen this picture for logistic regression, and you've also seen neural networks with a single hidden layer. So here's an example of a neural network with two hidden layers and a neural network with five hidden layers. We say that logistic regression is a very shallow model, whereas this model here is a much deeper model, and shallow versus depth is a matter of degree. So neural network with a single hidden layer, this would be a two layer neural network. Remember, when we count layers in neural network, we don't count the input layer, we just count the hidden layers as well as the output layer. So this would be a two layer neural network is still quite shallow, but not as shallow as logistic regression. Technically, logistic regression is a, you know, one layer neural network, but over the last several years, the AI or the machine learning community has realized that there are functions that very deep neural networks can learn, that shallower models are often unable to although for any given problem, it might be hard to predict in advance exactly how deep in your network you would want. So it would be reasonable to try logistic regression, try one and then two hidden layers, and view the number of hidden layers as another hyperparameter that you could try a variety of values of and evaluate on holdout cross validation data or on your development set. Say more about that later as well. Let's now go through the notation we'll use to describe deep neural networks. Here is a 1234, [27277:04:00] 0: layer neural network [27290:39:59] 0: with three hidden layers. And the number of units in these hidden layers are, I guess, 553, and it is one Alpha unit. So the notation we're going to use is going to use capital L to denote the number of layers in the network. So in this case, l is equal to four, and so that's the number of layers. And we're going to use N superscript l to denote the number of nodes or the number of units in layer lowercase L, so if we index this the input as layer zero, this is layer one, this is layer two, this is layer three, and this is layer four. Then we have that, for example, n1 that would be this the first hidden layer would be equal to five, because we have five hidden units there. For this one, we have that n2 the number of units in the second thing there, is also equal to five. N3 is equal to three, and n4 which is N, capital L is the number of units. This number of output units is equal to one, because here capital L is equal to four. And we're also going to have here that for the input layer, n zero is just equal to n x is equal to three. Okay, so that's the notation we'll use to describe the number of nodes we have in different layers. For each layer l, also going to use a L to denote the activations [27766:23:59] 0: in layer l, [27776:32:00] 0: so we'll see later that in forward propagation, you end up computing al as the activation G applied to Z L, and perhaps the activation is indexed by the layer l as well. And then we'll use WL to denote the weights for computing the value ZL in layer l. And similarly, BL is used to compute ZL. Finally, just to wrap up on the notation, the input features are called x, but x is also the activations of layer zero. So a zero is equal to x, and the activation of the final layer, a capital L, is equal to y hat so a superscript square factor, capital L is equal to. In the predicted output to prediction y hat to the neural network. So we now know what a deep neural network looks like, as was the notation we'll use to describe and to compute with deep networks. I know we've introduced a lot of notation in this video, but if you ever forget what some symbol means, we've also posted on the course website a notation sheet or notation guide they can use to look up what these different symbols means. Next, I'd like to describe what forward propagation in this type of network looks like. Let's go into the next video. [28165:09:20] 0: In the last video, we described what is a deep elevator neural network, and also talk about the notation we use to describe such networks. In this video, you see how you can perform forward propagation in a deep network as usual. Let's first go over what forward propagation will look like for a single training example X, and then later on, we'll talk about the vectorized version, where you want to carry out forward propagation on the entire training set at the same time. But given a single training example, x, here's how you compute the activations of the first layer. So for this first layer, you compute z1, equals w1, times x plus b1 so w1 and b1 are parameters that affect the activations in layer one. This is layer one of the neural network. And then you compute the activations for that layer to be equal to g of z1 and the deactivation function g depends on what layer you're at, and it will index that as deactivation function for layer one. So if you do that, you now compute the deactivations for layer one. How about layer two? Say that layer well, you would then compute z2, equals w2, A, one plus b2, and then, so the activation of layer two is the weight matrix times the outputs of layer one. So it's that value plus the bias vector for layer two. And then a two equals the activation function [28643:17:20] 0: applied to z2 [28664:53:20] 0: so that's it for layer two, and so on and so forth, until you get to the output layer. That's layer four, where you would have that z4 is equal to the parameters for that layer, times the activations from the previous layer, plus that bias vector, and then similarly, [28774:50:40] 0: a four equals G of z4 [28801:30:40] 0: and so that's how you compute your estimated output Y. Hat, so just one thing to notice, x here is also equal to a zero, because the input feature vector X is also the activations of layer zero. So we scratch our x, I'm going to cross our x and put a zero here, then you know, all of these equations basically look the same, right? The general rule is that Z L is equal to W, l times a of l minus one plus B, l1 there, and then the activations for that layer is the activation function applied to the values z. So that's the general forward propagation equation. So we've done all this for a single training example. How about for doing it in a vectorized way for the whole training set at the same time, the equations look quite similar as before. For the first layer, you would have capital z1, equals w1, times capital x plus b1, and then a one equals P of z1 right. And bearing in mind that x is equal to a zero, these are just, you know, the training examples stacked in different columns. You can take this, then scratch out x. You can put a zero there and then for the next layer, the next signal, z2 equals w2 [29285:30:40] 0: a one plus b2 [29299:06:40] 0: and a two equals P of z2 right? We're just taking. These vector z or a and so on, and stacking them up, this is Z vector for the first string example, Z vector for the second string example, and so on down to the M training example, and stacking these in columns and calling this capital Z, and similarly, for capital A, just as capital X, all the training examples are column vectors, stack left to right. And then again, end of this process, you end up with y hat, which is equal to g of z4 this is also equal to a four. And that's the predictions on all the retraining examples stamped horizontally. So just to summarize our notation, I'm going to modify this up here. Our notation allows us to replace lowercase c and a with the uppercase counterparts. These are already looks at the capital Z, and that gives you the vectorized version of what application that you carry out on the entire thing set at a time where a zero is x. Now, if you look at this implementation of vectorization, it looks like that. There is going to be for loop here, right? So it's a 4l equals one to four for l equals one through capital L, then you have to compute activations from layer one, then from there two, then from there three, and then from there four. So it seems that there is a for loop here, and I know that when implementing neural networks, we usually want to get rid of explicit for loops, but this is one place where I don't think there's any way to implement this without an explicit for loop. So when implementing forward propagation, it is perfectly okay to have a for loop that computes the activations for layer one, then layer two, then layer three, then therefore no one knows. And I don't think there exists any way to do this without a for loop that goes from one to capital L, from one through the total number of layers in a neural network. So this place is perfectly okay to have an explicit form. So that's it for the notation for deep neural networks, as well as how to do forward propagation in these networks. If the pieces we've seen so far looks a little bit familiar to you, that's because what we've seen is taking a piece very similar to what you've seen in the neural network with a single hidden layer and just repeating that more times. Now it turns out that we implement a deep neural network, one of the ways to increase your odds of having a bug free implementation is to think very systematically, carefully about the major solutions you're working with. So when I'm trying to debug my own code, I'll often pull a piece of paper and just think carefully through some dimensions of the matrix I'm working with. Let's see how you could do that [30126:50:40] 0: when implementing the deep neural network, one of the debugging tools I often use to check the practice of micro is to pull the piece of paper and just work through the dimensions and matrix I'm working with. So let me show you how to do that, since I hope this will make it easier for you to implement your deep Nexus. What so capital L is equal to five. Count them quickly, not counting the input layer. There are five layers here, so four output layer. And so the first step will be z1, w1, times d for future x plus b1, so let's ignore the bias terms B for now and focus on the parameters W. Now this first hidden layer has three hidden units. So this is layer zero, layer one, layer two, layer three, layer four and layer five. So using the notation which is the number, of individuals, and layer one is equal to three. And here we reflected n2 is equal to five and three is equal to four. Four is equal to two, and n5 is equal to one. And so far, but only seen, neural networks with a single output unit. But later, in later courses, we'll talk about neural networks with multiple output units as well, and finally, but the input there, we also have zero equals mx is equal to two. So now let's think about the dimensions of z, w and x. Z is the vector of activations for this first hidden layer. So z is going to be V by one is going to be a three dimensional vector. So I'm going to write it as a n1, N by one dimensional vector and one by one dimensional matrix, right? So d by N in this case. Now how about the input features at x? We have two input features. So x is, in this example, two by one, but more generally, it would be n zero by one. So what we need is for the matrix w1 to be something that when you multiply an n zero by one vector to it, you get an n1 by one vector, right? So you have sort of a three dimensional vector equals something times a two dimensional vector. And so by the rules of matrix multiplication, this has got to be a d by two matrix, right? Because the three by two matrix times a two by one matrix, or times a two by one vector, that gives you a three by one vector. And more generally, this is going to be an n1, by n, zero dimensional matrix. So what we figured out here is that the dimensions of w1 has to be n1 by n zero. And more generally, the dimensions of w l must be n L by N l minus one. So for example, the dimensions of w2 for this, it will have to be five by three, or it will be n2 by n1 [31081:57:19] 0: because we're going to compute z2 as [31110:45:20] 0: w2 times [31128:10:40] 0: a one and again, let's ignore the bias for now. [31151:22:40] 0: But so this is going to be three by one, [31169:14:40] 0: and we need this to be five by one. And so this had better be five by three. And similarly, w3 will be, is really the dimension of the next layer, comma the dimension of the previous layer. So this is going to be 4.5 w4 [31290:08:00] 0: is going to be [31305:25:20] 0: two by four, and w5 is going to be one by two. Okay, so the general formula to check is that, when you implementing the Matrix for a layer l, the dimension of that matrix be n, l by n, l minus one. Now let's think about the dimension of this vector, B. This is going to be a three by one vector, so you have to add that to another three by one vector in order to get a three by one vector as the output, or in this example, you need to add, this was going to be five by one. So it's going to be another five by one vector. In order for the sum of these three things that have in the boxes to be itself a five by one vector. So the more general rule is that, in the example on the left, b1, is n1 by one. Right, that's D by one. And in the second example, it is this is n2 by one. And so the more general case is that BL should be NL by one dimensional. So hopefully, these two equations help you to double check that the dimensions of your matrices W, as well as of your vectors B of the correct dimensions. And of course, if you're implementing back propagation, then the dimensions of dW should be the same as dimension of W, so DW should be the same dimension as W, and DB [31806:40:00] 0: should be the same dimension as B. [31823:59:59] 0: Now the other key set of quantities whose dimensions to check are these z x, as well as a of L, which we didn't talk too much about here, but because z of l is equal to g of a, of L apply element wise, then z and a should have the same dimension in these types of networks. Now let's see what happens when you have a vectorized implementation that looks at multiple examples at a time. Even for a vectorized implementation, of course, the dimensions of w, B, D, W and D, B will stay the same, but the dimensions of. Of z, a as well as x, will change a bit in your vectorized implementation. [32034:55:59] 0: So previously we had z1, [32050:56:00] 0: equals w1, [32063:44:00] 0: times x plus b1, where this was n1 by one, this was n1 [32121:41:20] 0: by n 0x. [32133:41:20] 0: Was n zero by one, and B was n1 by one. Now in a vectorized implementation, you would have z1 equals w1 times x plus b1 where now z1 is obtained by taking the Z ones for the individual example. So there's z1 one, z1 two up to z 1m and stacking them as follows and just gives you z1 so the dimension of z1 is that instead of being n1 by one, it ends up being n1 by M. If m is this is a training set the dimensions of w1 stays the same. So still, n1 by n zero and x, instead of being n zero by one, is now all your training examples stacked horizontally. So it's now n zero by m and so you notice that when you take a n1 by n zero matrix and multiply that by n zero by m matrix that together, they actually give you an n1 by m dimensional matrix as expected. Now, the final detail is that b1 is still n1 by one, but when you take this and add it to b then through Python broadcasting, this would get duplicated into an n1 by m matrix and then added element wise. So on the previous slide, we talked about the dimensions of w, B, D, W and D, B. Here what we see is that whereas Z, l, [32612:32:00] 0: as well as Al, [32624:32:00] 0: are of dimension NL by one, we have now instead that capital ZL, as well as capital Al, are NL by m, and a special case of this is when L is equal to zero, in which case, a zero, which is equal to just your claim Set Input Features, x, is going to be equal to n zero by n as expected. And of course, when you're implementing this in back propagation, we'll see later you end up when, when implementing a deep neural network, one of the debugging tools I often use to check the characters in my code is to pull a piece of paper and just work through the dimensions and matrix I'm working with. So let me show you how to do that, since I hope this will make it easier for you to implement your deep nets as well. So capital L is equal to five. Countdown quickly, not counting the input layer. There are five layers here. So 400 layers, one output layer. And so if you implement forward propagation, the first step will be z1, equals w1, times d for teachers, x plus b1 so let's ignore the bias terms B for now and focus on the parameters W. Now this first hidden layer has three hidden units. So this is layer zero, layer one, layer two, layer three, layer four, and layer five. So using the notation we had from the previous video, we have the n1 which is the number of hidden units in layer one is equal to three. And on here, we would have that n2 is equal to five, and three is equal to four, and four is equal to two, and five is equal to one. And so far, but only seeing neural networks with a single output unit. But later, in later courses, we'll talk about neural networks which also output units as well. And finally, for the input there, we also have n, zero equals mx is equal to two. So now let's think about the dimensions of z, w and x. Z is the vector of activations for this first hidden layer. So z is going to be V by one is going to. Be a three dimensional vector. So I'm going to vector. It is a n1 by one dimensional vector, or n1 by one dimensional matrix, three by one in this case. Now, how about the input features, X? X, we have two input features. So x is, in this example, two by one, but more generally, it will be n zero by one. So what we need is for the matrix w1 to be something that when you multiply an n zero by one vector to it, you get an n1 by one vector. So you have sort of a three dimensional vector equals something times a two dimensional vector. And so by the rules of matrix multiplication, this has got to be, say, three by two matrix, because a three by two matrix times a, two by one matrix, or times a, two by one vector, that gives you a three by one vector. And more generally, this is going to be n1 by n zero dimensional matrix. So what we think about here is that the dimensions of w1 has to be n1 by n zero. And more generally, the dimensions of WL must be NL by NL minus one. So for example, the dimensions of w2 for this, it will have to be [33736:37:20] 0: five by three, or it will be n2 by n1 [33772:53:19] 0: because we're going to compute z2 as [33801:46:39] 0: w2 times [33818:50:40] 0: a one and again. Let's ignore the bias now. But [33842:50:40] 0: so this is going to be three by one, [33860:10:40] 0: and we need this to be five by one. And so this had better be five by three. And similarly, w3 will be, is really the dimension of the next layer, comma the dimension of the previous layer. So this is going to be four by five. W4 is is going to be two by four, and w5 is going to be one by two. So the general formula to check is that when you're implementing the Matrix for a layer l, did the dimension of that matrix be n L by N l minus one? Now let's think about the dimension of this vector b. This is going to be a three by one vector. So you have to add that to another three by one vector in order to get a three by one vector as the output. Or in this example, you need to add this is going to be five by one. So it's going to be another five by one vector in order for the sum of these two things that have the boxes to be itself, a, five by one vector. So the more general rule is that, in the example on the left, b1, is n1 by one, that's D by one. And in the second example, it is this is n2 this is n2 by one. And so the more general case is that B L should be n l by one dimensional. So hopefully, these two equations help you to double check the dimensions of your matrices w as well as of your vectors e, of the correct dimensions. And of course, if you're implementing that propagation, then the dimensions of dW should be the same as dimension of W, so DW [34471:44:00] 0: should be the same dimension as W and DB [34497:19:59] 0: should be the same dimension as B. [34514:45:20] 0: Now the other key set of quantities whose dimensions to check are these z x, as well as a of L, which we didn't talk too much about here, but because z of l is equal to g, of a, of L, apply element wise, then z and a should have the same dimension as these types of networks. Now let's see what happens when you have a vectorized implementation that looks at multiple examples at a time. Even for a vectorized implementation, of course, the dimensions. Of w, B, D, W and D, B will stay the same, but the dimensions of z, a as well as x, will change a bit in your vectorized implementations. [34725:36:00] 0: So previously we had z1 [34741:35:59] 0: equals w1 times x plus b1 where this was n1 by one, this was n1 [34812:37:20] 0: by n 0x. [34824:21:19] 0: Was n zero by one, and B was n1 by one. Now in a vectorized implementation, you would have z1 equals w1 times x plus b1 where now z1 is obtained by taking the Z ones for the individual example, so that z1 one, z1 two up to z 1m and stacking them as follows, and this gives you z1 so the dimension of z1 is that instead of being n1 by one, it ends up being n1 by M. If m is the size of training set the dimensions of w1, stays the same. So still, n1 by n zero and x, instead of being n zero by one, is now all your training examples stacked horizontally. So it's now n zero by M. And so you notice that when you take a n1 by n zero matrix and multiply that by n zero by m matrix, that together, they actually give you an n1 by m dimensional matrix, as expected. Now the final detail is that b1 is still n1 by one, but when you take this and add it to B, then through Python broadcasting, this will get duplicated into an n1 by m matrix and then added element wise. So on the previous slide, we talked about the dimensions of w, B, D, W and D, B. Here what we see is that whereas ZL [35303:17:20] 0: as well as Al [35315:33:20] 0: are of dimension, NL by one, we have now instead that capital ZL as well as capital Al, Our ml by m, and the special case of this is when L is equal to zero, in which case a zero, which is equal to just your training set input features, x is going to be equal to n, zero by m, As expected. And of course, when you're implementing this in back propagation, you'll see later, you end up computing dz as well as dA and so these will, of course, has the same dimension as Z and a. So I hope the exercise went through helps clarify the dimensions of the various matrices you'll be working with when you implement back propagation for deep neural network. So long as you work through your code and make sure that all the matrices dimensions are consistent, that will usually help go some ways toward eliminating some class of possible bugs. So I hope that exercise for figuring out the dimensions of the various matrices you'll be working with is helpful when you implement a deep neural network, if you keep straight the dimensions of these various matrices and vectors you're working with, hopefully that will help you eliminate some class of possible bugs. It certainly helps me get my COVID. So next, we've now seen some of the mechanics of how to do certain forward palpitation in the neural network. But why are deep neural networks so effective? Why do they do better than shallow representations? Let's spend a few minutes in the next video to discuss i [35822:13:20] 0: Well, we've all been hearing that deep neural networks work really well for a lot of problems. And it's not just that they need to be big neural networks is that specifically they need to be deep or to have a lot of hidden layers. So why is that? Let's look for a couple examples and try to gain some intuition for why deep networks might work well. So first, what is a deep network computing if you're building a system for face recognition or face detection, here's what the deep neural network could be doing. Perhaps you input a picture of a face, then the first layer of the neural network you can think. Of as maybe being a feature detector or an edge detector. In this example, I'm plotting what a neural network with maybe 20 hidden units might be trying to compute on this image with the 20 hidden units visualized by these little square boxes. So for example, this little visualization represents a hidden unit that's trying to figure out if you know where the edges of that orientation in the image. And maybe this hidden unit might be trying to figure out where are the horizontal edges in this image. And when we talk about convolutional networks in a later course, this particular visualization will make a bit more sense. But informally, you can think of the first layer of the neural network as looking a picture and trying to figure out where are the edges in this picture. Now let's think about where the edges in this picture. By grouping together pixels to form edges, it can then take the detected edges and group edges together to form parts or faces. So for example, you might have a low neuron trying to see if it's finding an eye or a different neuron trying to find that part of the nose. And so by putting together lots of edges, it can start to detect different parts of faces. And then finally, by putting together different parts of faces, like an eye or a nose or an ear or a chin, it can then try to recognize or detect different types of faces. So intuitively, you can think of the earlier layers of a neural network as detecting simple functions like edges and then composing them together in the later layers of a neural network so that they can learn more and more complex functions. These visualizations will make more sense when we talk about convolutional nets. And one technical detail of this visualization, the edge detectors are looking in relatively small areas of an image, maybe very small regions like that. And then the facial detectors can look at maybe much larger areas of the image. But the main intuition, when you take away from this is just finding simpler things like edges and then building them up, composing them together to take more complex things, like an iron nose and then composing those together to find even more complex things. And this type of simple to complex, hierarchical representation, or compositional representation applies in other types of data than images and face recognition as well. For example, if you're trying to build a speech recognition system, it's hard to visualize speech, but if you input an audio clip, then maybe the first level of a neural network might learn to detect you know, low level audio waveform features such as, is this tone going up? Is this going down? Is it white noise or sibilant sound like, what is the pitch? You can do that detect low level waveform features like that, and then by composing low level waveforms, maybe you'll learn to detect basic units of sound. So in linguistics, they call phonemes. But for example, in the word cat, the cup is a phoneme. The up is a phoneme. The term is another phoneme, but learns to find maybe the basic units of sound, and then composing that together, maybe you learn to recognize words in the audio, and then maybe compose those together in order to recognize entire phrases or sentences. So deep neural network with multiple hidden layers might be able to have the earlier layers learn these lower level, simpler features and then have the later deeper layers then put together the simpler things is detected in order to detect more complex things like recognize specific words or even phrases or sentences that you're uttering in order to carry out speech recognition. And what we see is that whereas the early layers are computing what seems like relatively simple functions of the input, such as where the edges by the time you get deep in the network, you can actually do surprisingly complex things such as detect faces or detect words or phrases or sentences. Some people like to make an analogy between deep neural networks and the human brain, where we believe, or neuroscientists believe, that the human brain also starts off detecting simple things like edges in what your eyes see, and it builds those up to detect more complex things, like the faces that you see. I think analogies between deep learning and the human brain are sometimes a little bit dangerous, but you know, there is a lot of truth to this being how we think the human brain works, and that the human brain probably detects simple things like edges first, and it puts them together to form more and more complex objects. And so that has served as a loose form of inspiration for some deep learning as well. We'll say a bit more about the human brain, about the biological brain, in a later video this week. The other. Other piece of intuition about why deep networks seem to work well is the following. So this result comes from circuit theory, which pertains to thinking about what types of functions you can compute with different AND gates and OR gates and NOT gates, basically logic gates. So informally, the functions compute with a relatively small but deep neural network. And by small I mean the number of hidden units is relatively small, but that if you try to compute the same function with a shallow network, so beyond allow enough hidden layers, then you might require exponentially more hidden units to compute. So let me just give you one example and illustrate this a bit informally, but let's say you're trying to compute the exclusive OR or the parity of all the input features. You're trying to compute x, 1x or x 2x or x3 XOR up to x n if you have n or NX features, so if you build an XOR tree like this, right? So first compute the XOR of x1 and x2 then take x3 and x4 and compute their XOR. And technically, if you're just using AND OR NOT gate, you might need a couple layers to compute the XOR function, rather than just one layer, but with a relatively small circuit, you can compute the XOR, right, and so on. And then you can, you know, build really an XOR free, like so, until eventually you have a circuit here that outputs, you know, the always call this y, that outputs y, hat equals y, the exclusive, or the parity of all of these invertebrates. So to compute the XOR, the depth of the network will be on the order of log n, right? And this type of fix or tree, so the number of nodes and the number of circuit, circuit components, or the number of gates in this network, is not that lot. You don't need that many gates in order to compute the exclusive OR But now, if you're not allowed to use a neural network with multiple hidden layers, with, in this case, order log n, hidden layers, if you're forced to compute this function with just one hidden layer, right? So you have all these things going into, you [37960:32:00] 0: know, set of hidden units, and then these things, then [37979:01:20] 0: outputs y, then in order to compute the parity of x, or to compute this XOR function, this hidden layer will need to be exponentially large, because, essentially, you need to exhaustively enumerate all two to the n possible configurations, or the order of two to the n possible configurations of the input bits that result in the exclusive or being either one or zero. So you end up needing a hidden layer that is exponentially large in the number of bits. I think technically you could do this with two to the n minus 1 billion years, right? But that's the order two to the N exponentially large in the number of bits. So I hope this gives a sense that there are mathematical functions that are much easier to compute with deep networks than with shallow networks. I have to admit, I personally found the result from circuit theory less useful for gaining intuitions, but this is one of the results that people often cite when just when explaining the value of having very deep representations. Now, in addition to these reasons preferring deep neural networks, to be perfectly honest, I think the other reason the term, term deep learning, has taken off is just branding, right? These things used to be called neural networks above all the hidden layers, but the phrase deep learning, you know, it's just a great brand. It just is so deep, right? So I think that once that term called on that really neural networks rebranded, or neural networks with many hidden layers rebranded help to capture the popular imagination as well, but regardless of the PR branding, deep networks do work well. Sometimes people go overboard and exist on using tons of hidden layers. But when I'm starting out with a new problem, I'll often really start out with even logistic regression, then try something with one or two hidden layers and use that as a hyper parameter. Use that as a parameter, a hyper parameter that you two in order to try to find the right depth for your neural network. But over the last several years, there has been a trend toward people finding that for some applications, very, very deep neural networks with maybe many dozens of layers sometimes, can sometimes be the best model for a problem. So that's it for the intuitions for why deep learning seems to work. Well, let's now take a look at the mechanics of how to implement not just forward propagation, but also back propagation. So. [38702:24:00] 0: In the earlier videos from this week, as well as from the videos from the past several weeks, you've already seen the basic building blocks of board propagation and back propagation, the key components you need to implement a deep neural network. Let's see how you can put these components together to build a deep net. Here's a network with a few layers. Let's pick one layer and look at the computations focusing on just that layer for now. So for layer l, you have some parameters, WL and BL, and for the forward prop, [38861:14:40] 0: you will input [38875:22:39] 0: deactivations a, l minus one from the previous layer and output Al. So the way we did this previously was you compute Z, L equals W L times al minus one plus B L, and then al equals g of z L. So that's how you go from the input al minus one to the output Al. And it turns out that for later use, it'll be useful to also cache the value Z L. So let me include this cache as well, because storing the value Z L will be useful for backward for the back propagation step later, and then for the backward step, for the back propagation step again, focusing on computation for this layer l, you're going to implement a function that inputs da of L [39171:22:40] 0: and outputs da [39187:38:40] 0: l minus one, [39196:58:40] 0: and just to fetch All the details. The input is actually d a l as well as the cache. So you have available to you the value of Z L that you computed. And then, in addition to outputting d a l minus one, you're going to output the gradients you want in order to implement gradient descent for learning. Okay, so this is the basic structure of how you implement this forward step, when we call the forward function, as well as this backward step, which we call a backward function. So just to summarize, in layer l, you're going to have the forward step or the forward proper forward function, input al minus one, and output Al. And in order to make this computation, you need to use WL and BL and also output a cache which contains ZL and then the back way function, using the backprop step, will be another function that now inputs da of L and outputs da l minus one. So tells you, given the derivatives respect to these activations. That's D, A of L, what are the derivatives? Or how much do I wish al minus one changes and computed derivatives respect to the activations from the previous layer. Within this box, you need to use W, L and B, L, and it turns out, along the way, you end up computing D, L, and then this box. This backward function can also output D, W, L and D, B, L, but I was sometimes using red arrows to denote the backward iteration. So we prefer these arrows in red. So if you can implement these two functions, then the basic computation of neural network will be as follows. You're going to take the input features a zero, feed that in, and that will compute the activations of the first layer, a, one. And to do that, you need w1, and b1, and then we'll also cache away z1 Now, having done that, you feed that to use the second layer, and then using w2 and b2 you can compute the activations of the next layer, a, two and so on, until eventually you end up outputting a capsule L, which is equal to y, hat. And along the way, we cached all of these values, z, we. So that's the forward propagation step. Now for the back propagation step, what we're going to do will be a backward sequence of iterations in which you are going backwards and computing gradients like so. [40088:48:00] 0: So we're going to feed in here da [40108:21:19] 0: l, and then this box will give us da of l minus one and so on. Until we get da two, da one, you could actually get one more output to compute da zero, but this is derivative with respect to input features, which is not useful, at least for training the weights of these supervised neural networks. So you could just stop it there along the way, backprop also ends up outputting DWL, DBL, and DPL. [40281:20:00] 0: Just used WL and PL, [40302:08:00] 0: this would output D, w3 dB, [40320:16:00] 0: three, and so on. [40348:16:00] 0: So you end up computing all the derivatives you need. [40374:13:20] 0: And so just to maybe fill in the structure of this little bit more, right, these boxes will use those parameters as well. [40434:29:20] 0: And it turns out that we'll see later that inside these boxes will end up computing these Z's as well. So one innovation of training through a neural network involves starting with a zero, which is x, and going through forward crop as follows, computing y hat, and then using that to compute this, and then back prop right doing that. And now you have all these derivative terms. And so you know, W will get updated as w1, is the learning rate times dw, right, put into the layers, and similarly for B, right? Now the completed backprop and have all these derivatives, so that's one iteration of gradient descent for your neural network. Now, before moving on, just one more informational detail, conceptually, it'll be useful to think of the cache here as storing the value of z for the backward functions. But when you implement this, and you see this in the programming exercise, when you implement this, you find that the cache may be a convenient way to get this value of the parameters of w1, b1, into the backward function as well. So the programming exercise you you actually store the cache z as well as w and b. So to store z2, w2, v2, but from an implementational standpoint, I just find this a convenient way to just get the parameters copied to where you need to, need to use them later when you're computing backpropagation. So that's just an implementational detail that you see when you do the programming exercise. So you now see what are the basic building blocks for implementing a deep neural network. In each layer, there's a forward propagation step, and there's a corresponding backward propagation step, and there's a cache to pass information from one to the other. In the next video, we'll talk about who can actually implement these building blocks. Let's go into the next video. [41023:38:40] 0: In the previous video, you saw the basic blocks of implementing a deep neural network, a forward propagation step for each layer and a corresponding backward propagation step. Let's see how you can actually implement these steps. We'll start with forward propagation. Recall that what this will do is input al minus one, and output Al and the cache ZL. And we just said that from implementation point of view, maybe we'll cache WL and BL as well, just to make the functions call a bit easier in the programming exercise. And so the equations for this should already look familiar. The way to implement the forward function is just this equals WL times a, l minus one plus B L and then al equals the activation function applied to Z, and if you want to vectorize implementation, then it's just that [41296:26:40] 0: times a, l minus one [41316:10:40] 0: plus E, with the B adding B being high control costing and. Al equals P, applied element wise to Z. And you remember on the diagram for the fourth step, where we had this chain of boxes going forward. So you initialize that with feeding in a zero, which is equal to x. So initialize this. What is the input to the first one? Right? It's really a zero, which is the input features to either for one training example, if you do one example at a time, or a capital zero, the entire training side, if you are processing the entire training set at a time. So that's the input to the first forward function in the chain, and then just repeating this allows you to compute forward propagation from left to right. Next, let's talk about the backward propagation step. Here you go. Is the input, D, A L and output, D, A L minus one, and D, W, L and D B. Let me just write out the steps you need to compute these things. D z L is equal to d a l, element wise, product with g of l, prime z of L, and then compute the derivatives. D W L equals d z L times a of l minus one. I didn't explicitly put that in the cache, but it turns out you need this as well. And then DBL is equal to dz, l, and finally, da, of l minus one is equal to W, l, transpose times d, z L, okay, and I don't want to go through the detailed derivation for this, but it turns out that if you take this definition for da and plug it in here, then you get the same formula as we had in there previously, for how you compute dz L as a function of the previous dz L. In fact, well, if I just plug that in here, you end up that dz l is equal to W L plus one transpose dz l plus one [41953:20:00] 0: times DL prime [41967:44:00] 0: zero. Zfl. I know this is a looks like a lot of algebra. You can actually double check for yourself that this is the equation we had written down for back propagation last week when we were doing a neural network with just a single hidden layer. And as you reminder, this times this element wise product. So all you need is those four equations to implement your backward function. And then finally, I'll just write out the vectorized version. So the first line becomes d, z L equals E, A L, element wise product with D L, prime of z L, maybe no surprise there, DWL becomes one over m, dz, l times a, l minus one transpose. And then DBL becomes one over m, NP, dot, sum, dz, l, then x is equals one, keep dims equals true. We talked about the user NP, dot sum in the previous week to compute DB and then finally, da, l minus one is WL, transpose times d, z of L. So this allows you to input this quantity, da, over here, [42369:14:40] 0: and output [42380:42:40] 0: D, W, l, D, B, L, the derivatives you need as well as da l minus one, as follows. So that's how you implement the backward function. So just to summarize, take the input x you might have. The first layer maybe has a value activation function, then go to the second layer maybe uses another value activation function goes to the third layer, maybe has a sigmoid activation function, as you do binary classification, and this outputs y hat, and then, using y hat, you can compute the loss, and this allows you to start your backward innovation. I throw the arrow starts. I guess I don't have to change pens too much, where you would then have backup compute, the derivatives. Compute, DW three, DB, three, [42662:34:40] 0: dw, two. DB two, [42680:48:00] 0: DW one, DB one. And along the way you would be computing, I guess the cache would transfer z1 z2 z3 and here you pass back with D a two, and da one, this could compute da zero, but we won't use that, so you can just discard that. And so this is how you implement forward prop and backprop for a three layer neural network. Now there's just one last detail that I didn't talk about, which is for the forward recursion, we would initialize it with the input data, X, how about the backward recursion? Well, it turns out that D, A of L, when you're using logistic regression and using binary classification, is equal to y over a plus one minus y over one minus a. So it turns out that the derivative of the loss function with respect to the output with respect to y hat, can be shown to be equal to this. If you forget calculus, if you look up the loss function L and take derivatives with respect to y hat, with respect to A you can show that you get that formula. So this is the formula you should use for da, for the final layer, capital L. And of course, if you were to have a vectorized implementation, then you initialize the backward recursion, not with this, but with D, a capital A for the layer l, which is going to be the same thing for the different examples, right over a for the first training example, plus one minus y, for the first training example, over one minus A for the first train example, dot, dot, dot, down to your n training example, [43173:30:40] 0: one minus a. So that's how you implement [43192:10:40] 0: the vectorized version. That's how you initialize the vectorized version of back propagation. So you've now seen the basic building blocks of both forward propagation as well as back propagation. Now if you implement these equations, you will get a correct implementation of forward prop and back prop to give you the derivatives you need. You might be thinking, wow, there's a lot of equations. I'm slightly confused. I'm not quite sure I see how this works. And if you're feeling that way, my advice is, when you get to this week's programming assignment, you will be able to implement these for yourself, and they'll be much more concrete. And I know there was a lot of equations, and maybe some equations didn't make complete sense, but if you work through the calculus and the linear algebra, which is not easy, so feel free to try. But that's actually one of the more difficult derivations in machine learning. It turns out the equations wrote down like just the calculus equations for computing the derivatives, especially in fact. But once again, if this feels a little bit abstract, a little bit mysterious to you, my advice is, when you've done the permanent exercise, it will feel a bit more concrete to you, although I have to say, you know, even today, when I implement a learning algorithm, sometimes even I'm surprised when my learning algorithm implementation works, and it's because lot of the complexity of machine learning comes from the data rather than from the lines of code. So sometimes you feel like you implement a few lines of code, not quite sure what it did. But there's almost magically works, and because a lot of the magic is actually not in the piece of code you write, which is often, you know, not too long, it's not, it's not exactly simple, but it's not, you know, 10,000 100,000 lines of code. But you feed it so much data that sometimes, even though I've worked in machine learning for a long time, sometimes it still, you know, surprises me a bit when my learning algorithm works. Because lot of the complexity of your learning algorithm comes from the data rather than necessarily from the writing 1000s and 1000s of lines of code. All right, so that's how you implement deep neural networks. And again, this will become more concrete when you've done the chrome exercise before moving on, I want to discuss in the next video. Want to discuss hyper parameters and parameters. It turns out that when you're training deep nets, being able to organize your hyper parameters well will help you be more efficient in developing your networks in the Next video, let's talk about exactly what that means. I [43908:53:20] 2: simply [43968:10:40] 2: this mistake. Now I. [44675:33:20] 4: COVID, Extreme, [44722:13:20] 2: isolation, isolation. [44891:44:00] 2: Suspended by that [44900:32:00] 2: person I 28 significance [45017:46:40] 1: of his [45077:14:39] 2: arguments. [45244:26:40] 0: I can voice. Because we [45426:24:00] 2: are here This my [45523:17:20] 2: gosh. I think it [45569:14:40] 1: got super real right now, there's so much [45582:02:39] 1: going through my head. [45608:42:39] 1: Just Do just come by show. [45777:46:40] 1: Put this stuff over [45793:46:40] 1: here. Jimmy is doing this stuff Christmas. So he was his beautiful. [45955:33:20] 3: So okay, now squirrel, I am a farm. It's a lot of money I plan [46018:13:20] 2: on every year is different. Ingenuity Do you see my code? I coded that in virtual [46096:10:40] 1: it's like super hard. It smells really good, chocolate. [46222:13:20] 3: Older person will make money more in the nice water system. You hire someone to work our store and then to stay it's a pretty nice trip. See, like this older lady, they don't see what's happening for the training for this nice boy, I've always been driven, competitive and responsibly. It. Don't be there tonight, just gonna see [46513:09:19] 5: Alexa stop, different types [46573:25:20] 0: of mushrooms. I concern my survival philosophy is trying to become part of the fighting sexual i. Being effective in developing your deep newness requires that you not only organize your parameters well, but also your hyper parameters. So what are hyper parameters? Are w and b, and there are other things you need to tell your learning algorithm, such as the learning rate alpha, because you need to set alpha, and that, in turn, will determine how your parameters evolve, or maybe the number of iterations of gradient descent you carry out your learning algorithm has other numbers that you need to set, such as the number of hidden layers. So we call that capital L, or the number of hidden units, such as n1 n2 and so on. And then you also have the choice of activation function. Do you want to use a ReLU or tan h or a sigmoid, or something, especially in the hidden layers? And so all of these things are things that you need to tell your learning algorithm. And so these are parameters that control the ultimate parameters, w and b. And so we call all of these things below hyper parameters, because these things like alpha, the learning rate, the number of iterations, number of vision layers, and so on, these are all parameters that control w and b. So we call these things hyper parameters, because it is the hyper parameters that somehow determine the final value of the parameters w and b that you end up with. In fact, deep learning has a lot of different hyper parameters later in the later course, we'll see other hyper parameters as well, such as the momentum term, the mini batch size, [47238:24:00] 0: various forms of regularization, parameters [47260:16:00] 0: and so on. And if none of these terms at the bottom makes sense yet, don't worry about it. We'll talk about them in the second course, because deep learning has so many hyper parameters, in contrast to earlier areas of machine learning, I'm going to try to be very consistent in calling the learning rate alpha, a hyper parameter, rather than calling a parameter. I think, in earlier eras of machine learning where we didn't have so many hyper parameters, most of us used to be a bit sloppier and just call alpha a parameter. And technically, alpha is a parameter, but it's a parameter that determines the real parameters. I've tried to be consistent in calling these things like alpha, the number of iterations and so on, hyper parameters. So when you're creating a deep net for your own application, you find that there may be a lot of possible settings for the hyperparameters that you need to just try out. So apply. Deep learning today is a very empirical process where often you might have an idea, for example, you might have an idea for the best value for the learning rate. You might say, well, maybe alpha equals 0.01 I want to try that. Then you implement it, try it out, and then see how that works. And then based on that outcome, you might say, You know what, I've changed my mind. I want to increase the learning rate to 0.05 and so if you're not sure what's the best value for the learning rate to use, you might try one value of the learning rate, alpha, and see the cost functions J go down like this. Then you might try a larger value for the learning rate, alpha, and see the cost function go up and diverge. Then you might try another version and see it go down really fast. We converge the higher value. You might try another version and see it, you know, see the cost function J do that. Then after trying to set the values, you might say, Okay, it looks like this. The value of alpha gives me a pretty fast learning and allows me to converge to a lower cost function. James, I'm going to use this value of alpha. You saw the previous slide that there are a lot of different hyper parameters, and it turns out that when you're starting on a new application, I actually find it very difficult to know and values exactly what's the best value of the hyper parameters. So what often happens is you just have to try out many different values and go around the cycle, try out some value really, try five hidden layers with this many number of hidden units, implement that, see if it works, and then iterate. So the title of the slide is that applied deep learning is very empirical process, an empirical process is maybe a fancy way of saying you just have to try a lot of things and see what works. Another effect I've seen is that deep learning today is applied to certain problems ranging from computer vision to speech recognition to natural language processing. So a lot of structured data applications such as maybe on an app. Advertising or web search or product recommendations and so on. And what I've seen is that first, I've seen researchers from one discipline, any one of these, try to go to a different one, and sometimes the intuitions about hyper parameters carries over, and sometimes it doesn't. So I often advise people, especially when starting on a new problem, to just try out a range of values and see what works. And the next course will see a systematic way. We'll see some systematic ways for trying out a range of values. And second, even if you're working on one application for a long time, maybe you're working on online advertising, as you make progress on the problem, it's quite possible that the best value for the learning rate, the number of hidden units and so on, might change. So even if you tune your system to the best value of hyper parameters today, it's possible you find that the best value might change a year from now, maybe because the compute infrastructure, be it CPUs or the type of GPU you're running along or something has changed. But so maybe one rule of thumb is every now and then, maybe every few months, if you're working on a problem for an extended period of time, for many years, just try a few values for the hyper parameters and double check if there's a better value for the hyper parameters. And as you do so, you slowly gain intuition as well about the hyper parameters that work best for your problem. And I know that this might seem like an unsatisfying part of deep learning, that you just have to try a lot of value for these hyper parameters, but maybe this is one area where deep learning research is still advancing, and maybe over time, we'll be able to give being effective in developing your deep neural nets requires that you not only organize your parameters well, but also your hyperparameters. So what are hyperparameters? Let's take a look. So the parameters your model are w and b, and there are other things you need to tell your learning algorithm, such as the learning rate, alpha, because you need to set alpha, and that, in turn, will determine how your parameters evolve, or maybe the number of iterations of gradient descent you carry out. Your learning algorithm has other numbers that you need to set, such as the number of hidden layers. So we call that capital L, or the number of hidden units, such as n1 n2 and so on. And then you also have the choice of activation function. Do you want to use a ReLU or tannic, or a sigmoid or something, especially the hidden layers? And so all of these things are things that you need to tell your learning algorithm. And so these are parameters that control the ultimate parameters, w and b. And so we call all of these things below hyper parameters, because these things like alpha, the learning rate, the number of iterations, number of hidden layers, and so on, these are all parameters that control w and b. So we call these things hyper parameters, because it is the hyper parameters that somehow determine the final value of the parameters w and b that you end up with. In fact, deep learning has a lot of different hyperparameters later, in the later course, we'll see other hyperparameters as well, such as the momentum term, the mini batch size. As my children became older more times than myself, myself, various forms of regularization, parameters [49015:06:40] 0: and so on. And if none of these terms at the bottom make sense yet, don't worry about it. We'll talk about them in the second course. Because deep learning has so many hyper parameters in contrast to earlier areas of machine learning, I'm going to try to be very consistent in calling the learning rate, alpha a hyper parameter, rather than calling the parameter. I think in earlier areas of machine learning, where we didn't have so many hyper parameters, most of us used to be a bit sloppier and just call alpha a parameter. And technically, alpha is a parameter, but it's a parameter that determines the real parameters. I've tried to consistent in calling these things like alpha, the number of iterations and so on, hyperparameters. So when you're training a deep net for your own application, you find that there may be a lot of possible settings for the hyperparameters that you need to just try out. So apply deep learning today is a very empirical process where often you might have an idea, for example, you might have an idea for the best value, for the learning rate. You might say, well, maybe alpha equals 0.01, I want to try that. Then you implement it, try it out, and then see how that works. And then. Based on that outcome, you might say, You know what, I've changed my mind. I want to increase the learning rate to 0.05 and so if you're not sure what's the best value for the learning rate to use, you might try one value of the learning rate alpha, and see the cost function J go down like this. Then you might try a larger value for the learning rate alpha, and see the cost function blow up and diverge. Then you might try another version and see it go down really fast and converge to higher value. You might try another version and see it, you know, see the cost function. J, do that. Then after trying to set the values, you might say, Okay, it looks like this. The value of alpha gives me a pretty fast learning and allows me to converge to a lower cost function. J, so I'm going to use this value of alpha you saw in the previous slide that there are a lot of different hyperparameters. And it turns out that when you're starting on a new application, I actually find it very difficult to know in advance exactly what's the best value of the hyperparameters. So what often happens is you just have to try out many different values and go around the cycle, try out some value, maybe try five hidden layers with this many number of hidden units, implement that, see if it works, and then iterate. So the title of the slide is that applied deep learning is very empirical process, and empirical process is maybe a fancy way of saying you just have to try out model things and see what works. Another effect I've seen is that deep learning today is applied to so many problems ranging from computer vision to speech recognition to natural language processing to a lot of structured data applications such as maybe online advertising or web search or product recommendations and so on. And what I've seen is that first, I've seen researchers from one discipline, any one of these, try to go to a different one, and sometimes the intuitions about hyper parameters carries over, and sometimes it doesn't. So I often advise people, especially when starting on a new problem, to just try out a range of values and see what works. And the next course will see a systematic way. We'll see some systematic ways for trying out a range of values. And second, even if you're working on one application for a long time, maybe you're working on online advertising, as you make progress on the problem, it's quite possible that the best value for the learning rate, the number of hidden reasons and so on, might change. So even if you tune your system to the best value of hyper parameters today, it's possible you find that the best value might change a year from now, maybe because the compute infrastructure, be it CPUs or the type of GPU you're running along or something has changed. But so maybe one rule of thumb is every now and then, maybe every few months, have been working on a problem for an extended period of time, for many years, just try a few values for the hyper parameters and double check if there's a better value for the hyper parameters. And as you do so, you slowly gain intuition as well about the hyper parameters that work best in your problem. And I know that this might seem like an unsatisfying part of deep learning that you just have to try a lot of value for these hyper parameters. But maybe this is one area where deep learning research is still advancing, and maybe over time, we'll be able to give better guidance for the best hyper parameters to use. But it's also possible that, because CPUs and GPUs and networks and data sets are all changing, it's possible that the guidance won't convert for some time, and you just need to keep trying out different values and evaluate them on a hold on trans validation set or something, and pick the value that works for your problem. So that was a brief discussion of hyperparameters. In the second course, we'll also give some suggestions for how to systematically explore the space of hyperparameters, but by now, you actually have pretty much all the tools you need to do the prone exercise before you do that. Just to share with you one more set of ideas, which is, I often ask, what does deep learning have to do with the human brain? [50412:58:40] 5: I to learn. It only got [50450:24:00] 4: so much weight to lose so much energy. Gotta figure out how to get this food. [50514:45:20] 0: So what does deep learning have to do with the brain? At the risk of giving away the punchline, I would say, not a whole lot. But let's take a quick look at why people keep making the analogy between deep learning and the human brain. When you implement a neural network. This is what you do, forward prop and back prop. And I think because it's been difficult to convey intuitions about what these equations are doing, really great and descends on a very complex function. The analogy that is like the brain has become really an oversimplified explanation for what this is doing. But. Simplicity of this makes it kind of seductive for people to just say it publicly, as well as media to report it, and certainly called the popular imagination. And there is a very loose analogy between, let's say, a logistic regression unit with a sigmoid activation function. And here's a cartoon of a single neuron in the brain. In this picture of a biological neuron, this neuron, which is a cell in your brain, receives electric signals from other neurons, x1 x2 x3 or maybe from other neurons, A, one, a, two, A, three. There's a simple thresholded computation, and then if this neuron fires, it sends a pulse of electricity down the axon, down this long wire, perhaps to other neurons. So there is a very simplistic analogy between a single logistic unit, between a single neuron in the neural network, and a biological neuron like that shown on the right. But I think that today, even neuroscientists have almost no idea what even a single neuron is doing. A single neuron appears to be much more complex than we are able to characterize with neuroscience, and while some of what it's doing is a little bit like logistic regression, there's still a lot about what even a single neuron does that no one, that no human today understands, for example, exactly how neurons in the human brain learns is still a very mysterious process, and it's completely unclear today whether the human brain uses an algorithm does anything like back propagation or gradient descent, or if there's Some fundamentally different learning principle that the human brain uses. So when I think of deep learning, I think of it as being very good at learning, very flexible functions, very complex functions, to learn X to Y, mappings to learn input to output, mappings in supervised learning. And whereas deep is like the brain analogy, maybe that was useful once. I think the field has moved to the point where that analogy is breaking down, and I tend not to use that analogy much anymore. So that's it for neural networks and their brain. I do think that maybe the field of computer vision has taken a bit more inspiration from the human brain than other disciplines that also apply deep learning, but I personally use the analogy to the human brain less than I used to. So that's it for this video. You now know how to implement forward prop and back prop ingredient design, even for deep neural networks. Best of luck with the programming exercise, and I look forward to sharing more of these ideas of you In the second course You [51613:25:20] 5: results of our hogs. [51644:26:40] 2: Results about hockey, [51679:22:40] 2: Puck, it is never hurt anybody I.