240615
This commit is contained in:
@@ -7,10 +7,10 @@
|
||||
"# Optional Lab: Gradient Descent for Linear Regression\n",
|
||||
"\n",
|
||||
"<figure>\n",
|
||||
" <center> <img src=\"./images/C1_W1_L4_S1_Lecture_GD.png\" style=\"width:800px;height:200px;\" ></center>\n",
|
||||
" <center> <img src=\"https://i.wolves.top/picgo/202408041059271.png\" style=\"width:800px;height:200px;\" ></center>\n",
|
||||
"</figure>"
|
||||
],
|
||||
"id": "51d539b8933c08f0"
|
||||
"id": "64d8b1775dd1fa3a"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -20,7 +20,7 @@
|
||||
"In this lab, you will:\n",
|
||||
"- automate the process of optimizing $w$ and $b$ using gradient descent."
|
||||
],
|
||||
"id": "3c5af2ac34433447"
|
||||
"id": "99b50273cf2c810a"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -32,7 +32,7 @@
|
||||
"- Matplotlib, a popular library for plotting data\n",
|
||||
"- plotting routines in the lab_utils.py file in the local directory"
|
||||
],
|
||||
"id": "7a45aef1427d3296"
|
||||
"id": "4502238878e707b7"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -43,10 +43,10 @@
|
||||
"import math, copy\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"plt.style.use('./deeplearning.mplstyle')\n",
|
||||
"plt.style.use('deeplearning.mplstyle')\n",
|
||||
"from lab_utils_uni import plt_house_x, plt_contour_wgrad, plt_divergence, plt_gradients"
|
||||
],
|
||||
"id": "a1fce6f64e2718a1"
|
||||
"id": "3e02dd559bcc5bce"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -62,7 +62,7 @@
|
||||
"| 1 | 300 |\n",
|
||||
"| 2 | 500 |\n"
|
||||
],
|
||||
"id": "1c0984e19ab9e6b4"
|
||||
"id": "a52a5b79363891a4"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -74,7 +74,7 @@
|
||||
"x_train = np.array([1.0, 2.0]) #features\n",
|
||||
"y_train = np.array([300.0, 500.0]) #target value"
|
||||
],
|
||||
"id": "bf19c8fde61dfcfd"
|
||||
"id": "5f14f59dcc4f7e1e"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -84,7 +84,7 @@
|
||||
"### Compute_Cost\n",
|
||||
"This was developed in the last lab. We'll need it again here."
|
||||
],
|
||||
"id": "b1780bff7ac8aa73"
|
||||
"id": "7057888cd71b77c1"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -105,7 +105,7 @@
|
||||
"\n",
|
||||
" return total_cost"
|
||||
],
|
||||
"id": "fac0212ae8afd849"
|
||||
"id": "52ab101b90f4d2c3"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -118,7 +118,7 @@
|
||||
"In linear regression, you utilize input training data to fit the parameters $w$,$b$ by minimizing a measure of the error between our predictions $f_{w,b}(x^{(i)})$ and the actual data $y^{(i)}$. The measure is called the $cost$, $J(w,b)$. In training you measure the cost over all of our training samples $x^{(i)},y^{(i)}$\n",
|
||||
"$$J(w,b) = \\frac{1}{2m} \\sum\\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)})^2\\tag{2}$$ "
|
||||
],
|
||||
"id": "ae55d8d0930866a3"
|
||||
"id": "71fc10831d272645"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -142,7 +142,7 @@
|
||||
"\n",
|
||||
"Here *simultaniously* means that you calculate the partial derivatives for all the parameters before updating any of the parameters."
|
||||
],
|
||||
"id": "760cfdd6f3f8c687"
|
||||
"id": "60baf048109b0868"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -159,7 +159,7 @@
|
||||
"- The naming of python variables containing partial derivatives follows this pattern,$\\frac{\\partial J(w,b)}{\\partial b}$ will be `dj_db`.\n",
|
||||
"- w.r.t is With Respect To, as in partial derivative of $J(wb)$ With Respect To $b$.\n"
|
||||
],
|
||||
"id": "e3d4ec60b7ca63f6"
|
||||
"id": "a3d60bc28ec4ab6c"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -170,7 +170,7 @@
|
||||
"<a name='ex-01'></a>\n",
|
||||
"`compute_gradient` implements (4) and (5) above and returns $\\frac{\\partial J(w,b)}{\\partial w}$,$\\frac{\\partial J(w,b)}{\\partial b}$. The embedded comments describe the operations."
|
||||
],
|
||||
"id": "d2b095ba2d1b3f1a"
|
||||
"id": "f775b4e5b10ecad4"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -206,7 +206,7 @@
|
||||
" \n",
|
||||
" return dj_dw, dj_db"
|
||||
],
|
||||
"id": "c60c94ca2ba78bea"
|
||||
"id": "ee90c07f78acea2"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -214,7 +214,7 @@
|
||||
"source": [
|
||||
"<br/>"
|
||||
],
|
||||
"id": "4e7b10ba866408c9"
|
||||
"id": "19087465bb1bb266"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -223,7 +223,7 @@
|
||||
"<img align=\"left\" src=\"./images/C1_W1_Lab03_lecture_slopes.PNG\" style=\"width:340px;\" > 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. \n",
|
||||
"Let's use our `compute_gradient` function to find and plot some partial derivatives of our cost function relative to one of the parameters, $w_0$.\n"
|
||||
],
|
||||
"id": "6a7ec85f8dc84e49"
|
||||
"id": "d7ec2e7ed2eda8c"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -245,7 +245,7 @@
|
||||
"plt_gradients(x_train,y_train, compute_cost, compute_gradient)\n",
|
||||
"plt.show()"
|
||||
],
|
||||
"id": "92ccd2b1189c2e4f"
|
||||
"id": "b6ce17e4e7a6d49c"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -256,7 +256,7 @@
|
||||
"The left plot has fixed $b=100$. Gradient descent will utilize both $\\frac{\\partial J(w,b)}{\\partial w}$ and $\\frac{\\partial J(w,b)}{\\partial b}$ 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 $\\frac{\\partial J(w,b)}{\\partial w}$ and $\\frac{\\partial J(w,b)}{\\partial b}$ at that point.\n",
|
||||
"Note that the gradient points *away* from the minimum. Review equation (3) above. The scaled gradient is *subtracted* from the current value of $w$ or $b$. This moves the parameter in a direction that will reduce cost."
|
||||
],
|
||||
"id": "e2c8ab9d96d48ec0"
|
||||
"id": "b0e2daeddf6ed7cc"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -266,7 +266,7 @@
|
||||
"### Gradient Descent\n",
|
||||
"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 $w$ and $b$ on the training data."
|
||||
],
|
||||
"id": "9a8470ca87962ea"
|
||||
"id": "28097a7c403638d0"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -321,7 +321,7 @@
|
||||
" \n",
|
||||
" return w, b, J_history, p_history #return w and J,w history for graphing"
|
||||
],
|
||||
"id": "289c570d8e6d0b9f"
|
||||
"id": "596f5f893cabdacb"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -358,20 +358,20 @@
|
||||
" iterations, compute_cost, compute_gradient)\n",
|
||||
"print(f\"(w,b) found by gradient descent: ({w_final:8.4f},{b_final:8.4f})\")"
|
||||
],
|
||||
"id": "b78236dad287b179"
|
||||
"id": "bff23449aa3de468"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<img align=\"left\" src=\"./images/C1_W1_Lab03_lecture_learningrate.PNG\" style=\"width:340px; padding: 15px; \" > \n",
|
||||
"<img align=\"left\" src=\"https://i.wolves.top/picgo/202408041100217.png\" style=\"width:340px; padding: 15px; \" > \n",
|
||||
"Take a moment and note some characteristics of the gradient descent process printed above. \n",
|
||||
"\n",
|
||||
"- The cost starts large and rapidly declines as described in the slide from the lecture.\n",
|
||||
"- The partial derivatives, `dj_dw`, and `dj_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.\n",
|
||||
"- progress slows though the learning rate, alpha, remains fixed"
|
||||
],
|
||||
"id": "ac49cab25475792b"
|
||||
"id": "9df6b2fd3473f9f8"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -380,7 +380,7 @@
|
||||
"### Cost versus iterations of gradient descent \n",
|
||||
"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."
|
||||
],
|
||||
"id": "84928145fa5d834e"
|
||||
"id": "4dcde8c8bc9c91c7"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -408,7 +408,7 @@
|
||||
"ax1.set_xlabel('iteration step') ; ax2.set_xlabel('iteration step') \n",
|
||||
"plt.show()"
|
||||
],
|
||||
"id": "3fb63a2e448607fa"
|
||||
"id": "9ab3e30b473e57d6"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -417,7 +417,7 @@
|
||||
"### Predictions\n",
|
||||
"Now that you have discovered the optimal values for the parameters $w$ and $b$, 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."
|
||||
],
|
||||
"id": "877b4268d61cbd1e"
|
||||
"id": "e557c848a8191c07"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -439,7 +439,7 @@
|
||||
"print(f\"1200 sqft house prediction {w_final*1.2 + b_final:0.1f} Thousand dollars\")\n",
|
||||
"print(f\"2000 sqft house prediction {w_final*2.0 + b_final:0.1f} Thousand dollars\")"
|
||||
],
|
||||
"id": "e1840d7ee6522d2b"
|
||||
"id": "f492269c6a06d60e"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -449,7 +449,7 @@
|
||||
"## Plotting\n",
|
||||
"You can show the progress of gradient descent during its execution by plotting the cost over iterations on a contour plot of the cost(w,b). "
|
||||
],
|
||||
"id": "622ef2db1d43a509"
|
||||
"id": "d2ecb7dc0ac32a6f"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -471,7 +471,7 @@
|
||||
"fig, ax = plt.subplots(1,1, figsize=(12, 6))\n",
|
||||
"plt_contour_wgrad(x_train, y_train, p_hist, ax)"
|
||||
],
|
||||
"id": "9338e55a8dd87ceb"
|
||||
"id": "f758c238eb415c01"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -481,7 +481,7 @@
|
||||
"- The path makes steady (monotonic) progress toward its goal.\n",
|
||||
"- initial steps are much larger than the steps near the goal."
|
||||
],
|
||||
"id": "31e1cbcb0421b6e7"
|
||||
"id": "d4fa08759d4c0681"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -489,7 +489,7 @@
|
||||
"source": [
|
||||
"**Zooming in**, we can see that final steps of gradient descent. Note the distance between steps shrinks as the gradient approaches zero."
|
||||
],
|
||||
"id": "90e4ccbd6599f90a"
|
||||
"id": "e763eb94c55dd87"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -512,7 +512,7 @@
|
||||
"plt_contour_wgrad(x_train, y_train, p_hist, ax, w_range=[180, 220, 0.5], b_range=[80, 120, 0.5],\n",
|
||||
" contours=[1,5,10,20],resolution=0.5)"
|
||||
],
|
||||
"id": "871c3cbed53e2e1d"
|
||||
"id": "c5a157bda2c18e2c"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -522,13 +522,13 @@
|
||||
"### Increased Learning Rate\n",
|
||||
"\n",
|
||||
"<figure>\n",
|
||||
" <img align=\"left\", src=\"./images/C1_W1_Lab03_alpha_too_big.PNG\" style=\"width:340px;height:240px;\" >\n",
|
||||
" <img align=\"left\", src=\"https://i.wolves.top/picgo/202408041100502.png\" style=\"width:340px;height:240px;\" >\n",
|
||||
"</figure>\n",
|
||||
"In the lecture, there was a discussion related to the proper value of the learning rate, $\\alpha$ in equation(3). The larger $\\alpha$ is, the faster gradient descent will converge to a solution. But, if it is too large, gradient descent will diverge. Above you have an example of a solution which converges nicely.\n",
|
||||
"\n",
|
||||
"Let's try increasing the value of $\\alpha$ and see what happens:"
|
||||
],
|
||||
"id": "fe6cbc15bef486f6"
|
||||
"id": "f24d64e2c8fee91c"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -563,7 +563,7 @@
|
||||
"w_final, b_final, J_hist, p_hist = gradient_descent(x_train ,y_train, w_init, b_init, tmp_alpha, \n",
|
||||
" iterations, compute_cost, compute_gradient)"
|
||||
],
|
||||
"id": "904051e5b17f7611"
|
||||
"id": "b8c0fc11602dd2b7"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -572,7 +572,7 @@
|
||||
"Above, $w$ and $b$ are bouncing back and forth between positive and negative with the absolute value increasing with each iteration. Further, each iteration $\\frac{\\partial J(w,b)}{\\partial w}$ 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. \n",
|
||||
"Let's visualize this with a plot."
|
||||
],
|
||||
"id": "3e128d8848ed786d"
|
||||
"id": "8b57dc7379bdedc"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -594,7 +594,7 @@
|
||||
"plt_divergence(p_hist, J_hist,x_train, y_train)\n",
|
||||
"plt.show()"
|
||||
],
|
||||
"id": "525859aea07d903"
|
||||
"id": "624c656455504a91"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -602,7 +602,7 @@
|
||||
"source": [
|
||||
"Above, the left graph shows $w$'s progression over the first few steps of gradient descent. $w$ oscillates from positive to negative and cost grows rapidly. Gradient Descent is operating on both $w$ and $b$ simultaneously, so one needs the 3-D plot on the right for the complete picture."
|
||||
],
|
||||
"id": "f188bcb1dbebb9d"
|
||||
"id": "aaeedde0bcfa1065"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -618,15 +618,7 @@
|
||||
"- utilized gradient descent to find parameters\n",
|
||||
"- examined the impact of sizing the learning rate"
|
||||
],
|
||||
"id": "1d1832515c0982de"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [],
|
||||
"id": "2b544fe91032e577"
|
||||
"id": "5eff9781d2060439"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
Reference in New Issue
Block a user