Gradient to Cost Function - Appendix
Introduction
In this lesson, you'll find the details on how to compute the partial derivatives in the "Gradient to cost function" lesson.
Computing the First Partial Derivative
Let's start with taking the partial derivative with respect to
Now this is a tricky function to take the derivative of. So we can use functional composition followed by the chain rule to make it easier. Using functional composition, we can rewrite our function
Now using the chain rule to find the partial derivative with respect to a change in the slope, gives us:
Because g is a function of m we get
J is a function of g (which is a function of m) we get
Our next step is to solve these derivatives individually:
$$
\begin{align}
\frac{dJ}{dg}J(g(m, b))&=\frac{dJ}{dg}g(m,b)^2 &&\text{Solve
Each of the terms are treated as constants, except for the middle term.
Now plugging these back into our chain rule [1] we have: $$ \begin{align} \color{blue}{\frac{dJ}{dg}J(g(m,b))}\color{red}{\frac{dg}{dm}g(m,b)}&=\color{blue}{(2g(m,b))}\color{red}{-x}\ \ &= 2(y - (mx + b))*-x \ \end{align} $$ So
$$ \begin{align} [1]\mspace{5ex}\frac{\delta J}{\delta m}J(m, b)&=2*(y - (mx + b))-x\ \ &= -2x(y - (mx + b ))\ \end{align} $$
Computing the Second Partial Derivative
Ok, now let's calculate the partial derivative with respect to a change in the y-intercept. We express this mathematically with the following:
Then once again, we use functional composition following by the chain rule. So we view our cost function as the same two functions
So applying the chain rule, to this same function composition, we get:
Now, our next step is to calculate these partial derivatives individually.
From our earlier calculation of the partial derivative, we know that
Now we plug our terms into our chain rule [2] and get:
$$ \begin{align} \color{blue}{\frac{dJ}{dg}J(g)}\color{red}{\frac{dg}{db}g(m,b)}&= \color{blue}{2g(m,b)}\color{red}{-1}\ \ &= -2(y - (mx + b))\ \end{align} $$