Skip to content

Commit d60a86d

Browse files
committed
minor
1 parent f7caead commit d60a86d

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

pyTorchExample.ipynb

+40-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"source": [
14-
"First, we implement a gradient descent algorithm for reference"
14+
"First, we implement a gradient descent algorithm for reference, using numpy only"
1515
]
1616
},
1717
{
@@ -52,20 +52,31 @@
5252
"#y = np.array([9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74])\n",
5353
"x = np.array([10., 8., 13., 9., 11., 14., 6., 4., 12., 7., 5.])\n",
5454
"N = len(x)\n",
55+
"\n",
56+
"# Design matrix\n",
5557
"A = np.vstack((np.ones(N), x)).T\n",
5658
"\n",
59+
"# Learning rate\n",
5760
"eta = 0.01\n",
5861
" \n",
62+
"# initial parameters\n",
5963
"w = np.array([2., 1.])\n",
64+
"\n",
6065
"for epoch in range(10):\n",
66+
" # Error\n",
6167
" err = y-A.dot(w)\n",
68+
" \n",
69+
" # Total error\n",
6270
" E = np.sum(err**2)/N\n",
71+
" \n",
72+
" # Gradient\n",
6373
" dE = -2.*A.T.dot(err)/N\n",
6474
" \n",
6575
" if epoch%1 == 0: \n",
6676
" print(epoch,':',E)\n",
6777
" # print(w) \n",
68-
" \n",
78+
"\n",
79+
" # Perfom one descent step\n",
6980
" w = w - eta*dE"
7081
]
7182
},
@@ -149,7 +160,9 @@
149160
"cell_type": "markdown",
150161
"metadata": {},
151162
"source": [
152-
"## Implementation in pyTorch"
163+
"## Implementation in pyTorch\n",
164+
"\n",
165+
"### Fitting a line"
153166
]
154167
},
155168
{
@@ -198,12 +211,6 @@
198211
"# Set w_0\n",
199212
"f.bias.data = torch.FloatTensor([[2.]])\n",
200213
"\n",
201-
"#print(f.weight.data)\n",
202-
"#print(f.bias.data)\n",
203-
"#u = f(Variable(x))\n",
204-
"#E = torch.nn.functional.mse_loss(u, Variable(y))\n",
205-
"#E\n",
206-
"\n",
207214
"# learning rate\n",
208215
"eta = 0.01\n",
209216
"\n",
@@ -234,6 +241,13 @@
234241
"#print(f.weight.data, f.bias.data)"
235242
]
236243
},
244+
{
245+
"cell_type": "markdown",
246+
"metadata": {},
247+
"source": [
248+
"### Fitting a polynomial"
249+
]
250+
},
237251
{
238252
"cell_type": "code",
239253
"execution_count": 204,
@@ -476,6 +490,23 @@
476490
"nbconvert_exporter": "python",
477491
"pygments_lexer": "ipython3",
478492
"version": "3.6.1"
493+
},
494+
"latex_envs": {
495+
"LaTeX_envs_menu_present": true,
496+
"autocomplete": true,
497+
"bibliofile": "biblio.bib",
498+
"cite_by": "apalike",
499+
"current_citInitial": 1,
500+
"eqLabelWithNumbers": true,
501+
"eqNumInitial": 1,
502+
"hotkeys": {
503+
"equation": "Ctrl-E",
504+
"itemize": "Ctrl-I"
505+
},
506+
"labels_anchors": false,
507+
"latex_user_defs": false,
508+
"report_style_numbering": false,
509+
"user_envs_cfg": false
479510
}
480511
},
481512
"nbformat": 4,

0 commit comments

Comments
 (0)