Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week1/Optional Labs/C1_W1_Lab05_Gradient_Descent_Soln.ipynb
2201 views
Optional Lab: Gradient Descent for Linear Regression

Goals
In this lab, you will:
automate the process of optimizing and using gradient descent.
Tools
In this lab, we will make use of:
NumPy, a popular library for scientific computing
Matplotlib, a popular library for plotting data
plotting routines in the lab_utils.py file in the local directory
Gradient descent summary
So far in this course, you have developed a linear model that predicts : In linear regression, you utilize input training data to fit the parameters , by minimizing a measure of the error between our predictions and the actual data . The measure is called the , . In training you measure the cost over all of our training samples
In lecture, gradient descent was described as:
where, parameters , are updated simultaneously. The gradient is defined as:
Here simultaniously means that you calculate the partial derivatives for all the parameters before updating any of the parameters.
Implement Gradient Descent
You will implement gradient descent algorithm for one feature. You will need three functions.
compute_gradient
implementing equation (4) and (5) abovecompute_cost
implementing equation (2) above (code from previous lab)gradient_descent
, utilizing compute_gradient and compute_cost
Conventions:
The naming of python variables containing partial derivatives follows this pattern, will be
dj_db
.w.r.t is With Respect To, as in partial derivative of With Respect To .
The lectures described how gradient descent utilizes the partial derivative of the cost with respect to a parameter at a point to update that parameter.
Let's use our
compute_gradient
function to find and plot some partial derivatives of our cost function relative to one of the parameters, .
Above, the left plot shows or the slope of the cost curve relative to at three points. On the right side of the plot, the derivative is positive, while on the left it is negative. Due to the 'bowl shape', the derivatives will always lead gradient descent toward the bottom where the gradient is zero.
The left plot has fixed . Gradient descent will utilize both and to update parameters. The 'quiver plot' on the right provides a means of viewing the gradient of both parameters. The arrow sizes reflect the magnitude of the gradient at that point. The direction and slope of the arrow reflects the ratio of and at that point. Note that the gradient points away from the minimum. Review equation (3) above. The scaled gradient is subtracted from the current value of or . This moves the parameter in a direction that will reduce cost.
Gradient Descent
Now that gradients can be computed, gradient descent, described in equation (3) above can be implemented below in gradient_descent
. The details of the implementation are described in the comments. Below, you will utilize this function to find optimal values of and on the training data.
Take a moment and note some characteristics of the gradient descent process printed above.
The cost starts large and rapidly declines as described in the slide from the lecture.
The partial derivatives,
dj_dw
, anddj_db
also get smaller, rapidly at first and then more slowly. As shown in the diagram from the lecture, as the process nears the 'bottom of the bowl' progress is slower due to the smaller value of the derivative at that point.progress slows though the learning rate, alpha, remains fixed
Cost versus iterations of gradient descent
A plot of cost versus iterations is a useful measure of progress in gradient descent. Cost should always decrease in successful runs. The change in cost is so rapid initially, it is useful to plot the initial decent on a different scale than the final descent. In the plots below, note the scale of cost on the axes and the iteration step.
Predictions
Now that you have discovered the optimal values for the parameters and , you can now use the model to predict housing values based on our learned parameters. As expected, the predicted values are nearly the same as the training values for the same housing. Further, the value not in the prediction is in line with the expected value.
Above, the contour plot shows the over a range of and . Cost levels are represented by the rings. Overlayed, using red arrows, is the path of gradient descent. Here are some things to note:
The path makes steady (monotonic) progress toward its goal.
initial steps are much larger than the steps near the goal.
Zooming in, we can see that final steps of gradient descent. Note the distance between steps shrinks as the gradient approaches zero.
Increased Learning Rate
Let's try increasing the value of and see what happens:
Above, and are bouncing back and forth between positive and negative with the absolute value increasing with each iteration. Further, each iteration changes sign and cost is increasing rather than decreasing. This is a clear sign that the learning rate is too large and the solution is diverging. Let's visualize this with a plot.
Above, the left graph shows 's progression over the first few steps of gradient descent. oscillates from positive to negative and cost grows rapidly. Gradient Descent is operating on both and simultaneously, so one needs the 3-D plot on the right for the complete picture.
Congratulations!
In this lab you:
delved into the details of gradient descent for a single variable.
developed a routine to compute the gradient
visualized what the gradient is
completed a gradient descent routine
utilized gradient descent to find parameters
examined the impact of sizing the learning rate