import torch
from torch.autograd import Variable

dtype = torch.FloatTensor
N, D_in, H, D_out = 64, 1000, 100, 10 
# one input layer, one hidden layer, and one output layer

x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False)
w1 = Variable(torch.randn(D_in, H).type(dtype), requires_grad=True)
w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True)
learning_rate = 1e-6

for t in range(500):
    y_pred = x.mm(w1).clamp(min=0).mm(w2)
    loss = (y_pred - y).pow(2).sum()
    print(t, loss.data)
    loss.backward()
    w1.data -= learning_rate * w1.grad
    w2.data -= learning_rate * w2.grad
    w1.grad.zero_()
    w2.grad.zero_()

输出

0 tensor(33538672.)
1 tensor(29973312.)
2 tensor(26784652.)
3 tensor(21361500.)
4 tensor(14865291.)
5 tensor(9206818.)
6 tensor(5481059.)
7 tensor(3352937.5000)
8 tensor(2212013.7500)
9 tensor(1584447.7500)
10 tensor(1214588.1250)
11 tensor(975241.6250)
12 tensor(806804.3750)
13 tensor(680432.1875)
14 tensor(580979.7500)
15 tensor(500418.2188)
16 tensor(433949.5000)
17 tensor(378533.2500)
18 tensor(331744.8125)
19 tensor(292014.9375)
20 tensor(258002.2969)
21 tensor(228759.7031)
22 tensor(203502.3438)
23 tensor(181617.4062)
24 tensor(162530.6094)
25 tensor(145835.1406)
26 tensor(131164.5469)
27 tensor(118248.6328)
28 tensor(106880.9688)
29 tensor(96805.8125)
30 tensor(87843.5078)
31 tensor(79855.7891)
32 tensor(72728.5000)
33 tensor(66353.7422)
34 tensor(60627.9375)
35 tensor(55480.1055)
36 tensor(50842.4727)
37 tensor(46652.8438)
38 tensor(42859.6172)
39 tensor(39420.3984)
40 tensor(36297.3594)
41 tensor(33456.6836)
42 tensor(30868.9805)
43 tensor(28508.7754)
44 tensor(26352.9551)
45 tensor(24381.9219)
46 tensor(22576.9590)
47 tensor(20924.1289)
48 tensor(19406.6504)
49 tensor(18014.0859)
50 tensor(16734.3945)
51 tensor(15556.9463)
52 tensor(14473.6260)
53 tensor(13474.7432)
54 tensor(12552.1865)
55 tensor(11700.5293)
56 tensor(10912.8838)
57 tensor(10183.5830)
58 tensor(9508.0732)
59 tensor(8882.0479)
60 tensor(8301.2988)
61 tensor(7762.2920)
62 tensor(7261.7388)
63 tensor(6796.4380)
64 tensor(6363.9053)
65 tensor(5961.3506)
66 tensor(5586.3672)
67 tensor(5237.0176)
68 tensor(4911.4233)
69 tensor(4607.7842)
70 tensor(4324.3721)
71 tensor(4059.8486)
72 tensor(3812.7903)
73 tensor(3581.8992)
74 tensor(3366.1370)
75 tensor(3164.2825)
76 tensor(2975.8660)
77 tensor(2799.4905)
78 tensor(2634.3201)
79 tensor(2479.5688)
80 tensor(2334.5366)
81 tensor(2198.5542)
82 tensor(2070.9956)
83 tensor(1951.3154)
84 tensor(1838.9900)
85 tensor(1733.5266)
86 tensor(1634.4674)
87 tensor(1541.3926)
88 tensor(1453.9283)
89 tensor(1371.7056)
90 tensor(1294.3995)
91 tensor(1221.7031)
92 tensor(1153.3300)
93 tensor(1088.9634)
94 tensor(1028.3733)
95 tensor(971.3359)
96 tensor(917.6345)
97 tensor(867.0400)
98 tensor(819.3739)
99 tensor(774.4464)
100 tensor(732.1039)
101 tensor(692.1805)
102 tensor(654.5535)
103 tensor(619.0535)
104 tensor(585.5612)
105 tensor(553.9610)
106 tensor(524.1429)
107 tensor(495.9940)
108 tensor(469.4165)
109 tensor(444.3282)
110 tensor(420.6277)
111 tensor(398.2485)
112 tensor(377.1102)
113 tensor(357.1321)
114 tensor(338.2508)
115 tensor(320.4093)
116 tensor(303.5409)
117 tensor(287.5944)
118 tensor(272.5143)
119 tensor(258.2510)
120 tensor(244.7672)
121 tensor(232.0098)
122 tensor(219.9384)
123 tensor(208.5144)
124 tensor(197.7036)
125 tensor(187.4775)
126 tensor(177.7916)
127 tensor(168.6223)
128 tensor(159.9440)
129 tensor(151.7250)
130 tensor(143.9390)
131 tensor(136.5644)
132 tensor(129.5799)
133 tensor(122.9632)
134 tensor(116.6942)
135 tensor(110.7556)
136 tensor(105.1254)
137 tensor(99.7922)
138 tensor(94.7351)
139 tensor(89.9417)
140 tensor(85.3960)
141 tensor(81.0888)
142 tensor(77.0021)
143 tensor(73.1286)
144 tensor(69.4550)
145 tensor(65.9725)
146 tensor(62.6662)
147 tensor(59.5297)
148 tensor(56.5552)
149 tensor(53.7328)
150 tensor(51.0556)
151 tensor(48.5134)
152 tensor(46.1019)
153 tensor(43.8134)
154 tensor(41.6410)
155 tensor(39.5785)
156 tensor(37.6204)
157 tensor(35.7611)
158 tensor(33.9956)
159 tensor(32.3195)
160 tensor(30.7282)
161 tensor(29.2167)
162 tensor(27.7806)
163 tensor(26.4171)
164 tensor(25.1230)
165 tensor(23.8926)
166 tensor(22.7236)
167 tensor(21.6127)
168 tensor(20.5574)
169 tensor(19.5545)
170 tensor(18.6020)
171 tensor(17.6969)
172 tensor(16.8359)
173 tensor(16.0185)
174 tensor(15.2415)
175 tensor(14.5024)
176 tensor(13.8001)
177 tensor(13.1326)
178 tensor(12.4976)
179 tensor(11.8940)
180 tensor(11.3202)
181 tensor(10.7745)
182 tensor(10.2554)
183 tensor(9.7622)
184 tensor(9.2926)
185 tensor(8.8464)
186 tensor(8.4218)
187 tensor(8.0179)
188 tensor(7.6338)
189 tensor(7.2685)
190 tensor(6.9206)
191 tensor(6.5901)
192 tensor(6.2753)
193 tensor(5.9762)
194 tensor(5.6914)
195 tensor(5.4201)
196 tensor(5.1624)
197 tensor(4.9167)
198 tensor(4.6833)
199 tensor(4.4609)
200 tensor(4.2493)
201 tensor(4.0482)
202 tensor(3.8562)
203 tensor(3.6738)
204 tensor(3.5002)
205 tensor(3.3344)
206 tensor(3.1773)
207 tensor(3.0273)
208 tensor(2.8845)
209 tensor(2.7487)
210 tensor(2.6194)
211 tensor(2.4963)
212 tensor(2.3788)
213 tensor(2.2671)
214 tensor(2.1607)
215 tensor(2.0594)
216 tensor(1.9629)
217 tensor(1.8709)
218 tensor(1.7832)
219 tensor(1.6998)
220 tensor(1.6205)
221 tensor(1.5449)
222 tensor(1.4727)
223 tensor(1.4040)
224 tensor(1.3386)
225 tensor(1.2762)
226 tensor(1.2168)
227 tensor(1.1603)
228 tensor(1.1063)
229 tensor(1.0549)
230 tensor(1.0058)
231 tensor(0.9592)
232 tensor(0.9148)
233 tensor(0.8725)
234 tensor(0.8320)
235 tensor(0.7934)
236 tensor(0.7568)
237 tensor(0.7219)
238 tensor(0.6885)
239 tensor(0.6568)
240 tensor(0.6264)
241 tensor(0.5976)
242 tensor(0.5700)
243 tensor(0.5438)
244 tensor(0.5188)
245 tensor(0.4949)
246 tensor(0.4722)
247 tensor(0.4504)
248 tensor(0.4298)
249 tensor(0.4101)
250 tensor(0.3913)
251 tensor(0.3734)
252 tensor(0.3562)
253 tensor(0.3400)
254 tensor(0.3244)
255 tensor(0.3096)
256 tensor(0.2955)
257 tensor(0.2820)
258 tensor(0.2691)
259 tensor(0.2568)
260 tensor(0.2452)
261 tensor(0.2340)
262 tensor(0.2234)
263 tensor(0.2132)
264 tensor(0.2035)
265 tensor(0.1943)
266 tensor(0.1855)
267 tensor(0.1771)
268 tensor(0.1690)
269 tensor(0.1614)
270 tensor(0.1541)
271 tensor(0.1471)
272 tensor(0.1404)
273 tensor(0.1341)
274 tensor(0.1280)
275 tensor(0.1222)
276 tensor(0.1167)
277 tensor(0.1115)
278 tensor(0.1064)
279 tensor(0.1016)
280 tensor(0.0971)
281 tensor(0.0927)
282 tensor(0.0886)
283 tensor(0.0846)
284 tensor(0.0808)
285 tensor(0.0772)
286 tensor(0.0737)
287 tensor(0.0704)
288 tensor(0.0673)
289 tensor(0.0642)
290 tensor(0.0614)
291 tensor(0.0586)
292 tensor(0.0560)
293 tensor(0.0535)
294 tensor(0.0511)
295 tensor(0.0488)
296 tensor(0.0467)
297 tensor(0.0446)
298 tensor(0.0426)
299 tensor(0.0407)
300 tensor(0.0389)
301 tensor(0.0372)
302 tensor(0.0355)
303 tensor(0.0340)
304 tensor(0.0325)
305 tensor(0.0310)
306 tensor(0.0297)
307 tensor(0.0284)
308 tensor(0.0271)
309 tensor(0.0259)
310 tensor(0.0248)
311 tensor(0.0237)
312 tensor(0.0226)
313 tensor(0.0217)
314 tensor(0.0207)
315 tensor(0.0198)
316 tensor(0.0189)
317 tensor(0.0181)
318 tensor(0.0173)
319 tensor(0.0166)
320 tensor(0.0158)
321 tensor(0.0152)
322 tensor(0.0145)
323 tensor(0.0139)
324 tensor(0.0133)
325 tensor(0.0127)
326 tensor(0.0122)
327 tensor(0.0116)
328 tensor(0.0111)
329 tensor(0.0107)
330 tensor(0.0102)
331 tensor(0.0098)
332 tensor(0.0094)
333 tensor(0.0090)
334 tensor(0.0086)
335 tensor(0.0082)
336 tensor(0.0079)
337 tensor(0.0075)
338 tensor(0.0072)
339 tensor(0.0069)
340 tensor(0.0066)
341 tensor(0.0064)
342 tensor(0.0061)
343 tensor(0.0059)
344 tensor(0.0056)
345 tensor(0.0054)
346 tensor(0.0052)
347 tensor(0.0050)
348 tensor(0.0048)
349 tensor(0.0046)
350 tensor(0.0044)
351 tensor(0.0042)
352 tensor(0.0040)
353 tensor(0.0039)
354 tensor(0.0037)
355 tensor(0.0036)
356 tensor(0.0034)
357 tensor(0.0033)
358 tensor(0.0032)
359 tensor(0.0031)
360 tensor(0.0029)
361 tensor(0.0028)
362 tensor(0.0027)
363 tensor(0.0026)
364 tensor(0.0025)
365 tensor(0.0024)
366 tensor(0.0023)
367 tensor(0.0022)
368 tensor(0.0022)
369 tensor(0.0021)
370 tensor(0.0020)
371 tensor(0.0019)
372 tensor(0.0019)
373 tensor(0.0018)
374 tensor(0.0017)
375 tensor(0.0017)
376 tensor(0.0016)
377 tensor(0.0016)
378 tensor(0.0015)
379 tensor(0.0015)
380 tensor(0.0014)
381 tensor(0.0014)
382 tensor(0.0013)
383 tensor(0.0013)
384 tensor(0.0012)
385 tensor(0.0012)
386 tensor(0.0011)
387 tensor(0.0011)
388 tensor(0.0011)
389 tensor(0.0010)
390 tensor(0.0010)
391 tensor(0.0010)
392 tensor(0.0009)
393 tensor(0.0009)
394 tensor(0.0009)
395 tensor(0.0009)
396 tensor(0.0008)
397 tensor(0.0008)
398 tensor(0.0008)
399 tensor(0.0008)
400 tensor(0.0007)
401 tensor(0.0007)
402 tensor(0.0007)
403 tensor(0.0007)
404 tensor(0.0006)
405 tensor(0.0006)
406 tensor(0.0006)
407 tensor(0.0006)
408 tensor(0.0006)
409 tensor(0.0006)
410 tensor(0.0005)
411 tensor(0.0005)
412 tensor(0.0005)
413 tensor(0.0005)
414 tensor(0.0005)
415 tensor(0.0005)
416 tensor(0.0005)
417 tensor(0.0004)
418 tensor(0.0004)
419 tensor(0.0004)
420 tensor(0.0004)
421 tensor(0.0004)
422 tensor(0.0004)
423 tensor(0.0004)
424 tensor(0.0004)
425 tensor(0.0004)
426 tensor(0.0004)
427 tensor(0.0003)
428 tensor(0.0003)
429 tensor(0.0003)
430 tensor(0.0003)
431 tensor(0.0003)
432 tensor(0.0003)
433 tensor(0.0003)
434 tensor(0.0003)
435 tensor(0.0003)
436 tensor(0.0003)
437 tensor(0.0003)
438 tensor(0.0003)
439 tensor(0.0003)
440 tensor(0.0003)
441 tensor(0.0002)
442 tensor(0.0002)
443 tensor(0.0002)
444 tensor(0.0002)
445 tensor(0.0002)
446 tensor(0.0002)
447 tensor(0.0002)
448 tensor(0.0002)
449 tensor(0.0002)
450 tensor(0.0002)
451 tensor(0.0002)
452 tensor(0.0002)
453 tensor(0.0002)
454 tensor(0.0002)
455 tensor(0.0002)
456 tensor(0.0002)
457 tensor(0.0002)
458 tensor(0.0002)
459 tensor(0.0002)
460 tensor(0.0002)
461 tensor(0.0002)
462 tensor(0.0002)
463 tensor(0.0002)
464 tensor(0.0001)
465 tensor(0.0001)
466 tensor(0.0001)
467 tensor(0.0001)
468 tensor(0.0001)
469 tensor(0.0001)
470 tensor(0.0001)
471 tensor(0.0001)
472 tensor(0.0001)
473 tensor(0.0001)
474 tensor(0.0001)
475 tensor(0.0001)
476 tensor(0.0001)
477 tensor(0.0001)
478 tensor(0.0001)
479 tensor(0.0001)
480 tensor(0.0001)
481 tensor(0.0001)
482 tensor(0.0001)
483 tensor(0.0001)
484 tensor(0.0001)
485 tensor(0.0001)
486 tensor(9.8671e-05)
487 tensor(9.6656e-05)
488 tensor(9.4918e-05)
489 tensor(9.3419e-05)
490 tensor(9.1719e-05)
491 tensor(9.0300e-05)
492 tensor(8.8702e-05)
493 tensor(8.7077e-05)
494 tensor(8.5830e-05)
495 tensor(8.4661e-05)
496 tensor(8.3026e-05)
497 tensor(8.1507e-05)
498 tensor(8.0135e-05)
499 tensor(7.8957e-05)

Reference
https://github.com/llSourcell/pytorch_in_5_minutes