Skip to content

Commit f8fa01a

Browse files
committed
explain while loop; define learning rate
1 parent bf4a35a commit f8fa01a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Diff for: notebooks_en/2_Logistic_Regression.ipynb

+11-4
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@
425425
"pip install autograd\n",
426426
"```\n",
427427
"\n",
428-
"or, if you installed your Python environment via Anaconda, you can run this command:\n",
428+
"Or, if you installed your Python environment via [Anaconda](https://www.anaconda.com/products/individual), you can run this command:\n",
429429
"```\n",
430430
"conda install -c conda-forge autograd\n",
431431
"```\n",
@@ -526,6 +526,7 @@
526526
"##### Note:\n",
527527
"\n",
528528
"> The argument list of the function returned by `grad()` will be the same as the argument list of the loss function that we input to it.\n",
529+
"> The default setting of `grad()` is to return the derivative(s) with respect to the first argument, in this case, the two-element array `params`. This gives us the two derivatives (with respect to $w$ and $b$) at once.\n",
529530
"\n",
530531
"Let's now make a random starting guess for the two model parameters:"
531532
]
@@ -536,6 +537,7 @@
536537
"metadata": {},
537538
"outputs": [],
538539
"source": [
540+
"numpy.random.seed(0)\n",
539541
"params = np.random.rand(2)\n",
540542
"print(params)"
541543
]
@@ -560,7 +562,12 @@
560562
"cell_type": "markdown",
561563
"metadata": {},
562564
"source": [
563-
"Now we optimize! Notice that we set both a maximum number of iterations, and an exit criterion based on two successive residuals being very close. \n",
565+
"Now we optimize! Notice that in this optimization loop we chose to use a `while` statement, instead of `for`. \n",
566+
"We set both a maximum number of iterations, and an exit criterion based on the norm of the gradient being very small.\n",
567+
"Already you should be thinking about these choices we make:\n",
568+
"\n",
569+
"- The gradient is multiplied by a small step of $0.01$: this is called the _learning rate_. How do we choose this value?\n",
570+
"- The `while` loop exits when the norm of the gradient is $0.001$: how do we know if this is a good choice?\n",
564571
"\n",
565572
"Finally, we plot the synthetic data we created above, and the logistic regression curve corresponding to the parameters found in the optimization loop."
566573
]
@@ -571,9 +578,9 @@
571578
"metadata": {},
572579
"outputs": [],
573580
"source": [
574-
"max_iter = 3000\n",
581+
"max_iter = 5000\n",
575582
"i = 0\n",
576-
"descent = np.ones(len(x_data))\n",
583+
"descent = np.ones(len(params))\n",
577584
"\n",
578585
"while np.linalg.norm(descent) > 0.001 and i < max_iter:\n",
579586
"\n",

0 commit comments

Comments
 (0)