Skip to content

Commit abd1d73

Browse files
committed
update week 36
1 parent 1a1618a commit abd1d73

File tree

15 files changed

+668
-119
lines changed

15 files changed

+668
-119
lines changed
4 Bytes
Binary file not shown.
387 Bytes
Binary file not shown.

doc/LectureNotes/_build/html/_sources/exercisesweek37.ipynb

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "1b638450",
5+
"id": "1b941c35",
66
"metadata": {
77
"editable": true
88
},
@@ -14,7 +14,7 @@
1414
},
1515
{
1616
"cell_type": "markdown",
17-
"id": "60060788",
17+
"id": "dc05b096",
1818
"metadata": {
1919
"editable": true
2020
},
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"cell_type": "markdown",
30-
"id": "7cdd88e4",
30+
"id": "2cf07405",
3131
"metadata": {
3232
"editable": true
3333
},
@@ -37,7 +37,7 @@
3737
"After having completed these exercises you will have:\n",
3838
"1. Your own code for the implementation of the simplest gradient descent approach applied to ordinary least squares (OLS) and Ridge regression\n",
3939
"\n",
40-
"2. Be able to compare the analytical expressions for OLS and Rudge regression with the gradient descent approach\n",
40+
"2. Be able to compare the analytical expressions for OLS and Ridge regression with the gradient descent approach\n",
4141
"\n",
4242
"3. Explore the role of the learning rate in the gradient descent approach and the hyperparameter $\\lambda$ in Ridge regression\n",
4343
"\n",
@@ -46,7 +46,7 @@
4646
},
4747
{
4848
"cell_type": "markdown",
49-
"id": "0328fa2a",
49+
"id": "3c139edb",
5050
"metadata": {
5151
"editable": true
5252
},
@@ -58,7 +58,7 @@
5858
},
5959
{
6060
"cell_type": "markdown",
61-
"id": "ac760265",
61+
"id": "aad4cfac",
6262
"metadata": {
6363
"editable": true
6464
},
@@ -70,7 +70,7 @@
7070
},
7171
{
7272
"cell_type": "markdown",
73-
"id": "8d4b0753",
73+
"id": "6682282f",
7474
"metadata": {
7575
"editable": true
7676
},
@@ -83,7 +83,7 @@
8383
},
8484
{
8585
"cell_type": "markdown",
86-
"id": "4517d311",
86+
"id": "89e2f4c4",
8787
"metadata": {
8888
"editable": true
8989
},
@@ -99,7 +99,7 @@
9999
},
100100
{
101101
"cell_type": "markdown",
102-
"id": "da1834a9",
102+
"id": "b06d4e53",
103103
"metadata": {
104104
"editable": true
105105
},
@@ -120,7 +120,7 @@
120120
{
121121
"cell_type": "code",
122122
"execution_count": 1,
123-
"id": "590b2fb0",
123+
"id": "63796480",
124124
"metadata": {
125125
"collapsed": false,
126126
"editable": true
@@ -140,7 +140,7 @@
140140
},
141141
{
142142
"cell_type": "markdown",
143-
"id": "24dd92fc",
143+
"id": "80748600",
144144
"metadata": {
145145
"editable": true
146146
},
@@ -156,7 +156,7 @@
156156
},
157157
{
158158
"cell_type": "markdown",
159-
"id": "4ba80e16",
159+
"id": "92751e5f",
160160
"metadata": {
161161
"editable": true
162162
},
@@ -168,18 +168,18 @@
168168
},
169169
{
170170
"cell_type": "markdown",
171-
"id": "be65f56f",
171+
"id": "aedfbd7a",
172172
"metadata": {
173173
"editable": true
174174
},
175175
"source": [
176-
"## Exercise 3, use the analytical formulae for OLS and Ridge regression to find the optimal paramters $\\boldsymbol{\\theta}$"
176+
"## Exercise 3, using the analytical formulae for OLS and Ridge regression to find the optimal paramters $\\boldsymbol{\\theta}$"
177177
]
178178
},
179179
{
180180
"cell_type": "code",
181181
"execution_count": 2,
182-
"id": "c265677e",
182+
"id": "5d1288fa",
183183
"metadata": {
184184
"collapsed": false,
185185
"editable": true
@@ -200,7 +200,7 @@
200200
},
201201
{
202202
"cell_type": "markdown",
203-
"id": "b989efb9",
203+
"id": "628f5e89",
204204
"metadata": {
205205
"editable": true
206206
},
@@ -214,7 +214,7 @@
214214
},
215215
{
216216
"cell_type": "markdown",
217-
"id": "17b08af7",
217+
"id": "f115ba4e",
218218
"metadata": {
219219
"editable": true
220220
},
@@ -226,7 +226,7 @@
226226
},
227227
{
228228
"cell_type": "markdown",
229-
"id": "6acd4708",
229+
"id": "a9b5189c",
230230
"metadata": {
231231
"editable": true
232232
},
@@ -238,7 +238,7 @@
238238
},
239239
{
240240
"cell_type": "markdown",
241-
"id": "50579422",
241+
"id": "a3969ff6",
242242
"metadata": {
243243
"editable": true
244244
},
@@ -258,7 +258,7 @@
258258
{
259259
"cell_type": "code",
260260
"execution_count": 3,
261-
"id": "c57cc917",
261+
"id": "34d87303",
262262
"metadata": {
263263
"collapsed": false,
264264
"editable": true
@@ -277,13 +277,14 @@
277277
"cost_history = np.zeros(num_iters)\n",
278278
"\n",
279279
"# Gradient descent loop\n",
280-
"m = n_samples # number of examples\n",
280+
"m = n_samples # number of data points\n",
281281
"for t in range(num_iters):\n",
282282
" # Compute prediction error\n",
283283
" error = X_norm.dot(theta) - y_centered \n",
284284
" # Compute cost for OLS and Ridge (MSE + regularization for Ridge) for monitoring\n",
285285
" cost_OLS = ?\n",
286286
" cost_Ridge = ?\n",
287+
" # You could add a history for both methods (optional)\n",
287288
" cost_history[t] = ?\n",
288289
" # Compute gradients for OSL and Ridge\n",
289290
" grad_OLS = ?\n",
@@ -301,7 +302,7 @@
301302
},
302303
{
303304
"cell_type": "markdown",
304-
"id": "e2654903",
305+
"id": "989f70bb",
305306
"metadata": {
306307
"editable": true
307308
},
@@ -313,19 +314,19 @@
313314
},
314315
{
315316
"cell_type": "markdown",
316-
"id": "7e7e7de6",
317+
"id": "370b2dad",
317318
"metadata": {
318319
"editable": true
319320
},
320321
"source": [
321322
"### 4b)\n",
322323
"\n",
323-
"Try to add a stopping parameter as function of the number iterations. How would you define a stopping criterion?"
324+
"Try to add a stopping parameter as function of the number iterations and the difference between the new and old $\\theta$ values. How would you define a stopping criterion?"
324325
]
325326
},
326327
{
327328
"cell_type": "markdown",
328-
"id": "75542708",
329+
"id": "ef197cd7",
329330
"metadata": {
330331
"editable": true
331332
},
@@ -351,7 +352,7 @@
351352
{
352353
"cell_type": "code",
353354
"execution_count": 4,
354-
"id": "06077986",
355+
"id": "4ccc2f65",
355356
"metadata": {
356357
"collapsed": false,
357358
"editable": true
@@ -380,7 +381,7 @@
380381
},
381382
{
382383
"cell_type": "markdown",
383-
"id": "86d46505",
384+
"id": "00e279ef",
384385
"metadata": {
385386
"editable": true
386387
},
@@ -394,7 +395,7 @@
394395
},
395396
{
396397
"cell_type": "markdown",
397-
"id": "f1d2f848",
398+
"id": "c910b3f4",
398399
"metadata": {
399400
"editable": true
400401
},
@@ -406,7 +407,7 @@
406407
},
407408
{
408409
"cell_type": "markdown",
409-
"id": "f1c91e84",
410+
"id": "89e6e040",
410411
"metadata": {
411412
"editable": true
412413
},

doc/LectureNotes/_build/html/exercisesweek37.html

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ <h2> Contents </h2>
389389
</ul>
390390
</li>
391391
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-2-calculate-the-gradients">Exercise 2, calculate the gradients</a></li>
392-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-3-use-the-analytical-formulae-for-ols-and-ridge-regression-to-find-the-optimal-paramters-boldsymbol-theta">Exercise 3, use the analytical formulae for OLS and Ridge regression to find the optimal paramters <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span></a><ul class="nav section-nav flex-column">
392+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-3-using-the-analytical-formulae-for-ols-and-ridge-regression-to-find-the-optimal-paramters-boldsymbol-theta">Exercise 3, using the analytical formulae for OLS and Ridge regression to find the optimal paramters <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span></a><ul class="nav section-nav flex-column">
393393
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#id1">3a)</a></li>
394394
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#b">3b)</a></li>
395395
</ul>
@@ -422,7 +422,7 @@ <h2>Learning goals<a class="headerlink" href="#learning-goals" title="Link to th
422422
<p>After having completed these exercises you will have:</p>
423423
<ol class="arabic simple">
424424
<li><p>Your own code for the implementation of the simplest gradient descent approach applied to ordinary least squares (OLS) and Ridge regression</p></li>
425-
<li><p>Be able to compare the analytical expressions for OLS and Rudge regression with the gradient descent approach</p></li>
425+
<li><p>Be able to compare the analytical expressions for OLS and Ridge regression with the gradient descent approach</p></li>
426426
<li><p>Explore the role of the learning rate in the gradient descent approach and the hyperparameter <span class="math notranslate nohighlight">\(\lambda\)</span> in Ridge regression</p></li>
427427
<li><p>Scale the data properly</p></li>
428428
</ol>
@@ -482,8 +482,8 @@ <h3>1a)<a class="headerlink" href="#a" title="Link to this heading">#</a></h3>
482482
<h2>Exercise 2, calculate the gradients<a class="headerlink" href="#exercise-2-calculate-the-gradients" title="Link to this heading">#</a></h2>
483483
<p>Find the gradients for OLS and Ridge regression using the mean-squared error as cost/loss function.</p>
484484
</section>
485-
<section id="exercise-3-use-the-analytical-formulae-for-ols-and-ridge-regression-to-find-the-optimal-paramters-boldsymbol-theta">
486-
<h2>Exercise 3, use the analytical formulae for OLS and Ridge regression to find the optimal paramters <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span><a class="headerlink" href="#exercise-3-use-the-analytical-formulae-for-ols-and-ridge-regression-to-find-the-optimal-paramters-boldsymbol-theta" title="Link to this heading">#</a></h2>
485+
<section id="exercise-3-using-the-analytical-formulae-for-ols-and-ridge-regression-to-find-the-optimal-paramters-boldsymbol-theta">
486+
<h2>Exercise 3, using the analytical formulae for OLS and Ridge regression to find the optimal paramters <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span><a class="headerlink" href="#exercise-3-using-the-analytical-formulae-for-ols-and-ridge-regression-to-find-the-optimal-paramters-boldsymbol-theta" title="Link to this heading">#</a></h2>
487487
<div class="cell docutils container">
488488
<div class="cell_input docutils container">
489489
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># Set regularization parameter, either a single value or a vector of values
@@ -537,13 +537,14 @@ <h2>Exercise 4, Implementing the simplest form for gradient descent<a class="hea
537537
cost_history = np.zeros(num_iters)
538538

539539
# Gradient descent loop
540-
m = n_samples # number of examples
540+
m = n_samples # number of data points
541541
for t in range(num_iters):
542542
# Compute prediction error
543543
error = X_norm.dot(theta) - y_centered
544544
# Compute cost for OLS and Ridge (MSE + regularization for Ridge) for monitoring
545545
cost_OLS = ?
546546
cost_Ridge = ?
547+
# You could add a history for both methods (optional)
547548
cost_history[t] = ?
548549
# Compute gradients for OSL and Ridge
549550
grad_OLS = ?
@@ -567,7 +568,7 @@ <h3>4a)<a class="headerlink" href="#id2" title="Link to this heading">#</a></h3>
567568
</section>
568569
<section id="id3">
569570
<h3>4b)<a class="headerlink" href="#id3" title="Link to this heading">#</a></h3>
570-
<p>Try to add a stopping parameter as function of the number iterations. How would you define a stopping criterion?</p>
571+
<p>Try to add a stopping parameter as function of the number iterations and the difference between the new and old <span class="math notranslate nohighlight">\(\theta\)</span> values. How would you define a stopping criterion?</p>
571572
</section>
572573
</section>
573574
<section id="exercise-5-ridge-regression-and-a-new-synthetic-dataset">
@@ -697,7 +698,7 @@ <h2>Exercise 5, Ridge regression and a new Synthetic Dataset<a class="headerlink
697698
</ul>
698699
</li>
699700
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-2-calculate-the-gradients">Exercise 2, calculate the gradients</a></li>
700-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-3-use-the-analytical-formulae-for-ols-and-ridge-regression-to-find-the-optimal-paramters-boldsymbol-theta">Exercise 3, use the analytical formulae for OLS and Ridge regression to find the optimal paramters <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span></a><ul class="nav section-nav flex-column">
701+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-3-using-the-analytical-formulae-for-ols-and-ridge-regression-to-find-the-optimal-paramters-boldsymbol-theta">Exercise 3, using the analytical formulae for OLS and Ridge regression to find the optimal paramters <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span></a><ul class="nav section-nav flex-column">
701702
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#id1">3a)</a></li>
702703
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#b">3b)</a></li>
703704
</ul>

doc/LectureNotes/_build/html/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)