Zum Inhalt springen
L

Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).

PyTorch Tutorial 04 - Backpropagation - Theory With Example

Patrick Loeber13:13 146.216 Aufrufe veröffentlicht Auf YouTube

Das Wichtigste aus dem Video

Tipp auf eine Zeit – das Video springt genau dorthin.

Transkriptautomatisch erstellt · 58 Zeilen
Herunterladen
  1. hi everybody welcome to a new PI torch tutorial in this video I'm going to explain the famous back propagation algorithm and how we can calculate
  2. gradients with it I explained the necessary concepts of this technique and then I will walk you through a concrete example with some numbers and at the end
  3. we will then see how easy it is to apply back propagation in pi torch so let's start and the first concept we must know is the chain rule so let's say we have
  4. two operations or two functions so first we have to input X and then we apply a function a and get an output Y and then we use this output as the input for our
  5. second function so the second function B and then we get the final output C and now we want to minimize our C so we want to know the derivative of C with respect
  6. to our X and here in the beginning and we can do this using the so-called chain rule so for this we first compute the derivative of C with respect to Y and
  7. multiply this with the derivative of Y with respect to X and then we get the final derivative we want so first here we compute the derivative at this
  8. position so the derivative of this output with respect to this input and then here the derivative of this output with respect to this input and then we
  9. multiply them together and get the final gradient we are interested in so that's the chain rule and now the next concept is the so called computational graph so
  10. for every operation we do with our tenth source high-touch will create a graph for us silver at each node we apply one operation or one function with some
  11. inputs and then get an output so here at this case in this example we use a multiplication operations so we multiply x and y and
  12. then getsy and now at these notes we can calculate so-called local gradients and we can use them later in the chain rule to get the final gradient so here the
  13. local gradients we can compute two gradients the gradient of C with respect to X and this is simple since we know this function here so this is the
  14. gradient gradient of x times y with respect to X which is y and here in the bottom we compute the derivative of x times y would respect to Y which is X so
  15. local gradients are easy because we know this function and why do we want them because typically our graph has more operations and at the very end we
  16. calculate a loss function that we want to minimize so we have to calculate the gradient of this loss with respect to our parameter X in the beginning and now
  17. let's suppose at this position we already know the derivative of the loss with respect to our C and then we can get the final gradient we want so that
  18. with the chain rule so the gradient of the loss with respect to X is then the gradient of loss with respect to C times our local gradient so the derivative of
  19. C with respect to X and yeah this is how we get the final gradient then and now the whole concept consists of three steps so first we do a forward pass
  20. where we apply all the functions and compute the loss then at each node we calculate the local gradients and then we do a so-called backward pass where we
  21. compute the gradient of the loss with respect to our weights or parameters using the chain rule so these are the three steps we're gonna do
  22. and now we look at a concrete example so here we want to use linear regression and if you don't know how this works then I highly recommend my machine
  23. learning from scratch tutorial about linear regression I will put the link in the description so basically we model our output with a linear combination of
  24. some weights and an input so our Y hat or Y predicted is W times X and then we formulate some loss function so in this case this is the squared error actually
  25. it should be the mean squared error but for simplicity we just use the squared error otherwise you would have another operation to get the mean so the loss is
  26. the difference of the predicted Y minus the actual Y and then we square it and now we want to minimize our loss so we want to know the derivative of the loss
  27. with respect to our weights and how do we get that so we apply our three steps first we do a forward pass and put in the X and the W and then here we put in
  28. the Y and apply our functions here and then we get the loss then we calculate the group the local gradients at each node
  29. so here the gradient of the loss with respect to our s then here at the gradient of the s with respect to our Y hat and here at this node the gradient
  30. of Y hat with respect to our W and then we do a backward pass so we start at the end and here we have the first we have the derivative of the loss with respect
  31. to our s and then we use them and we also use the chain rule to get the derivative of the loss with respect to of the Y hat and then again we use this
  32. and the chain rule to get the final grade of the loss with respect to our W so let's do this with some concrete numbers
  33. so let's say we have x and y is given so X is 1 and Y is 2 in the beginning and so these are our training samples and we initialize our weight so let's say for
  34. example we say our W is 1 in the beginning and then we do the forward pass so here at the first node we multiply X and W so we get Y hat equals
  35. 1 then at the next node we do a subtraction so Y hat minus y this one minus 2 equals minus 1 and at the very end so we square our s so we have 1/2 s
  36. squared so our loss then is 1 and now we calculate the local gradient so at the last node we have the gradient of the loss with respect to s and this is
  37. simple because we know the function so this is the gradient of s squared so this is just 2 s and then at the next node we have the gradient of s with
  38. respect to Y hat which is the gradient of the function y hat minus y with respect to Y hat which is just 1 and then here at the last node we have the
  39. derivative of Y hat with respect to W so this is the derivative of W times X with respect to W which is X and also notice that we don't need to go don't need to
  40. know the derivatives in this graph lines so we don't need to know what is the derivative of s with respect to Y and also here we don't need this because our
  41. X and our Y are fixed so we are only interested in our parameters that we want to update here and yeah and then we do the backward
  42. pass so first now we use our local gradients so we want to compute the derivative of the loss with respect to y hat and here we use the chain rule with
  43. our to local gradients that we just computed which is 2 s times 1 and s is minus 1 which we calculated up here and then so this is minus 2 and now we use
  44. this derivative and also this loka gradient to then get the final gradient the gradient of the loss with respect to our W which is the gradient of the loss
  45. with respect to y hat times the gradient of Y hat with respect to W which is minus 2 times X and X is 1 so the final gradient is minus 2 so this is the final
  46. gradient then that we know want to know and yeah that's all how back propagation works and let's jump over to our code and verify that pi touch get these exact
  47. numbers so let's remember X is 1 Y is 2 and W is 1 and then our first gradient should be minus 2 so let's see how we can use this in pi torch and first of
  48. all we import torch of course then we create our vector art ends us so we say x equals torch dot tens or and this is 1 and then our y equals torch dot tens or
  49. with 2 and then our initial weight is a tensor also with 1 so one point zero to make it a float and here in with our weights we are interested in the
  50. gradient so we need to specify require squat equals true and then we do the forward pass and gets and compute the loss so we
  51. simply say y hat equals W times X which is our function and then we say loss equals y hat minus the actual Y and then we square this so we say this to the
  52. power of two and now let's print our loss and see this is one in the beginning and now we want to do the backward pass so let's do the backward
  53. pass and pi touch and we'll compute the local gradients automatically for us and also computes the backward pass automatically for us so the only thing
  54. that we have to call is say loss backward so this is the whole gradient computation and now our W has this dot gret attribute and we can print this and
  55. now this is the first gradient in the after the first forward and backward pass and remember this should be minus two in the beginning and here we see we
  56. have eight enso with minus two so this is working and the next steps would be for example now we update our weights and then we do the next forward and
  57. backward pass and do this for a couple of iterations and yeah that's how back propagation works and how and also how easy it is to use it in pi torch and I
  58. hope you enjoyed this tutorial please subscribe to the channel and see you next time bye

Zum Nachlesen