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

Study methods

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

Backpropagation in Neural Networks

Transcript

hi in this module i'm going to talk about the back propagation algorithm for computing gradients automatically it's generally associated with training neural networks but it's actually a far more general algorithm so let's begin with our motivating example which is suppose we're doing regression with a four layer neural network so remember that we do the loss on a given example the loss with respect to a particular example and now we have the weights of the network v1 v2 v3 and w is equal to

and remember the form of the neural network you start with a feature vector you multiply it by some weight matrix that gives you a vector you send it through the activation function repeatedly apply it apply a matrix send it through an activation function um the left multiplied by a matrix center through an activation function you take the final vector you take the dot product with respect to the final weight vector and that gives you your score so this is your prediction subtract

the target value and you square it that gives you your loss so now if you wanted to train this neural network using stochastic gradient descent you would need to compute the gradient of this loss function with respect to each of the parameters so for example would compute the gradient of the loss with respect to v1 that gives you a gradient update which you can then use to update v1 same with v2 3 and w so now you can sit down with this lovely expression and you can just grind through the map

and get the expressions it's straightforward but it's rather tedious another question is how can you get the gradients without all doing all this manual work so the answer to that is computation graphs so here is our loss function again and what we're going to do is write down the computation graph for this mathematics computation graph is a direct acyclic graph whose root node represents the final expression this loss function and each node represents intermediate sub expressions

like one v of x for example now what this computation graph is going to allow us to do is allows us to apply the back propagation algorithm to the computation graph and automatically get gradients out so there's two purposes actually that we're going to do this the first is computing the gradients automatically and this is how deep learning packages like tensorflow and pi torch work behind the hood and second more of all we're going to use this as a tool to

gain insight into the modular structure of the gradients and try to demystify because taking gradients by hand you can lead it into situations where you just have a lot of symbols but using a graph we can start to see the structure okay so our starting point is to think about functions as boxes so imagine you have this expression a plus b and that gives rise to some variable c so i'm going to represent this as a very simple computation graph where you have a and b

and these arrows point into this box that does plus and the result will be labeled as c here okay so now the question is if i change a or b by a small amount how much does c change oh this is just a notion of a gradient so informally we can look at this as a plus b equals c now if i go and fiddle with a a little bit i add epsilon so what happens to the right hand side well on the right hand side i just get plus one epsilon and what i'm gonna do so the gradient of c with respect to a is one

i'm just gonna write it on that so this can be interpreted as kind of amplification or gain if i move a by l a little bit this is kind of the multiplicative factor that c needs to be by so let's do the other side so a uh plus b and you add a bit of noise to b and again you get one plus epsilon so the gradient of c with respect to b is one as well here's another example of c equals a times b so as a computation graph a and b go into this box which uh takes the dot product and you get c so

what happens uh when you add epsilon noise to a a plus epsilon times b is equal to c plus and now you have b epsilon coming out so therefore the gradient of c with respect to a is now b and analogously we add the voice to b and we see that the the contribution to the output c is a times epsilon a epsilon okay therefore the gradient over here is a okay so this all should be kind of familiar i just cast the sum and product rules for differentiation in graphical form

so let's do a few more kind of small examples these small examples are going to be the building blocks it turns out that you can take these building blocks and compose them to build all sorts of more complicated functions so here's the example we saw before a plus b and the gradients are one and one a minus b the gradients are 1 and minus 1 because if you add epsilon to b then this difference is going to go down by epsilon here we saw this example a times b and the gradients are b and a

um if you look at the squared function a squared the gradient with respect to input is 2a so the kind of power rule um let's consider a and b where you take the max okay so this one uh let's think about this so if i add a little bit of add epsilon to a how's that going to change the max well if a is greater than b then it's just going to change the max by epsilon but if a is less than b then it's going to be 0 because b is going to be um so the gradient of this max of a and b with respect to a

is the indicator function of whether a is greater than b or not and symmetrically the gradient with respect to b is whether b is greater than a okay and finally here is the logistic function a uh sending it through this logistic function and a little bit of algebra which i'll spare you of produces that's actually a quite elegant expression which is sigma times one minus and you can kind of check that as a goes to infinity or minus infinity remember the the sigmoid is going to

saturate at one or zero which means this gradient is actually going to go to zero so that's just a simple sanity check okay so these are the basic building blocks and that's really all the the kind of the um uh brute force differentiation that we're gonna really do the rest is just composition so so now we take these building blocks and we put them together so here's a simple example so suppose you take a square you get b and then you take b and you square it and you get c so by the

building blocks from the previous slide we know that the gradient on this edge is going to be 2 times input here which is b and the gradient along this edge is going to be 2 times a okay so now using ts2 we can apply chain rule from calculus to compute the gradient of c with respect to a and this is going to be nothing more than just the product of those two quantities so in this case we get 2b times 2a and remember that b is equal to a squared subset that 2 that in you get 4 8 cubed

and remember c is a to the fourth so we can verify that this is indeed consistent with using the power rule okay so in general you can take compute these gradients by simply taking the product along edges and that's going to be really useful um on this slide okay so now let's turn to our first example um the hinge loss for linear classification we actually did this one before but i just want to do it again through the lens of computation graph so here is a loss function

and given this loss function i'm going to construct the computation graph and then compute the gradient of the loss with respect to w so working bottom up we have the weight vector and we have the future vector and we take the dot product that gives us a score we take the score we take y and multiply them together and that gives us the margin 1 minus the margin and you take the max of that and 0 and you get the loss so another nice thing about the computation graph is it allows you to

annotate these sub expressions and see how the computation and what the pieces are okay so now let us compute the gradient of the loss with respect to w and what i'm going to do here is all i need to do is compute the gradients along all these edges from loss down to w okay so let's begin at the top here so the gradient width and oh here is our cheat sheet don't forget the cheat sheet so we just now pattern match here's a max over two things well what's on this edge it's first thing

greater than second thing okay so the gradient here is going to be first thing which is one minus margin greater than the second thing which is zero and now what about this edge so here is minus one so let's have minus one what about this uh times so times is the second input second input and the second input here is y and here's another times the second input is f of x so this allows us to think about the gradients one piece at a time and all the little edges are just

implications of this cheat sheet here okay now we're ready to read off the gradient of the loss with respect to w and this is just going to be product of all the edges okay so we have first we have one minus margin greater than 0 so that's i'm going to rewrite that as margin less than one verify that's the same thing but we have a minus sign here that's a minus sign here and then we have y and then we have phi of x you multiply them all together and that's the expression and you can verify that

this is indeed the gradient of the loss function so in summary we computed from uh computed the computation graph and then we apply the this cheat sheet to the individual edges and then you just multiply them all together and just as another note remember the gradient with respect to w is really think about perturbations if you change w by a little bit how much is a loss going to change and the change is going to be the product of all these kind of amplifications evaluated at a particular point

all right so now let's do neural networks now um so this is not going to be really anything new it's just going to be a different example so i'm going to do a two-layer layer neural networks and we're going to again build this computation graph up so we have the feature vector you have the first layer weight matrix v you take the product and then you stick this through the activation function and we're going to label that h which is the hidden uh hidden vector and now we're going to take the dot

product of w and h that gives you the score and then now it's a score minus y is a residual and the residual square is a loss okay another aside is that the computation graph really allows you to see visually this modularity so the part up here is just the square loss and the part down here is anything any way of computing any way of computing a score so before we had a linear classifier a class linear predictor now we have a two-layer neural network it could be a four-layer neural network which

this computation graph is just okay so that's a computation graph now let's uh to perform stochastic gradient descent we need to compute the gradient with respect to both w and v okay so let's compute the gradient with respect to w of the loss and what i'm going to do is look at the the edges and could be the gradients okay so here's our cheat sheet um so okay what goes on this edge what's the gradient of the square this is just two times the input which is in this case two times residual um

what about this edge so um minus so this should just be a one on here and then what about this edge uh this is just going to be the second input um right here so that is an h okay so now multiply all these things together and you get the gradient of um the loss function with respect to w okay all right um so one thing you can um kind of double check is that we did do the gradient of the square lots for linear predictors and it was also two times the residual times the feature vector instead of h

and now we just have h which is a kind of a stand-in for the feature vector with as far as w is concerned so that's kind of a nice sanity check all right so now let's do this more complicated one so this we want to compute the gradient with respect to v of loss of all the arguments and this equals um let's fill in all the edges um so first of all notice that these two uh edges are actually in common with this path so we can go ahead and write them down so one cool thing about computation

graphs is it allows you to see the shared structure that the gradients are actually have themselves also have common sub expressions okay so now we need to do more work here so the gradient on this edge is going to be the other input which is w um this is sigma so the gradient is going to be sigma the input minus 1 minus sigma so this is going to just be h times 1 minus h this this hollow circle here represents the element product of a vector so you just take two vectors and you multiply the

elements together and this is because this function is applying just element-wise and then what about this final edge this is just going to be p of x which is just other input and now we can just multiply the rest of these things together so we have w times uh h times y minus h times a fee of x transpose so there's a slight kind of uh annoyance here because here we have v times v of x whereas before um there's no transpose here because we just have w dot something and w dot is the same as w

transpose okay so uh but the the high level is that the product of all of these green pieces yields the gradient of the loss with respect to v okay all right so that finishes up this example so now we have mainly used this graphical representation to visualize the computation of function values using gradients but you know the promise of back propagation is that we didn't have to do any of that at all i just did that to kind of illustrate the the inner workings of

radiant computations on the computation but now we're going to introduce a batch propagation algorithm which is a general procedure for computing these gradients so we never have to worry about it i'm going to do this back propagation for a simple example which is just this squared loss and linear regression and one note is that previously we've worked with uh symbolic expressions but the actual algorithm is going to operate on numbers usually so what i'm going to do is work with a

concrete example and walk the through the back propagation algorithm with this example so the back propagation algorithm includes two steps a forward uh step and a backwards step so in the forward step what we're going to do is we're going to compute a bunch of forward values from the leaves to the root and each forward value is simply the value of that sub expression rooted at i the value could be a scalar a vector or a matrix so let's walk through this example here

okay so at the leaves we have um w which is three one and we have the feature vector one two so now if you take um these two quantities and you multiple take the dot product you get three plus two which is five um and now you take the score five and you take y you subtract them and you get the residual which is three notice that the forward values of this node is five and the four value of this node is three and now finally you square this and the value of the square is uh 3 squared which is 9.

or value at this node is okay so now we're done with a forward phase all we've done is evaluated uh the loss but importantly we have also remembered all the values along the way which will become come in handy so now the backward step is we're going to compute a backward value gi 1 for every node and this is going to be the gradient of the loss with respect to the value at that node if that node changes value how does the loss change so the backward pass is going to compute

the values from the root to the leaves so let's do this first of this example so the base case gradient of the loss with respect to loss is one um and now we look at the gradient on this edge we did this before it's just two times the residual okay so now um we need to compute the backward value of this node okay to do that we're going to take the backward value of the parent and multiply whatever is on this edge what's on this edge is two times the residual the residual is three

so it's two times three which is six and so one times six is six notice that in computing this backward value i'm using the intermediate computations for the from the forward pass okay so let's continue so the gradient on this edge is 1. so backward value here is 6 which is the parent backward value times what's on this edge which is 1 that gives us 6. and then the backward value of this node is 6 times what's on this edge which is this other input one two and that gives us six comma

so to conclude the back complication algorithm takes these concrete values this expression and produces the gradient of the loss with respect to w evaluated at these concrete values and that's 6 comma 12. okay and the math propagation algorithm remember works for any computation graph four layer neural networks much more complicated models but this is just a simple example to show you the dynamics of forward pass and backward pass okay so now we have the back propagation algorithm we compute

gradients we stick these gradients into stochastic gradient descent and then we just run scans the gradients in and then we get some weights um so now one question is uh what do we get um so we wanted to optimize the training loss using stochastic gradient descent but running stochastic ingredients in does it actually minimize this weight this is a little bit of a delicate uh question here so for linear predictors it turns out that the training loss for a complex loss is going to be a convex

function which means that it is going to have a single global minimum which means that if you start at some point and then you just follow your nodes by running gradient set with appropriate step size it's going to converge to the goal law optima but for neural networks the training loss is non-complex and which means that there's no guarantees at all that you're going to converge to the global minimum you're lucky you can convert to a local mill so optimization of neural networks is in

principle hard but of course people do it anyway and you uh actually get some good results so that there's a gap between theory and practice which is uh not quite understood yet but in practice um getting neuronal works to train properly is a little bit of art so i think of it as kind of like driving stick there's just a lot of degrees of freedom you can stall and get stuck but if you know what you're doing you can actually get a lot of good results okay so here are some examples just to give

you a flavor of what needs to be done okay so here is a 200 network and here is the loss function um the first is initialization matters so if you have a convex function wherever you initialize you run it for long enough you converge to that global optimum for a non-convex function if you initialize here you might get stuck up here if you initialize over here you'll get stuck here and so on so generally you have to be a little bit careful about how you initialize um you can't initialize at zero

because all it turns out that all the rows of your play matrix are going to be identical which is not very useful um so you temporarily initialize around zero with some amount of random noise or you can use pre-training to initialize uh your network as well which we won't cover right now um another thing that people do is called over parameterization so here this corresponds to adding more hidden units than you kind of really need this corresponds to having more a lot of

rows of this uh of this matrix and the idea here is that the more hidden units you have the more kind of quote-unquote chances you have of having the network learn something reasonable by your data so some of the units might die off and not be very useful but you know maybe like some fraction of them will actually be useful and the final thing that people do is using adaptive step sizes which is generally an extension of stochastic gradient descent remember as the caster grain is said you have

we had a single step size ada which controlled how fast you move with methods like atograd or atom you actually get a uh per features uh or per uh parameter step size so for every weight you get a number which dictates how fast you should be moving in that direction and this generally leads to better results okay so one maybe high level thing to keep in mind is don't let your gradients vanish or explode so um if i explain this it will become kind of clear so when you run gradient

descent or gradients the cascading descent if your gradients vanish which means become too small or close to zero then you won't make you'll get stuck and you won't make progress but your gradients become too large then you'll just explode and you will oscillate and might diverge so with careful initialization and setting up the step sizes generally uh and even designing of the neural uh network architecture all of this is around kind of making sure that your gradients don't vanish or explode

okay so that's all the guidance i'll provide you there's a lot more to be said on this topic we're just kind of giving you a high level overview okay so let's summarize now so the most important topic of this module is that of a computation group this allows you to represent arbitrary mathematical expressions and these expressions are built out of these simple building blocks and i hope that this car the idea of computation graphs will allow you to get a better visual

understanding of what your mathematical expressions are doing and also what gradient computations are about and then we saw we had a back propagation algorithm which is this general purpose algorithm for leveraging the computation graph to make uh compute the gradients so notice that we've done this kind of in the context of neural networks but i stress that computation graphs and back propagation is fully general it allows you to handle many many functions and the generality is one of these reasons that you can allows you to iterate very quickly on new types of models and loss functions and opens up this new paradigm for model development differential programming which we'll talk about in a future module all right that's it thanks

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent