1. Home
  2. Explore
  3. Computer Science
  4. Backpropagation and Neural Networks

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Backpropagation and Neural Networks

Transcript

On Monday we discussed the new network. We did we define a definition um of uh some components of the networks you know and some of the um typical new networks espec es especially for computer vision. Um we will uh introduce the new network architectures for language models. Um when we talk about language models and today we're going to talk about back propagation. Um as we said last time this is the algorithm to compute the gradient of the loss function with respect to parameters. So maybe just a very very brief review. So the background you need from last lecture was that you know there is a function let's call it the loss [clears throat] function or the cost function which is the average of the cost function um of individual examples. So each example I has a cost function and this cost function is something like maybe a loss maybe cross entropy loss for example on some model output h theta of x and uh some label y and maybe the ice input and

ice label something like this. So, and when you run SGD, the most simplistic version of SGD is that you update the parameter theta to theta minus some EA times the gradient uh on some example maybe say I sub K theta where I sub K is sampled randomly from uh the data set. So something like this and uh the the key focus of this lecture is that how do we compute the gradient this object how do we compute the gradient efficiently um with respect to the uh the parameters

right so we know how to compute this last time suppose you have defined the model by combining many of the components it's a multi-layer MLP you know with you know mification with some activation functions you know many many layers of that it's a very complex network. That's what this guy is. And how do you compute a gradient of this function, right? This is just the same as this and same as this one. And how do you compute gradient with respect to the parameter theta? So that's the um the the whole focus of this lecture which is kind of like the um actually this kind of questions you know is one of the kind of like the most technical questions you can ask in deep learning these days because all the other things you cannot really objectively answer in many cases like how do you tune in rate how do you design your data sets all of those are kind of like kind of like a somewhat kind of like a subjective in some sense um so so that's why I think there was a

long time when um all of these kind of like research labs when they interview people they ask how you do back propagation I think that's probably still the case for some of the frontier labs I don't know exactly um but so so this is some this is kind of like a non-trival algorithm that um that is really algorithm that is needed for every training jobs uh under the hood even though that's you you don't you don't really have to do it yourself these days it's called back propagation or auto differentiation um it means that it's automatable right so you don't really have to do it yourself but uh but I think it's it's something that is good to understand um um um because you know some of these kind of like other decisions you make depends on fundamentally how it works to some degree. So that's the that's why we're discussing it. Okay. So um so I think it's kind of like a so we're going to start with a um a more generic view to think about how do you take gradients in

general when you have a kind of like a circuit or kind of a very general function um and and we're going to apply that kind of like a um uh to uh to new networks. So basically this kind of like auto differentiation is not specific to uh new networks. you can use it for almost every nonlinear functions as long you can define the nonlinear functions in the forward path like as long as your function can be computable um uh in a specific way um when you when you when you compute the function value then you can comput the gradient so that's what I'm going to write um here in a in a kind of like um in abstract uh way so let's say suppose you define some let's say differentiable circuit

so I'm I'm using this as kind of like a you know maybe dispens dis uh differentiable networks. So let's say this is kind of like a basically um a network that is composition of of you know um sequence of arithmetics operations um and um maybe other elementary functions.

>> [laughter] >> So arithmetic operations means like for example plus minus division you know so and so forth. Um and elementary functions could be something like you know maybe cosine s you know maybe exponential you know cosine s you know the exact definitions here don't really matter I'm just trying to maybe rel this what I mean by elementary function sigmoid so basically you can say you know have a input x1 you know in the circuit kind of world people drawing this things like this where you have input x1 x2 then you have some gate plus then you you do the compute the sum of And then maybe you can have x3 and then you can have a a gate which is kind of like uh a gate or operation which is like maybe times and then you compute this means that you do x1 + x2 * x3 and maybe you can reuse x1 * x2 here maybe let's say you can do plus then you just add another okay so then this one is x1 plus x2 plus x4 and then you can probably aggregate

again so and so forth. So, so you can see that the difference between this and a formula is that when you have a circuit, you can reuse some parts. But if you have a formula, you have to expand everything. So, if x1 plus x2 is used twice, then you have to write x1 plus x2 twice in your formula. But in a circuit science, you don't have to re recmp compute it again. So, that's the only minor difference. Anyway, this is just um some kind of a computational graph. In some sense, the exact detail don't matter. The claim here is actually uh pretty profound which is um so so the theorem here um of course is informally stated theorems because you know if you really want to make it formal you have to specify exactly what all of this kind of composition really means then you have to go through a lot of dragons which is not necessary for this lecture. So the theorem is that suppose um let's say a differentiable circuit

um of size n and here what does size mean? Size means that how many kind of operations you have or how many of these operations you have. So and size n uh computes um a real value function. So I'm only thinking about a circuit where there's only one output um a scalar output because that simplifies my life a little bit. So a real fun real value function let's say this function is f. So this is what the circuit's job right is computing of a function that maps l input to one scalar. Suppose you have such a function which is computable by this differentiable circuit. Then um the gradient

the gradient let's say gradient of f with respect to the input. So first of all maybe let's uh look at a type signature here. What's the gradient dimension of the gradient? You have a a function that maps all inputs to one input. The gradient is a r all dimensional vector right? because for each coordinate you have one partial [snorts] gradient. So this is a L dimensional vector and this can also be computed

um in time say O of N. So you don't have to spend much more time uh buy a circuit uh of size of it. [snorts] So by the way you know when I talk about time and cir here I'm implicitly assuming that each of these very basic operation takes off one time. it takes one unit of time and that's so that's why to compute a function you just go through this circuit you because the size is n it takes of all time right to compute a function and I'm claiming that if you have a such a f circuit that

computes the function f then the gradient of this circuit of this function can also be computed in similar time and also the gradient itself is representable by the circuit which is also an interesting fact so um so in some sense this is saying Right. Um okay in deep learning people call computing of this called as people call it forward pass which just means a computer function of the model you want to you know evaluate and the computer gradient is called backward pass that

just a different uh terminology. So and if you use terminology it means that the forward pass and the backward pass have the similar time. So if you can do the forward pass in all of time then you can do the backward pass in of time because all of these new artworks are some circuits right all the matrix application you can write them as a circuit because they are all elementary operations in matrix application all of these activation functions are elementary functions um and and every every component people are using are a combination of elementary functions. So all the networks are this kind of differentiable circuits and if the forward pass the function f can be computed in some time n then the backward pass can also be done in a similar time. So um so maybe more precisely I guess I'll just write down what I'm trying to say here. So in the in deep learning so this function f you know the function f here corresponds to the loss function right on some example maybe i

the variable x corresponds to theta because theta is what you really care about as the input to this function and uh and what is l which which is the number of inputs of the function that's the number of parameters because because my variable here is really theta and uh and interestingly for new works you know at least from almost all new artworks people are defining these days you know maybe there are some small exceptions in almost all cases the the number of operations you need to

evaluate new networks is proportional to the number of parameters in your network so for example you have a matrix right you have say um matrixification with dimension D byD right how many parameters you have you have D square parameters and how many operations you need to do the matrix vector product you also need these square operations so so in neur networks it's kind of like the number of operations you need is almost always equals to proportional to number of parameters [gasps]

um and and that means and and then using this theorem you know the gradient of f corresponds to the gradient with respect to the loss. Sorry, the gradient of the loss with respect to the parameters and and it's saying that this one can also be done in time of n which is the number of parameters. So basically both the forward pass evaluating the loss and the gradient can be done in big O of the number of parameters and that's the the fundamental complexity of computing these gradients um

yeah and um and in most of the I think the main job of this lecture is to show you how to do this you know how to prove this theorem in some sense but we are going to prove it in the in the most abstract sense because that's kind of too abstract we're going to prove it in kind of like a language that is similar to the neur works so that it's easier to understand. Um but the but the abstract theorems hold and maybe before going that going into that let's also see some in my opinion some implic also advanced application of this kind of like idea. So so this idea or this theorem is also the basis of some of the second order methods um people are using. So what do I mean by that is it means that you know you can actually apply these kind of things twice and then compute some of the second order gradient some form of second order gradient you cannot compute everything but you can compute some of them for example let's say suppose um you have this loss function um I

guess maybe let's use L of theta as the loss so this is a loss function um and then maybe I should use J just to to be consistent with the notation we have so let's say we have a loss function A and uh if you sometimes you say okay let let me let me first do one step descent I do this and then I evaluate a loss. So basically I take some parameter I take one step of gradient descent and then evaluate the loss of this. So if you want to comput the gradient of this with respect to theta suppose you

say I I do the def this d theta right I computed the so this is a this is itself a function of theta right because theta shows up twice you and you can differentiate it through respect to theta and this is still efficiently do computable and why the reason is that computing this function as a function of theta is is efficient computable Because you can compute theta, you can comput the green of theta, you can comput a loss, each of these three steps are efficient computable because you just

pay three times the number of parameters, right? And because this function itself is computable efficiently, then the gradient is also computable and that's just you use it twice in some sense. Um, and another application is that suppose you define um, let me make sure I use the right notation. So let's say suppose you define another one. Um but by the way this kind of things is often used in some of these metalarning uh situation where um I guess it's probably hard to explain

exactly but you know just in case you are familiar with some of this like or maybe you see you'll see it later. So you can try to run an algorithm you know you can try to kind of like run some tuning algorithm and you can uh for example tune your um learning rate by back prop back propagating through the the algorithm. So you can tune for example you can tune your uterization u um and and the loss function for theization is that if I run this algorithm for 10 steps what's my results

right that's your final loss function which is defined by an algorithm and you can still optimize that loss function even the algorithms involved in the defining the on the loss function and this is used in some of this test time training stuff um um I guess you know it's very hard for me to say you know without context but some this kind of ideas is useful for uh for some advanced cases and another case that is useful is that for example you can define g say you can define gx to be say the gradient of f in a product with some vector v let's say you just do this and then you comput the gradient of g [snorts] it turns out that gradient of g is equals to this is just a basic kind of like a calculus or kind of like a differentiation is equals to the heisen of f which is a matrix times the vector v. So and um you know that this is efficiently computable right in all of time you know that inner product is efficiently computable. So you know G

itself is efficiently computable and because G is computable efficiently then the grain of G is also efficiently computable and that's that means that the hash vector product is efficiently computable. So this is the hashing this is just a vector. So you can compute the product of a hashing times the vector any vector in a very efficient way in all time but you cannot actually compute this matrix itself in the same time because this matrix itself is too big it's n byn right you have so many uh

dimensions um but u but this vector hyen vector product can be computable um efficiently okay so that's um all about the abstract stuff and then now let's try to kind of like u see how it works with ne works and you know I'm still stay you know half abstract half kind of like concrete um any questions before I continue

okay so the core idea uh for the back propagation is that you use this so-called chain rule so um let's um review what chain rule is I Most people know chain rule but I think it requires some kind of like different interpretations of it or different presentation of it. So to make it understandable uh in our context but maybe question yeah >> some optimizers they use like in this case are we only going to commute like the first sorry can you say the sentence you know ask the question again I don't understand the question you said that there's some algorithm is second derivative >> like is this only for optimiz

doesn't use second order derivatives. I think you need a pretty advanced optimizers to use the second order derivatives. Uh and you know I think most of the optimizers these days you know that are really useful don't have to actually compute a hashing vector product. Yeah. like >> so that that's what I tell you about right like so so so I didn't tell you how to compute it I'm just saying it's computable but how to compute it that's the that's the whole job of the rest of the lecture

so um okay so let's talk about chain rule right so this is how we are going to interpret chain rule in this context which I think is probably the easiest way to think about it so um Let's say you have maybe a um this is Z is the input. Let's say you have M dimensional vector which is the input. Um and you have a function let's say U. You can call this function U sorry you have a variable U which is equals to G a function G applied on Z [sighs] and let's say this is a dimension N.

And then later you have another function that applied on u and make a scalar let's say r. So basically if you draw it you say you have z and it go through some kind of like function g and then you get this intermediate variable u and then you go through some function f and then you get some scalar output j. So what chain rule does you know and our interpretation is that so chain rule basically give you away give you away such that if you are given the gradient with respect to U right the

gradient of the final output with respect to the intermediate variable U so given this so chain rule will try to compute chain rule is this formula that give give you the gradient with respect to Z. So basically if you know the the the the gradient with respect to the the intermediate variable you somehow know the gradient with respect to Z. So um so basically in some sense it's kind of like it's how you back prop right. So if you imagine there's a chain of kind of these operations, you can

compute the gradient with resp to the first variable and then you can apply this iteratively to get the gradient with respect to the second variable and gradient respect to third variable. Right? So if you have a chain of like this so that's kind of like the the main idea but we are trying to kind of abstract this in a way that you only have two variables U and Z. U is the output of G and the Z is the input of G. And then when you comput a gradient, you start with the gradient with respect to U with respect to the output of G and then you can use that to compute the gradient with respect to the input of G which is Z. [gasps and sighs] Okay. So and and this is actually the exact same chain you have learned in calculus right so like so so basically if you want to compute the gradient the partial derivative with respect to one of the variable Z1 ZI let's say. So how do you do chain rule? You say these are intermediate variables. So I'm going to expand take a sum over all possible

intermediate variables u1 up to u n I have n variables here. So i from 1 to n and I take the partial derivative of my final target with respect to u let me say sorry let's use j here to not be confused with i. So the the partial derivative of J with respect to UI one of intermediate variable and then the partial derivative of G J with respect to ZI GJ is the same as UI right GJ is the J output which is equals to UI so so that's pretty much uh how you do it and

if you think about this right so these things are these guys right and this is these So, and this so so this equation is saying that DJ over DZ is a linear combination of DJ over DU. So basically the quantities you care about is a linear combination of the quantity you are given and and you can rewrite that as a matrix multiplication. So if you reorganize this, you say DJ over DZ1 up to DJ over D Z M. You organize this into a vector. Um then this is actual mification

where the matrix is some is the Jacobian is actually transpose of Jacobian and um and this is the the the input you're given right which is the DJ over DU's and this matrix is I need to make sure I call a P selection to not mess up the indices.

So this is actually the transpose of the Jacobian and uh and this is what you are given right in our formulation right. So you are given this and you you multiply a matrix you get the derivative with respect to uh Z right. So yeah and you can see that this computation one magic here is that this computation chain rule [sighs] only need information

info about G the function G you have to know what the function G is you have to know the variable Z what the value Z you are evaluating the gradient at right but You don't have to but not f but not f. So f is completely out of this picture even though we have f here right which is but but what f is really is doesn't really matter and that's how you make it efficient in some sense. You can forget about what f is the only thing you have to remember is this thing dj over du. So

even f is a very very complex function right. Let's say f is like actually a sequence of like you know f_sub_1 f_sub_2 so on and so forth it doesn't really matter as long as I know dj over du I can forget about how I got all of this kind of all of the what happens here doesn't matter at all so that's the magic so sometimes it's marovian in some sense right you can forget about the past when you when you try to compute the gradient respect to the the first variable z right maybe it's a sequence of operation right for example you have z you know B, F_sub_1, say F_sub_2, F_sub_3, so on and so forth, right? And until you have final layer scaler and uh and uh I guess here the output of okay, I guess I'm maybe that's it. So I I'm just redrawing this in a in a slightly better way. So there's a very complex computation here to get the final scaler. [sighs and gasps] But if you want to you only have to know once you know this thing dj over du you can forget about

everything above and you can get the dj over dz. So this part doesn't have to require the history. So in some sense you make a marovian and you can forget about history in some sense and that's why it's efficient. Um so and I think um in some sense this mapping from here to here is the so-called backward function. uh if you look at a petarch like this is the for every module you have to implement a backward function because this module is a so so this is a this

function itself is only depends on G and Z right so you only have to know the local information to implement the the module um um and and it can compose with anything uh here so and this is the backward function and and the G itself is so is so-called forward function so maybe just to make it more concrete Um let's just uh try to think about this you know um in some kind of like we have a composition of chain rules right so let's suppose you have so maybe back prop which is kind of

like basically comp composing the chain rules in the most um natural way okay so maybe let's just consider this the following situation right where you have say the final target is let's say a repeated application of many modules many functions let's say you have this maybe I should just draw this once so that I don't have to draw it let's say you say you have x which is mapped to m you go through um um you go through some kind of modules m1 and then you say you get u one and

then you go through some either modules or functions I as sometimes I call it modules because in pytor it's called modules but it's really just a function and then you get u2 and then maybe you get you go to another function m3 um and then let's say you get the loss something like this so and uh how does so you do this forward computation and how do you do the backward computation the first thing you do is you compute so step one you compute DJ over D U2. And this is how do you do this? This is

just a very simple. It's local. You just do some kind of like differentiation, right? You just you have only one module. It's easy. And now once you have this, then you use this to compute DJ over D U1. And how do you do this? This is the the backward for this M, right? So use chain rule. Right? This is the same thing as we draw here. Right? So you start with you just assume this part as F. Right? So we have the F and G. Right? You assume this part is F and this part is G. Right? So and then

you apply the chain rule on G which is M2. [snorts] Right? I think probably I should have a notation here just to formalize this. So you can probably I think people sometimes in my lecture notes I write this. I say DJ J of this is actual matrix mification and the matrix multiplication is something like is a function of G and Z time DJ over DU. So you apply this backward function which is uh this is a backward function that depends on the m_sub_2 and the variable u1.

So this these two information to compute the back the matrix here which is the transpose Jacobian and then do this uh backward path and you get this one and after you get this one now you want to comput suppose you want to comput DJ over DX right so how do you do this the same way right so now you pretend that [snorts] you pretend that this whole part is my f I don't really care about exactly what f is and and this part is my G and I apply my chain rule. So my chain rule is saying that I can so so this I get DJ over DU which is the the same as this right that's my input and I can get the output which is the DJ over DX so and and and here again you don't have to know anything about F you just have to know something about G uh and the G here is M1 right you just have to apply

this backward transformation which is related to M1 and the variable X um and you apply it this in sometimes is a matrix you apply it on this vector you get a um the the DJ over DX does it make sense and of course you know you can do it with many many layers but here I'm only showing three functions but you can compose it with any number of functions you just keep going backward from the beginning And uh for example you know and another thing is that all of these modules doesn't have to be uh the same right they can be all different right maybe one module here one of this G is machine application and one of these is the uh um uh activation functions activation

functions for example you can have you can say I start with my X I first do a mimification And I got some U1 and then I do a sigma the activation functions. I got U2 and then you can do another mimication and you get U3. Maybe you do a layer norm something like that. You know you can you can do arbitrary number of this and you finally get J. And the same thing you just have to go backward but you know for each of the individual functions you have to use the this uh the different kind of like a

jacobian in some sense to to do the back backward. So basically next you know um we can compute what is the specific backward function this mapping for each of the specific uh operations right um you know there's a oper there's a specific kind of like backward function for the um for the mification there's a specific one for the activation functions each of them has different kind of like jacobian here right um so different kind of like a mapping um so um and we can compute each of them and once Once we have all of them, we just compose and get the backward pass.

Any any questions? [snorts] So, one thing I kind of like I cheat a little bit is that it's not like really cheat, but like what is the variable here we're talking about? It sounds like I'm I'm kind of like trying to tell you that this is the variable I'm trying to differentiate with with respect to, right? But if I draw a new work, do I really care about differentiating with respect to x? I don't really necessarily care about that. Sometimes do, but but generally speaking, I care about the

differentiation with respect to the parameters. So, but actually that's still follows the same thing. The only thing is that you have to think of the variables. Basically this graph of computation becomes more complicated. So what happens is that um so if you have parameters so what happens is that you have maybe let's think about this right this [sighs] uh has parameters let's say theta right let's say theta is the collection of the weight matrices and biases. So

and in some sense the way you think about it is that you um you think of this as like a you can think of this com this function has two input one is theta and one is x. Okay. So and now suppose I already know so I already told you how to so like I already told you how I how we compute DJ over D U one right because that's just a a very simple sequential kind of backward right so and now how do I compute DJ over D theta maybe I even call this theta 1 because each of the this modules each of these multiplication has different parameters How do I comput DJ over D theta 1? It's the same thing. It's just like here you don't view X as v inputs. You view theta as inputs. So then you compute the the the relationship. So basically you want to

find a way to relate this to DJ over D theta 1. But it's the same thing. You just view X as the fixed and you view this function as a function and map theta 1 to U1 and then you compute the gradient with respect to theta 1. So basically the the input the data the raw data versus the parameters they're just different kind of like uh uh um matrices in some sense different kind of like tensors. So, so in some sense I think if you write it abstractly it's really just that this thing is equals to this backward uh function uh which is u depends on the m the mification and you view the setta one as the inputs and then you apply this to u1. So that's how you comput the the the derivative with respect to the parameters. So actually the real um um back propagation actually has this two phase. So uh and these two phase can interle with each other. Maybe let me just try to let me see whether I can um what's the best way for me to draw this?

I think let me erase here just a little challenging for me because um on my nose is vertical and here I need to turn it into horizontal. So okay. So let's say more precisely I think with a lot of like notations um I think what happens is that you have x you have some matri application and there's parameter theta 1 let's say and you map this to maybe u1 and you use maybe a sigma and then you get to u2 and then use another my patient say they say the two and they get to U3 and then you probably get another sigma

so on so forth and eventually you get maybe J um maybe let's write the last one as maturation so let's say you get U K J um something like this and you start with you know DJ over D U K So you you by the way all of these use are the intermediate variables the activations the so-cal activations right they are not parameters and the parameters are folded in the matrium application so the first pass you do you know the first uh phase of pass you do is that the

backward pass is that you try to compute the derivatives with respect to the intermediate variables with respect to the UIs the with respect to the activations so you start with this and then you do a backward to at uh Ukus one and then you do this so and so forth and you get u say three and then you compute the backward of this you get an DJ over D U2 so and so forth right that's the first uh wrong and then once you have this you can have a branch in some sense that compute the

derivative respective parameters for example with from this you and compute and with some information about this you can compute DJ over D theta 1 and uh how do you compute DJ over D theta 2 in some sense you view theta 2 as a so basically so basically you need to go from here to get DJ over D theta 2 because you think of this as a function that map theta 2 to U3 right theta 2 to U3 is the analog of analog of the G here and And you already have the so basically in

some sense I guess I'm repeating myself a little bit but just to make it maximally clear. So you view this part of the things as f in that uh [laughter] abstract way and you say I'm going to view this as a function theta 2 going to you. So you view this part as G and then you that's why how you compute from D J over DU to DJ over D theta 2 and this is like really like some kind of backward function of this animation application I theta 2 and applied multiplied with the DJ 3 something like this. So and and and you can do the same thing here you know maybe here you can do dj over d theta I guess you know k minus 2 you can this will give you dj over d theta k minus one

and you can see that this gradient respect to the so to compute the gradient respect to the first ways you have to go through this chain and then get here, right? Because this this row is what you really care about the the derivative with with respect to the parameters. So to get the last one, you always you have to go compute all the derivatives with respect to activations. But for the first gradient of the uh uh the the gradient with respect to the first parameters, you don't have to go

through this whole chain of calculation. Once you get this, you can get here, right? So, so that's why you can in some sense paralyze this, right? So, like this is in hier sequential, but some of this can be done on the side um in sometimes in a parallel fashion. Um yeah and another thing is that for example if you have computed all of this then you can drop all of the the calculation you have done here so that you can clear up me your memory and then this is a way to

free your memories so and so forth. So, so basically the how you compute this you know affects the um the system perspective a lot because which tensor depends on which which variable or which quantity depends on which quantity right and the dependency between them is very subtle and but but uh but the um um once you understand the dependency you can optimize how you compute them in what order and when you free the memory when you communicate with other GPUs so forth. So basically eventually all of those depends on this computational graph.

[sighs] Any questions? Okay. So now let's let's be even more concrete and and compute what's the the backward uh function for some specific G right so here everything is abstract we have G right so and we have this abstract notation for this backward function which is this transpose of the Jacobian but now let's compute uh for the most concrete things like for mification what's the backwards function for E.

So let's go back to this kind of like uh notation. So let's say suppose you have G which is matrix application. So what does that mean? I think let's assume say G uh uh is equals to W * Z plus a bias. So um and uh let's say you know Z is of dimension I guess I'm using a consistent notation is dimension M w is of dimension say N by M and B is of dimension N um and here you know one thing is that we have to think about you know and then we have some function f right f you know

your final loss function J is you know some f of um oh I guess um probably I should let me use u here so so g this this function is meation but the variable I called u so eventually I have a function f applied on u which is j so and um um as I said you know like you can view this as input or you can view this as input so let's view the z as input and compute so let's say our goal is to say given DJ J over D U we want to compute what is DJ over D Z. So that's uh the first task we are trying to do here. So and uh and how do we do that? We just plug in the the abstract definition right the the computation we had about from the chain rule. So we know that

we know that DJ over DZ is this matrix times DJ over DU and this matrix is that uh is this guy right? So it's this matrix. So let's just plug in uh the values there. So, so here is the first output wz + b the first dimension um differentiate differentiated with respect to first variable right and here this is the the last uh the first uh output differentiated with respect to the the last input so and so forth something like So, so basically this matrix is of the form each entry is DW plus B the JS coordinate which is the J output

differentiate with respect to the I input. So this is the one of the entries here right and let's compute this uh thing right so it's dzi let's expand wz + b um the j entry of wz + b is the you know let's write a bj first and then what is the j entry of wz definition of matriation you enumerate over the inner dimension which is 1 to m I have wj k zk so this is the j coordinate of WZ. And now you want to differentiate this formula with respect to ZI and and you see that many of these

variables has nothing to do with ZI, right? So ZK has no derivative with respect to ZI if K is not equal to I. So it's zero if K is not equal to I. So you can drop all of those terms and the only thing that you that is left is just u and bj has nothing to do with zi. So that's doesn't have derivatives as well. So the only thing that is left is the term when k is equal to i.

And this is a linear function of zi. So the derivative is just a constant in front of the linear terms which is just wji. So basically that this matrix the IGS entry is just equal to WJI. So that means that you can simplify this dramatically right. So if G is the matri of Kation then actually what happens is just that DJ over DZ is just equals Wrpose time DJ over D U. So the backward function is just only a transpose uh you just transpose your forward matrix and you you multiply that and that's it. So

[clears throat] and you can similarly you can compute the the relationship between this and the um and w and b. So next thing we we want to compute is that what's the relationship between this like how does this relate to this right? If you have dj over du then what's the derivative respect with respect to w and that's and w is your parameter that's why you care about that's what we care for example here right so w is your parameter so when you have this then you need to

compute the derivative with respect to the parameter um that's why you need to compute this so that's the next question and this is a little bit more subtle because it's kind of like um um it's just a notation is a little bit complicated So again what we care about is that um we want to know what is let's just compute each of the wig right so and let's apply the chain rule so when you apply the chain rule you enumerate over all the possible uh uis right so you say it's take k from one to

is this m o n let me see Sorry, I think this is N. [snorts] And you do DJ over D U K times U K is really just WZ + B the K coordinate and you that is differentiated with respect to wig. So this is the chain rule. Now I'm not using a mification because it'll be tricky because this thing is kind of like a to to write it because you have so many JS like the the number of like variables you are differentiating with with respect to is kind of like a matrix. It's a two dimensional. So so you need a more kind of like tensor notations to write it in a more compact way. So that's why I'm just doing the the more brute force way and uh but the same thing you just expand this um this thing

and let's look at what this uh means right so this is uh [snorts] the the case coordinate is equals to um you first look at the case coordinate of WZ wz which is uh w K S Z S and then the case co coordinate of B which is BK and you are differentiating this quantity with respect to WIG and the only terms that is not zero is the case when K and S is equals to I and J all other things has nothing to do with I and J right there's no it's all like the derivative is always zero so that's why the only thing that is left

is the I and J term and for the I J term when K is so basically okay let me write them one more step W I J when K is I I is J right ZJ uh [laughter] I guess so when K is not equal to I this is just zero and and that means this sum is going to be gone. So,

[clears throat] so basically this is just when k is equal to i ui times um this derivative the the linear coefficient it's a linear term uh the gradient is the linear coe the co coefficient [clears throat] and the coefficient zi so just zi zj zj so maybe Just rewrite this is equals to so this is very very simple actually this is called hein rule uh in uh

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent