Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week3/Optional Labs/C1_W3_Lab06_Gradient_Descent_Soln.ipynb
3748 views
Optional Lab: Gradient Descent for Logistic Regression
Goals
In this lab, you will:
update gradient descent for logistic regression.
explore gradient descent on a familiar data set
Data set
Let's start with the same two feature data set used in the decision boundary lab.
As before, we'll use a helper function to plot this data. The data points with label are shown as red crosses, while the data points with label are shown as blue circles.
Logistic Gradient Descent
Recall the gradient descent algorithm utilizes the gradient calculation:
Where each iteration performs simultaneous updates on for all , where
m is the number of training examples in the data set
is the model's prediction, while is the target
For a logistic regression model where is the sigmoid function:
Gradient Descent Implementation
The gradient descent algorithm implementation has two components:
The loop implementing equation (1) above. This is
gradient_descent
below and is generally provided to you in optional and practice labs.The calculation of the current gradient, equations (2,3) above. This is
compute_gradient_logistic
below. You will be asked to implement this week's practice lab.
Calculating the Gradient, Code Description
Implements equation (2),(3) above for all and . There are many ways to implement this. Outlined below is this:
initialize variables to accumulate
dj_dw
anddj_db
for each example
calculate the error for that example
for each input value in this example,
multiply the error by the input , and add to the corresponding element of
dj_dw
. (equation 2 above)
add the error to
dj_db
(equation 3 above)
divide
dj_db
anddj_dw
by total number of examples (m)note that in numpy
X[i,:]
orX[i]
and isX[i,j]
Check the implementation of the gradient function using the cell below.
Expected output
Gradient Descent Code
The code implementing equation (1) above is implemented below. Take a moment to locate and compare the functions in the routine to the equations above.
Let's run gradient descent on our data set.
Let's plot the results of gradient descent:
In the plot above:
the shading reflects the probability y=1 (result prior to decision boundary)
the decision boundary is the line at which the probability = 0.5
Another Data set
Let's return to a one-variable data set. With just two parameters, , , it is possible to plot the cost function using a contour plot to get a better idea of what gradient descent is up to.
As before, we'll use a helper function to plot this data. The data points with label are shown as red crosses, while the data points with label are shown as blue circles.
In the plot below, try:
changing and by clicking within the contour plot on the upper right.
changes may take a second or two
note the changing value of cost on the upper left plot.
note the cost is accumulated by a loss on each example (vertical dotted lines)
run gradient descent by clicking the orange button.
note the steadily decreasing cost (contour and cost plot are in log(cost)
clicking in the contour plot will reset the model for a new run
to reset the plot, rerun the cell
Congratulations!
You have:
examined the formulas and implementation of calculating the gradient for logistic regression
utilized those routines in
exploring a single variable data set
exploring a two-variable data set