You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/LectureNotes/_build/html/_sources/exercisesweek37.ipynb
+30-29Lines changed: 30 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
"cells": [
3
3
{
4
4
"cell_type": "markdown",
5
-
"id": "1b638450",
5
+
"id": "1b941c35",
6
6
"metadata": {
7
7
"editable": true
8
8
},
@@ -14,7 +14,7 @@
14
14
},
15
15
{
16
16
"cell_type": "markdown",
17
-
"id": "60060788",
17
+
"id": "dc05b096",
18
18
"metadata": {
19
19
"editable": true
20
20
},
@@ -27,7 +27,7 @@
27
27
},
28
28
{
29
29
"cell_type": "markdown",
30
-
"id": "7cdd88e4",
30
+
"id": "2cf07405",
31
31
"metadata": {
32
32
"editable": true
33
33
},
@@ -37,7 +37,7 @@
37
37
"After having completed these exercises you will have:\n",
38
38
"1. Your own code for the implementation of the simplest gradient descent approach applied to ordinary least squares (OLS) and Ridge regression\n",
39
39
"\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",
41
41
"\n",
42
42
"3. Explore the role of the learning rate in the gradient descent approach and the hyperparameter $\\lambda$ in Ridge regression\n",
43
43
"\n",
@@ -46,7 +46,7 @@
46
46
},
47
47
{
48
48
"cell_type": "markdown",
49
-
"id": "0328fa2a",
49
+
"id": "3c139edb",
50
50
"metadata": {
51
51
"editable": true
52
52
},
@@ -58,7 +58,7 @@
58
58
},
59
59
{
60
60
"cell_type": "markdown",
61
-
"id": "ac760265",
61
+
"id": "aad4cfac",
62
62
"metadata": {
63
63
"editable": true
64
64
},
@@ -70,7 +70,7 @@
70
70
},
71
71
{
72
72
"cell_type": "markdown",
73
-
"id": "8d4b0753",
73
+
"id": "6682282f",
74
74
"metadata": {
75
75
"editable": true
76
76
},
@@ -83,7 +83,7 @@
83
83
},
84
84
{
85
85
"cell_type": "markdown",
86
-
"id": "4517d311",
86
+
"id": "89e2f4c4",
87
87
"metadata": {
88
88
"editable": true
89
89
},
@@ -99,7 +99,7 @@
99
99
},
100
100
{
101
101
"cell_type": "markdown",
102
-
"id": "da1834a9",
102
+
"id": "b06d4e53",
103
103
"metadata": {
104
104
"editable": true
105
105
},
@@ -120,7 +120,7 @@
120
120
{
121
121
"cell_type": "code",
122
122
"execution_count": 1,
123
-
"id": "590b2fb0",
123
+
"id": "63796480",
124
124
"metadata": {
125
125
"collapsed": false,
126
126
"editable": true
@@ -140,7 +140,7 @@
140
140
},
141
141
{
142
142
"cell_type": "markdown",
143
-
"id": "24dd92fc",
143
+
"id": "80748600",
144
144
"metadata": {
145
145
"editable": true
146
146
},
@@ -156,7 +156,7 @@
156
156
},
157
157
{
158
158
"cell_type": "markdown",
159
-
"id": "4ba80e16",
159
+
"id": "92751e5f",
160
160
"metadata": {
161
161
"editable": true
162
162
},
@@ -168,18 +168,18 @@
168
168
},
169
169
{
170
170
"cell_type": "markdown",
171
-
"id": "be65f56f",
171
+
"id": "aedfbd7a",
172
172
"metadata": {
173
173
"editable": true
174
174
},
175
175
"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}$"
177
177
]
178
178
},
179
179
{
180
180
"cell_type": "code",
181
181
"execution_count": 2,
182
-
"id": "c265677e",
182
+
"id": "5d1288fa",
183
183
"metadata": {
184
184
"collapsed": false,
185
185
"editable": true
@@ -200,7 +200,7 @@
200
200
},
201
201
{
202
202
"cell_type": "markdown",
203
-
"id": "b989efb9",
203
+
"id": "628f5e89",
204
204
"metadata": {
205
205
"editable": true
206
206
},
@@ -214,7 +214,7 @@
214
214
},
215
215
{
216
216
"cell_type": "markdown",
217
-
"id": "17b08af7",
217
+
"id": "f115ba4e",
218
218
"metadata": {
219
219
"editable": true
220
220
},
@@ -226,7 +226,7 @@
226
226
},
227
227
{
228
228
"cell_type": "markdown",
229
-
"id": "6acd4708",
229
+
"id": "a9b5189c",
230
230
"metadata": {
231
231
"editable": true
232
232
},
@@ -238,7 +238,7 @@
238
238
},
239
239
{
240
240
"cell_type": "markdown",
241
-
"id": "50579422",
241
+
"id": "a3969ff6",
242
242
"metadata": {
243
243
"editable": true
244
244
},
@@ -258,7 +258,7 @@
258
258
{
259
259
"cell_type": "code",
260
260
"execution_count": 3,
261
-
"id": "c57cc917",
261
+
"id": "34d87303",
262
262
"metadata": {
263
263
"collapsed": false,
264
264
"editable": true
@@ -277,13 +277,14 @@
277
277
"cost_history = np.zeros(num_iters)\n",
278
278
"\n",
279
279
"# Gradient descent loop\n",
280
-
"m = n_samples # number of examples\n",
280
+
"m = n_samples # number of data points\n",
281
281
"for t in range(num_iters):\n",
282
282
" # Compute prediction error\n",
283
283
" error = X_norm.dot(theta) - y_centered \n",
284
284
" # Compute cost for OLS and Ridge (MSE + regularization for Ridge) for monitoring\n",
285
285
" cost_OLS = ?\n",
286
286
" cost_Ridge = ?\n",
287
+
" # You could add a history for both methods (optional)\n",
287
288
" cost_history[t] = ?\n",
288
289
" # Compute gradients for OSL and Ridge\n",
289
290
" grad_OLS = ?\n",
@@ -301,7 +302,7 @@
301
302
},
302
303
{
303
304
"cell_type": "markdown",
304
-
"id": "e2654903",
305
+
"id": "989f70bb",
305
306
"metadata": {
306
307
"editable": true
307
308
},
@@ -313,19 +314,19 @@
313
314
},
314
315
{
315
316
"cell_type": "markdown",
316
-
"id": "7e7e7de6",
317
+
"id": "370b2dad",
317
318
"metadata": {
318
319
"editable": true
319
320
},
320
321
"source": [
321
322
"### 4b)\n",
322
323
"\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?"
Copy file name to clipboardExpand all lines: doc/LectureNotes/_build/html/exercisesweek37.html
+8-7Lines changed: 8 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -389,7 +389,7 @@ <h2> Contents </h2>
389
389
</ul>
390
390
</li>
391
391
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#exercise-2-calculate-the-gradients">Exercise 2, calculate the gradients</a></li>
392
-
<liclass="toc-h2 nav-item toc-entry"><aclass="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 <spanclass="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span></a><ulclass="nav section-nav flex-column">
392
+
<liclass="toc-h2 nav-item toc-entry"><aclass="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 <spanclass="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span></a><ulclass="nav section-nav flex-column">
<p>After having completed these exercises you will have:</p>
423
423
<olclass="arabic simple">
424
424
<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>
426
426
<li><p>Explore the role of the learning rate in the gradient descent approach and the hyperparameter <spanclass="math notranslate nohighlight">\(\lambda\)</span> in Ridge regression</p></li>
427
427
<li><p>Scale the data properly</p></li>
428
428
</ol>
@@ -482,8 +482,8 @@ <h3>1a)<a class="headerlink" href="#a" title="Link to this heading">#</a></h3>
482
482
<h2>Exercise 2, calculate the gradients<aclass="headerlink" href="#exercise-2-calculate-the-gradients" title="Link to this heading">#</a></h2>
483
483
<p>Find the gradients for OLS and Ridge regression using the mean-squared error as cost/loss function.</p>
<h2>Exercise 3, use the analytical formulae for OLS and Ridge regression to find the optimal paramters <spanclass="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span><aclass="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>
<h2>Exercise 3, using the analytical formulae for OLS and Ridge regression to find the optimal paramters <spanclass="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span><aclass="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>
487
487
<divclass="cell docutils container">
488
488
<divclass="cell_input docutils container">
489
489
<divclass="highlight-none notranslate"><divclass="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
537
537
cost_history = np.zeros(num_iters)
538
538
539
539
# Gradient descent loop
540
-
m = n_samples # number of examples
540
+
m = n_samples # number of data points
541
541
for t in range(num_iters):
542
542
# Compute prediction error
543
543
error = X_norm.dot(theta) - y_centered
544
544
# Compute cost for OLS and Ridge (MSE + regularization for Ridge) for monitoring
545
545
cost_OLS = ?
546
546
cost_Ridge = ?
547
+
# You could add a history for both methods (optional)
547
548
cost_history[t] = ?
548
549
# Compute gradients for OSL and Ridge
549
550
grad_OLS = ?
@@ -567,7 +568,7 @@ <h3>4a)<a class="headerlink" href="#id2" title="Link to this heading">#</a></h3>
567
568
</section>
568
569
<sectionid="id3">
569
570
<h3>4b)<aclass="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 <spanclass="math notranslate nohighlight">\(\theta\)</span> values. How would you define a stopping criterion?</p>
@@ -697,7 +698,7 @@ <h2>Exercise 5, Ridge regression and a new Synthetic Dataset<a class="headerlink
697
698
</ul>
698
699
</li>
699
700
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#exercise-2-calculate-the-gradients">Exercise 2, calculate the gradients</a></li>
700
-
<liclass="toc-h2 nav-item toc-entry"><aclass="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 <spanclass="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span></a><ulclass="nav section-nav flex-column">
701
+
<liclass="toc-h2 nav-item toc-entry"><aclass="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 <spanclass="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span></a><ulclass="nav section-nav flex-column">
0 commit comments