Zum Inhalt springen
L

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

Backpropagation Details Pt. 1: Optimizing 3 parameters simultaneously.

StatQuest with Josh Starmer18:32 310.367 Aufrufe veröffentlicht Auf YouTube

Das Wichtigste aus dem Video

Tipp auf eine Zeit – das Video springt genau dorthin.

Transkriptautomatisch erstellt · 105 Zeilen
Herunterladen
  1. The sun is out and it's nice outside. It's the perfect weather for StatQuest. Yeah. Hello, I'm Josh Starmer and welcome to
  2. StatQuest. Today, we're going to talk about backpropagation details, part one. Note, this StatQuest assumes that you have already watched Neural Networks
  3. Part 2, Backpropagation Main Ideas. If not, check out the quest. The link is in the description below. In Backpropagation Main Ideas, we had
  4. this super simple data set that showed whether or not different drug dosages were effective against a virus.
  5. Then we had this simple neural network that already had optimal values for all of the parameters except for the last bias term, B sub 3.
  6. Then, using everything in the neural network except for the last bias, B sub 3, we drew this green squiggle.
  7. Then we demonstrated the main ideas behind backpropagation by optimizing B sub 3. We first used the chain rule to
  8. calculate the derivative of the sum of the squared residuals with respect to the unknown parameter, which in this case was B sub 3.
  9. Then we initialized the unknown parameter with a number, and in this case, we set B sub 3 equal to 0. And used gradient descent to optimize
  10. the unknown parameter. Hooray! We can optimize the last bias term, B sub 3. Now let's pretend we don't know B sub
  11. 3's optimal value and start working our way backwards so that, along with B sub 3, we optimize the last two weights, W W 3 and W sub 4.
  12. Note, the goal of this quest is to learn how the chain rule and gradient ascent applies to multiple parameters and to introduce some
  13. fancy notation. In the next part, we'll go completely bonkers with the chain rule and learn how to optimize all seven parameters in
  14. this neural network simultaneously. Bam! So, let's go back to not knowing the optimal values for W sub 3, W sub 4, and
  15. B sub 3. And, just like before, we'll assume that the other weights and biases are already optimized.
  16. The first thing we do is initialize the weights W sub 3 and W sub 4 with random starting values. And, in this example, that means we
  17. randomly select two values from a standard normal distribution. Then, we initialize the last bias B sub 3 to zero because bias terms frequently
  18. start at zero. Now, if we run dosages from zero to one through the connection to the top node in the hidden layer,
  19. then, just like before, we get the corresponding Y axis coordinates and this blue curve. Now, we multiply the Y axis coordinates
  20. on the blue curve by W sub 3, which starts out with the random value 0.36. And, we get this new blue curve. Now, if we run dosages from zero to one
  21. through the connection to the bottom node in the hidden layer, then, just like before, we get the corresponding Y axis coordinates
  22. for this orange curve. Now, we multiply the Y axis coordinates on the orange curve by W sub 4, which starts with the random value 0.63.
  23. And we get this new orange curve. Now, we add the blue and orange curves together and get this green squiggle.
  24. Lastly, since the initial value for B sub 3 is 0, adding it to the Y-axis values on the green squiggle does not change anything.
  25. In other words, given the current parameters for this neural network, some of which are optimal, and some of which are not optimal,
  26. we end up with this green squiggle. Now, just like before, we can quantify how well the green squiggle fits the data by calculating the sum of the
  27. squared residuals. And we get the sum of the squared residuals equals 1.4. Now, even though we have not yet
  28. optimized W sub 3 and W sub 4, we can still plot the sum of the squared residuals with respect to B sub 3. And just like before, if we change B sub
  29. 3, then we will change the sum of the squared residuals. And that means, just like before, we can optimize B sub 3 by finding the
  30. derivative of the sum of the squared residuals with respect to B sub 3 and plugging the derivative into the gradient descent algorithm to find the
  31. optimal value for B sub 3. And just like before, because the predicted values in the sum of the squared residuals
  32. come from the green squiggle, and the green squiggle is the sum of the blue and orange curves plus B sub 3, then the sum of the squared residuals
  33. are linked to B sub 3 by the predicted values. So, by the chain rule, the derivative of the sum of the squared residuals with
  34. respect to B sub 3 is the derivative of the sum of the squared residuals with respect to the predicted values
  35. times the derivative of the predicted values with respect to B sub 3. Note, this is the exact same derivative that we calculated in backpropagation
  36. main ideas. The point of this is that even though we are now optimizing more than one parameter,
  37. the derivatives that we have already calculated with respect to the sum of the squared residuals do not change. Bam!
  38. Now, let's talk about how to calculate the derivatives of the sum of the squared residuals with respect to the weights W sub 3 and W sub 4.
  39. Unfortunately, before we can do that, we have to introduce some fancy notation. First, let's remember that the I in this summation notation
  40. is an index for the data in the data set. For example, when I equals 1, we are talking about observed sub 1, which is
  41. 0. And we are talking about predicted sub 1, which is 0.72. However, we can also talk about dosage
  42. sub I. And when I equals 1, we are talking about dosage sub 1, which is 0. When I equals 2, we're talking about
  43. dosage sub 2, which is 0.5. And when I equals 3, we're talking about dosage sub 3, which is 1. And because dosage sub I is the input
  44. value, we call it input sub I. And that means this connection multiplies input sub I by weight W sub 1, which is 3.34.
  45. And it adds bias sub 1, which is -1.43 to get an x-axis coordinate for the activation function in the top node in the hidden layer.
  46. Meanwhile, the other connection multiplies input sub I by weight W sub 2, which is -3.53 and adds bias B sub 2, which is 0.57
  47. to get an x-axis coordinate for the activation function in the bottom node in the hidden layer. So, we have two different x-axis
  48. coordinates for input sub I. In order to keep track of things, let's call this x-axis coordinate X sub 1, I where the one in 1, I refers to the
  49. activation function in the top node. And the I in 1, I tells us that it corresponds to input sub I. Likewise, let's call this x-axis
  50. coordinate X sub 2, I where the two in 2, I refers to the activation function in the bottom node and the I in 2, I tells us that it
  51. corresponds to input sub I. For example, if I equals 3, then we're talking about the third dosage, dosage sub 3.
  52. And that means we're talking about input sub 3, which is one, the maximum dosage. And that means the x-axis coordinate for the activation function in the top node,
  53. X sub 1, 3 is equal to 1.91. And the x-axis coordinate for the activation function in the bottom node,
  54. X sub 2,3 is equal to -2.96. Bam. If we plugged in all values for I into
  55. dosage sub I, we get X sub 1,I values in this red box. And X sub 2,I values in this red box. Now, in order to get the Y axis
  56. coordinates for the activation function in the top node, we plug X sub 1,I into the activation function,
  57. which, in this example, is the soft plus function, and that gives us Y sub 1,I. Just like before, the one in 1,I tells
  58. us that we are talking about the activation function in the top node. And the I tells us which dosage we are talking about.
  59. Likewise, in order to get the Y axis coordinates for the activation function in the bottom node, we plug X sub 2,I into the activation
  60. function. And that gives us Y sub 2,I. Bam. Now that we understand the
  61. fancy notation, we can talk about how to calculate the derivatives of the sum of the squared residuals with respect to the weights W
  62. sub 3 and W sub 4. First, remember that Y sub 1,I represents the Y axis coordinates for the top activation function.
  63. And they form this initial blue curve. However, we get the final blue curve by multiplying the Y axis coordinates Y sub 1,I by W sub 3.
  64. And that means we can plug Y sub 1,I * W sub 3 into the equation for the predicted values. Likewise, W sub 4 multiplies the Y-axis
  65. coordinates Y sub 2, I from the bottom activation function to create the final orange curve. And that means we can plug Y sub 2, I *
  66. W sub 4 into the equation for the predicted values. Now, since this sum creates the green squiggle
  67. and the green squiggle gives us predictions that we evaluate with the sum of the squared residuals
  68. then the sum of the squared residuals are linked to W sub 3 and W sub 4 by the predicted values. That means we can use the chain rule
  69. to determine the derivative of the sum of the squared residuals with respect to W sub 3 and with respect to W sub 4.
  70. The chain rule says that the derivative of the sum of the squared residuals with respect to W sub 3 is the derivative of the sum of the
  71. squared residuals with respect to the predicted values * the derivative of the predicted values with respect to W sub 3.
  72. Likewise, the derivative with respect to W sub 4 is the derivative of the sum of the squared residuals with respect to the predicted values
  73. * of the predicted values with respect to W sub 4. Double bam? Not yet. Note, in both cases, the derivative of the sum of the
  74. squared residuals with respect to the predicted values is the exact same as the derivative used for B sub 3.
  75. Just to remind you, we start by substituting the sum of the squared residuals with its equation. Then we use the chain rule
  76. to move the square to the front and then we multiply that by the derivative of the stuff inside the parentheses with respect to the
  77. predicted values, -1. Lastly, we simplify by multiplying two by -1. And this is the derivative of the sum of
  78. the squared residuals with respect to the predicted values. So, we just plug it in. Now, to solve for the derivative of the
  79. predicted values with respect to W sub three, we plug in the equation for the predicted values and the derivative of the first term
  80. with respect to W sub three is Y sub one {comma} I. And the derivatives of the other terms are both zero since they do not contain
  81. W sub three. And we end up with just Y sub one {comma} I. So, we multiply the derivative of the
  82. sum of the squared residuals with respect to the predicted values by Y sub one {comma} I. Likewise, the derivative of the
  83. predicted values with respect to W sub four is zero for the first term plus Y sub two {comma} I for the second
  84. term plus zero for the third term which is just Y sub two {comma} I. So, we multiply the derivative of the
  85. sum of the squared residuals with respect to the predicted values by Y sub two {comma} I. Double bam!
  86. Now that we have the derivatives of the sum of the squared residuals with respect to W sub three, W sub four,
  87. and B sub three, we can plug them into gradient descent to optimize W sub three, W sub four, and B sub three.
  88. First, we initialize W sub 3 and W sub 4 with random values and set B sub 3 equal to 0. Now, starting with the derivative of the
  89. sum of the squared residuals with respect to W sub 3, first, we expand the summation. Then we plug in the observed values
  90. and plug in the predicted values from the green squiggle. Remember, we get the predicted values on the green squiggle by running the
  91. dosages through the neural network. Now we plug in the Y axis coordinates for the activation function in the top node, Y sub 1, I.
  92. Lastly, we do the math and get 2.58. Likewise, we calculate the derivative of the sum of the squared residuals with respect to W sub 4
  93. and with respect to B sub 3. Now we use the derivatives to calculate the new values for W sub 3,
  94. W sub 4, and B sub 3.
  95. Now we repeat that process until the predictions no longer improve very much or we reach a maximum number of steps or we meet some other criteria.
  96. Now let's check out a fancy animation that shows the gradient descent in action. These gray dots represent the data that
  97. we are using to train the neural network. And the orange and blue curves represent the orange and blue curves.
  98. And the green squiggle represents the sum of the orange and blue curves plus B sub 3. Now, watch how the green squiggle fits
  99. the data after 175 steps in gradient descent. Bam! So, after a bunch of steps, we see how gradient descent optimizes the
  100. parameters. Triple bam! In the next StatQuest, we'll go totally bonkers with the chain rule and show how
  101. to optimize all of the parameters in a neural network simultaneously. Now, it's time for some shameless self-promotion.
  102. If you want to review statistics and machine learning offline, check out the StatQuest Study Guides at statquest.org. There's something for everyone.
  103. Hooray! We've made it to the end of another exciting StatQuest. If you like this StatQuest and want to see more, please subscribe. And if you want to
  104. support StatQuest, consider contributing to my Patreon campaign, becoming a channel member, buying one or two of my original songs or a t-shirt or a hoodie,
  105. or just donate. The links are in the description below. All right. Until next time, quest on.

Zum Nachlesen