1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
|
{
"cells": [
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"\n",
"What is `torch.nn` *really*?\n",
"============================\n",
"by Jeremy Howard, `fast.ai <https://www.fast.ai>`_. Thanks to Rachel Thomas and Francisco Ingham.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"We recommend running this tutorial as a notebook, not a script. To download the notebook (.ipynb) file,\n",
"click the link at the top of the page.\n",
"\n",
"PyTorch provides the elegantly designed modules and classes `torch.nn <https://pytorch.org/docs/stable/nn.html>`_ ,\n",
"`torch.optim <https://pytorch.org/docs/stable/optim.html>`_ ,\n",
"`Dataset <https://pytorch.org/docs/stable/data.html?highlight=dataset#torch.utils.data.Dataset>`_ ,\n",
"and `DataLoader <https://pytorch.org/docs/stable/data.html?highlight=dataloader#torch.utils.data.DataLoader>`_\n",
"to help you create and train neural networks.\n",
"In order to fully utilize their power and customize\n",
"them for your problem, you need to really understand exactly what they're\n",
"doing. To develop this understanding, we will first train basic neural net\n",
"on the MNIST data set without using any features from these models; we will\n",
"initially only use the most basic PyTorch tensor functionality. Then, we will\n",
"incrementally add one feature from ``torch.nn``, ``torch.optim``, ``Dataset``, or\n",
"``DataLoader`` at a time, showing exactly what each piece does, and how it\n",
"works to make the code either more concise, or more flexible.\n",
"\n",
"**This tutorial assumes you already have PyTorch installed, and are familiar\n",
"with the basics of tensor operations.** (If you're familiar with Numpy array\n",
"operations, you'll find the PyTorch tensor operations used here nearly identical).\n",
"\n",
"MNIST data setup\n",
"----------------\n",
"\n",
"We will use the classic `MNIST <http://deeplearning.net/data/mnist/>`_ dataset,\n",
"which consists of black-and-white images of hand-drawn digits (between 0 and 9).\n",
"\n",
"We will use `pathlib <https://docs.python.org/3/library/pathlib.html>`_\n",
"for dealing with paths (part of the Python 3 standard library), and will\n",
"download the dataset using\n",
"`requests <http://docs.python-requests.org/en/master/>`_. We will only\n",
"import modules when we use them, so you can see exactly what's being\n",
"used at each point.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"jupyter": {
}
},
"outputs": [
],
"source": [
"from pathlib import Path\n",
"import requests\n",
"\n",
"DATA_PATH = Path(\"data\")\n",
"PATH = DATA_PATH / \"mnist\"\n",
"\n",
"PATH.mkdir(parents=True, exist_ok=True)\n",
"\n",
"URL = \"https://github.com/pytorch/tutorials/raw/master/_static/\"\n",
"FILENAME = \"mnist.pkl.gz\"\n",
"\n",
"if not (PATH / FILENAME).exists():\n",
" content = requests.get(URL + FILENAME).content\n",
" (PATH / FILENAME).open(\"wb\").write(content)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"This dataset is in numpy array format, and has been stored using pickle,\n",
"a python-specific format for serializing data.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
}
},
"outputs": [
],
"source": [
"import pickle\n",
"import gzip\n",
"\n",
"with gzip.open((PATH / FILENAME).as_posix(), \"rb\") as f:\n",
" ((x_train, y_train), (x_valid, y_valid), _) = pickle.load(f, encoding=\"latin-1\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" ...\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]]\n"
]
}
],
"source": [
"print(x_valid)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Each image is 28 x 28, and is being stored as a flattened row of length\n",
"784 (=28x28). Let's take a look at one; we need to reshape it to 2d\n",
"first.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"jupyter": {
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(50000, 784)\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAz0AAAM2CAYAAADcr+22AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy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QAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWtuw6AUAMI299tpr0nkbN26cdF5HZ5555qTzHvOYx0w26+CDD55s1hlnnDHZrD/+4z+ebNYrXvGKyWYlyQMPPDDZrLe97W2TzTrnnHMmm8XiONMDAAC0JnoAAIDWRA8AANCa6AEAAFoTPQAAQGuiBwAAaE30AAAArS0seqrqxqoaK/x8e1HrAgAAeln0l5PemeRdy+y/Z+qFAAAAPS06eu4YY5y94DUAAACN+UwPAADQ2qLP9Dy6ql6Z5MlJ7k1ybZIrxhgPLXZZAABAF4uOnv2TXLTdvhuq6pfGGJ99uAdX1aYVbjpkt1cGAAC0sMi3t12Q5IQshc8+SX4yyflJDkjyt1X1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BwAAaE30AAAArYkeAACgNdEDAAC0JnoAAIDWRA8AANCa6AEAAFoTPQAAQGuiBwAAaE30AAAArYkeAACgNdEDAAC0JnoAAIDWRA8AANCa6AEAAFoTPQAAQGs1xlj0GuauqjYlOXzR6wCAR5IHH3xwslkbNmyYbNbmzZsnm/WCF7xgslmXX375ZLNgLRlj1Gof40wPAADQmugBAABaEz0AAEBrogcAAGhN9AAAAK2JHgAAoDXRAwAAtCZ6AACA1kQPAADQmugBAABaEz0AAEBrogcAAGhN9AAAAK2JHgAAoDXRAwAAtCZ6AACA1kQPAADQmugBAABaEz0AAEBrogcAAGhN9AAAAK2JHgAAoDXRAwAAtCZ6AACA1kQPAADQmugBAABa27DoBQDA7nr6058+2axTTjllslnPetazJpuVJBs29PxrwXXXXTfZrCuuuGKyWcDOc6YHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtbVj0AgCYxsEHHzzpvDPPPHOyWS996Usnm7X//vtPNquzhx56aLJZt9xyy2SztmzZMtksYOc50wMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNY2LHoBAGvN/vvvP9msV7ziFZPNOvPMMyeblSQHHHDApPPYfVdfffVks9761rdONuuv/uqvJpsFrE3O9AAAAK2JHgAAoDXRAwAAtCZ6AACA1kQPAADQmugBAABaEz0AAEBrux09VfWEqnpVVf1lVV1fVfdX1Z1V9bmqOr2qlp1RVUdV1SVVdXtV3VdV11bVWVW11+6uCQAAYKt5fDnpqUnel+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AACwot1+e9sY49Ixxie2DZ7Z/m8nef/s12N3dw4AAMCumMeZnh3599l28zK3/VBV/UqSJyS5LckXxhjX7uH1AAAA68wei56q2pDkF2e/fnKZu5w4+9n2MZcnOW2McdNOzti0wk2H7OQyAQCA5vbkJavfluRpSS4ZY3xqm/33Jfm9JEckedzs55gsXQTh2CSfqap99uC6AACAdaS+/6Jqc3jSqtckeXeSryU5eoxx+048ZkOSzyU5MslZY4x378b8TUkO39XHAwAAa9MYo1b7mLmf6amqM7IUPNclOW5ngidJxhibs3SJ6yR53rzXBQAArE9zjZ6qOivJeVn6rp3jZldwW41/nW29vQ0AAJiLuUVPVf1GkncmuSZLwfPdXXia58y235rXugAAgPVtLtFTVW/J0oULNiU5YYxx6w7ue2RVPWqZ/ccned3s1w/NY10AAAC7fcnqqjotye8meSjJlUleU/V9ny26cYxx4ezPf5jk0NnlqW+e7Xt6kuNnf37LGOOq3V0XAABAMp/v6XnKbLtXkrNWuM9nk1w4+/NFSU5O8qwkJyX5gSTfSfIXSc4bY1w5hzUBAAAk2UOXrF40l6wGAICe1sQlqwEAANYS0QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0JroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALQmegAAgNZEDwAA0Co+k40AAAd+SURBVJroAQAAWhM9AABAa6IHAABoTfQAAACtiR4AAKA10QMAALTWNXoOWPQCAACAtWHDohewh9w12964isccMtt+bb5LoQmvD3bE64Md8fpgR7w+2BGvj//qgPzn3/NXp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fPqbxe/v7OAu/zTGOGC7xxyd5Ley9DbIvZNcn+TPkpzrS217Wc3ro6pOT3Jyli5H+z+S/ECS7yT5QpLzxhhXrvQkPPLsxGsjST47xjh2u8c5fqwDq319OH7svnUfPQAAQG/r9jM9AADA+iB6AACA1kQPAADQmugBAABaEz0AAEBrogcAAGhN9AAAAK2JHgAAoDXRAwAAtCZ6AACA1kQPAADQmugBAABaEz0AAEBrogcAAGhN9AAAAK2JHgAAoDXRAwAAtPb/AZlgB+Ge770sAAAAAElFTkSuQmCC",
"text/plain": [
"<Figure size 864x504 with 1 Axes>"
]
},
"execution_count": 4,
"metadata": {
"image/png": {
"height": 411,
"width": 414
},
"needs_background": "light"
},
"output_type": "execute_result"
}
],
"source": [
"from matplotlib import pyplot\n",
"import numpy as np\n",
"\n",
"pyplot.imshow(x_train[0].reshape((28, 28)), cmap=\"gray\")\n",
"print(x_train.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"PyTorch uses ``torch.tensor``, rather than numpy arrays, so we need to\n",
"convert our data.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"jupyter": {
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" ...,\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.]]) tensor([5, 0, 4, ..., 8, 4, 8])\n",
"torch.Size([50000, 784])\n",
"tensor(0) tensor(9)\n"
]
}
],
"source": [
"import torch\n",
"\n",
"x_train, y_train, x_valid, y_valid = map(\n",
" torch.tensor, (x_train, y_train, x_valid, y_valid)\n",
")\n",
"n, c = x_train.shape\n",
"print(x_train, y_train)\n",
"print(x_train.shape)\n",
"print(y_train.min(), y_train.max())"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Neural net from scratch (no torch.nn)\n",
"---------------------------------------------\n",
"\n",
"Let's first create a model using nothing but PyTorch tensor operations. We're assuming\n",
"you're already familiar with the basics of neural networks. (If you're not, you can\n",
"learn them at `course.fast.ai <https://course.fast.ai>`_).\n",
"\n",
"PyTorch provides methods to create random or zero-filled tensors, which we will\n",
"use to create our weights and bias for a simple linear model. These are just regular\n",
"tensors, with one very special addition: we tell PyTorch that they require a\n",
"gradient. This causes PyTorch to record all of the operations done on the tensor,\n",
"so that it can calculate the gradient during back-propagation *automatically*!\n",
"\n",
"For the weights, we set ``requires_grad`` **after** the initialization, since we\n",
"don't want that step included in the gradient. (Note that a trailing ``_`` in\n",
"PyTorch signifies that the operation is performed in-place.)\n",
"\n",
"<div class=\"alert alert-info\"><h4>Note</h4><p>We are initializing the weights here with\n",
" `Xavier initialisation <http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf>`_\n",
" (by multiplying with 1/sqrt(n)).</p></div>\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"import math\n",
"\n",
"weights = torch.randn(784, 10) / math.sqrt(784)\n",
"weights.requires_grad_()\n",
"bias = torch.zeros(10, requires_grad=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Thanks to PyTorch's ability to calculate gradients automatically, we can\n",
"use any standard Python function (or callable object) as a model! So\n",
"let's just write a plain matrix multiplication and broadcasted addition\n",
"to create a simple linear model. We also need an activation function, so\n",
"we'll write `log_softmax` and use it. Remember: although PyTorch\n",
"provides lots of pre-written loss functions, activation functions, and\n",
"so forth, you can easily write your own using plain python. PyTorch will\n",
"even create fast GPU or vectorized CPU code for your function\n",
"automatically.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"def log_softmax(x):\n",
" return x - x.exp().sum(-1).log().unsqueeze(-1)\n",
"\n",
"def model(xb):\n",
" return log_softmax(xb @ weights + bias)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"In the above, the ``@`` stands for the dot product operation. We will call\n",
"our function on one batch of data (in this case, 64 images). This is\n",
"one *forward pass*. Note that our predictions won't be any better than\n",
"random at this stage, since we start with random weights.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"bs = 64 # batch size\n",
"\n",
"xb = x_train[0:bs] # a mini-batch from x\n",
"preds = model(xb) # predictions\n",
"preds[0], preds.shape\n",
"print(preds[0], preds.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"As you see, the ``preds`` tensor contains not only the tensor values, but also a\n",
"gradient function. We'll use this later to do backprop.\n",
"\n",
"Let's implement negative log-likelihood to use as the loss function\n",
"(again, we can just use standard Python):\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"def nll(input, target):\n",
" return -input[range(target.shape[0]), target].mean()\n",
"\n",
"loss_func = nll"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Let's check our loss with our random model, so we can see if we improve\n",
"after a backprop pass later.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"yb = y_train[0:bs]\n",
"print(loss_func(preds, yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Let's also implement a function to calculate the accuracy of our model.\n",
"For each prediction, if the index with the largest value matches the\n",
"target value, then the prediction was correct.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"def accuracy(out, yb):\n",
" preds = torch.argmax(out, dim=1)\n",
" return (preds == yb).float().mean()"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Let's check the accuracy of our random model, so we can see if our\n",
"accuracy improves as our loss improves.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"print(accuracy(preds, yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"We can now run a training loop. For each iteration, we will:\n",
"\n",
"- select a mini-batch of data (of size ``bs``)\n",
"- use the model to make predictions\n",
"- calculate the loss\n",
"- ``loss.backward()`` updates the gradients of the model, in this case, ``weights``\n",
" and ``bias``.\n",
"\n",
"We now use these gradients to update the weights and bias. We do this\n",
"within the ``torch.no_grad()`` context manager, because we do not want these\n",
"actions to be recorded for our next calculation of the gradient. You can read\n",
"more about how PyTorch's Autograd records operations\n",
"`here <https://pytorch.org/docs/stable/notes/autograd.html>`_.\n",
"\n",
"We then set the\n",
"gradients to zero, so that we are ready for the next loop.\n",
"Otherwise, our gradients would record a running tally of all the operations\n",
"that had happened (i.e. ``loss.backward()`` *adds* the gradients to whatever is\n",
"already stored, rather than replacing them).\n",
"\n",
".. tip:: You can use the standard python debugger to step through PyTorch\n",
" code, allowing you to check the various variable values at each step.\n",
" Uncomment ``set_trace()`` below to try it out.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"from IPython.core.debugger import set_trace\n",
"\n",
"lr = 0.5 # learning rate\n",
"epochs = 2 # how many epochs to train for\n",
"\n",
"for epoch in range(epochs):\n",
" for i in range((n - 1) // bs + 1):\n",
" # set_trace()\n",
" start_i = i * bs\n",
" end_i = start_i + bs\n",
" xb = x_train[start_i:end_i]\n",
" yb = y_train[start_i:end_i]\n",
" pred = model(xb)\n",
" loss = loss_func(pred, yb)\n",
"\n",
" loss.backward()\n",
" with torch.no_grad():\n",
" weights -= weights.grad * lr\n",
" bias -= bias.grad * lr\n",
" weights.grad.zero_()\n",
" bias.grad.zero_()"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"That's it: we've created and trained a minimal neural network (in this case, a\n",
"logistic regression, since we have no hidden layers) entirely from scratch!\n",
"\n",
"Let's check the loss and accuracy and compare those to what we got\n",
"earlier. We expect that the loss will have decreased and accuracy to\n",
"have increased, and they have.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"print(loss_func(model(xb), yb), accuracy(model(xb), yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Using torch.nn.functional\n",
"------------------------------\n",
"\n",
"We will now refactor our code, so that it does the same thing as before, only\n",
"we'll start taking advantage of PyTorch's ``nn`` classes to make it more concise\n",
"and flexible. At each step from here, we should be making our code one or more\n",
"of: shorter, more understandable, and/or more flexible.\n",
"\n",
"The first and easiest step is to make our code shorter by replacing our\n",
"hand-written activation and loss functions with those from ``torch.nn.functional``\n",
"(which is generally imported into the namespace ``F`` by convention). This module\n",
"contains all the functions in the ``torch.nn`` library (whereas other parts of the\n",
"library contain classes). As well as a wide range of loss and activation\n",
"functions, you'll also find here some convenient functions for creating neural\n",
"nets, such as pooling functions. (There are also functions for doing convolutions,\n",
"linear layers, etc, but as we'll see, these are usually better handled using\n",
"other parts of the library.)\n",
"\n",
"If you're using negative log likelihood loss and log softmax activation,\n",
"then Pytorch provides a single function ``F.cross_entropy`` that combines\n",
"the two. So we can even remove the activation function from our model.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"import torch.nn.functional as F\n",
"\n",
"loss_func = F.cross_entropy\n",
"\n",
"def model(xb):\n",
" return xb @ weights + bias"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Note that we no longer call ``log_softmax`` in the ``model`` function. Let's\n",
"confirm that our loss and accuracy are the same as before:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"print(loss_func(model(xb), yb), accuracy(model(xb), yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Refactor using nn.Module\n",
"-----------------------------\n",
"Next up, we'll use ``nn.Module`` and ``nn.Parameter``, for a clearer and more\n",
"concise training loop. We subclass ``nn.Module`` (which itself is a class and\n",
"able to keep track of state). In this case, we want to create a class that\n",
"holds our weights, bias, and method for the forward step. ``nn.Module`` has a\n",
"number of attributes and methods (such as ``.parameters()`` and ``.zero_grad()``)\n",
"which we will be using.\n",
"\n",
"<div class=\"alert alert-info\"><h4>Note</h4><p>``nn.Module`` (uppercase M) is a PyTorch specific concept, and is a\n",
" class we'll be using a lot. ``nn.Module`` is not to be confused with the Python\n",
" concept of a (lowercase ``m``) `module <https://docs.python.org/3/tutorial/modules.html>`_,\n",
" which is a file of Python code that can be imported.</p></div>\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"from torch import nn\n",
"\n",
"class Mnist_Logistic(nn.Module):\n",
" def __init__(self):\n",
" super().__init__()\n",
" self.weights = nn.Parameter(torch.randn(784, 10) / math.sqrt(784))\n",
" self.bias = nn.Parameter(torch.zeros(10))\n",
"\n",
" def forward(self, xb):\n",
" return xb @ self.weights + self.bias"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Since we're now using an object instead of just using a function, we\n",
"first have to instantiate our model:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"model = Mnist_Logistic()"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Now we can calculate the loss in the same way as before. Note that\n",
"``nn.Module`` objects are used as if they are functions (i.e they are\n",
"*callable*), but behind the scenes Pytorch will call our ``forward``\n",
"method automatically.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"print(loss_func(model(xb), yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Previously for our training loop we had to update the values for each parameter\n",
"by name, and manually zero out the grads for each parameter separately, like this:\n",
"::\n",
" with torch.no_grad():\n",
" weights -= weights.grad * lr\n",
" bias -= bias.grad * lr\n",
" weights.grad.zero_()\n",
" bias.grad.zero_()\n",
"\n",
"\n",
"Now we can take advantage of model.parameters() and model.zero_grad() (which\n",
"are both defined by PyTorch for ``nn.Module``) to make those steps more concise\n",
"and less prone to the error of forgetting some of our parameters, particularly\n",
"if we had a more complicated model:\n",
"::\n",
" with torch.no_grad():\n",
" for p in model.parameters(): p -= p.grad * lr\n",
" model.zero_grad()\n",
"\n",
"\n",
"We'll wrap our little training loop in a ``fit`` function so we can run it\n",
"again later.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"def fit():\n",
" for epoch in range(epochs):\n",
" for i in range((n - 1) // bs + 1):\n",
" start_i = i * bs\n",
" end_i = start_i + bs\n",
" xb = x_train[start_i:end_i]\n",
" yb = y_train[start_i:end_i]\n",
" pred = model(xb)\n",
" loss = loss_func(pred, yb)\n",
"\n",
" loss.backward()\n",
" with torch.no_grad():\n",
" for p in model.parameters():\n",
" p -= p.grad * lr\n",
" model.zero_grad()\n",
"\n",
"fit()"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Let's double-check that our loss has gone down:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"print(loss_func(model(xb), yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Refactor using nn.Linear\n",
"-------------------------\n",
"\n",
"We continue to refactor our code. Instead of manually defining and\n",
"initializing ``self.weights`` and ``self.bias``, and calculating ``xb @\n",
"self.weights + self.bias``, we will instead use the Pytorch class\n",
"`nn.Linear <https://pytorch.org/docs/stable/nn.html#linear-layers>`_ for a\n",
"linear layer, which does all that for us. Pytorch has many types of\n",
"predefined layers that can greatly simplify our code, and often makes it\n",
"faster too.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"class Mnist_Logistic(nn.Module):\n",
" def __init__(self):\n",
" super().__init__()\n",
" self.lin = nn.Linear(784, 10)\n",
"\n",
" def forward(self, xb):\n",
" return self.lin(xb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"We instantiate our model and calculate the loss in the same way as before:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"model = Mnist_Logistic()\n",
"print(loss_func(model(xb), yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"We are still able to use our same ``fit`` method as before.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"fit()\n",
"\n",
"print(loss_func(model(xb), yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Refactor using optim\n",
"------------------------------\n",
"\n",
"Pytorch also has a package with various optimization algorithms, ``torch.optim``.\n",
"We can use the ``step`` method from our optimizer to take a forward step, instead\n",
"of manually updating each parameter.\n",
"\n",
"This will let us replace our previous manually coded optimization step:\n",
"::\n",
" with torch.no_grad():\n",
" for p in model.parameters(): p -= p.grad * lr\n",
" model.zero_grad()\n",
"\n",
"and instead use just:\n",
"::\n",
" opt.step()\n",
" opt.zero_grad()\n",
"\n",
"(``optim.zero_grad()`` resets the gradient to 0 and we need to call it before\n",
"computing the gradient for the next minibatch.)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"from torch import optim"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"We'll define a little function to create our model and optimizer so we\n",
"can reuse it in the future.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"def get_model():\n",
" model = Mnist_Logistic()\n",
" return model, optim.SGD(model.parameters(), lr=lr)\n",
"\n",
"model, opt = get_model()\n",
"print(loss_func(model(xb), yb))\n",
"\n",
"for epoch in range(epochs):\n",
" for i in range((n - 1) // bs + 1):\n",
" start_i = i * bs\n",
" end_i = start_i + bs\n",
" xb = x_train[start_i:end_i]\n",
" yb = y_train[start_i:end_i]\n",
" pred = model(xb)\n",
" loss = loss_func(pred, yb)\n",
"\n",
" loss.backward()\n",
" opt.step()\n",
" opt.zero_grad()\n",
"\n",
"print(loss_func(model(xb), yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Refactor using Dataset\n",
"------------------------------\n",
"\n",
"PyTorch has an abstract Dataset class. A Dataset can be anything that has\n",
"a ``__len__`` function (called by Python's standard ``len`` function) and\n",
"a ``__getitem__`` function as a way of indexing into it.\n",
"`This tutorial <https://pytorch.org/tutorials/beginner/data_loading_tutorial.html>`_\n",
"walks through a nice example of creating a custom ``FacialLandmarkDataset`` class\n",
"as a subclass of ``Dataset``.\n",
"\n",
"PyTorch's `TensorDataset <https://pytorch.org/docs/stable/_modules/torch/utils/data/dataset.html#TensorDataset>`_\n",
"is a Dataset wrapping tensors. By defining a length and way of indexing,\n",
"this also gives us a way to iterate, index, and slice along the first\n",
"dimension of a tensor. This will make it easier to access both the\n",
"independent and dependent variables in the same line as we train.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"from torch.utils.data import TensorDataset"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Both ``x_train`` and ``y_train`` can be combined in a single ``TensorDataset``,\n",
"which will be easier to iterate over and slice.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"train_ds = TensorDataset(x_train, y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Previously, we had to iterate through minibatches of x and y values separately:\n",
"::\n",
" xb = x_train[start_i:end_i]\n",
" yb = y_train[start_i:end_i]\n",
"\n",
"\n",
"Now, we can do these two steps together:\n",
"::\n",
" xb,yb = train_ds[i*bs : i*bs+bs]\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"model, opt = get_model()\n",
"\n",
"for epoch in range(epochs):\n",
" for i in range((n - 1) // bs + 1):\n",
" xb, yb = train_ds[i * bs: i * bs + bs]\n",
" pred = model(xb)\n",
" loss = loss_func(pred, yb)\n",
"\n",
" loss.backward()\n",
" opt.step()\n",
" opt.zero_grad()\n",
"\n",
"print(loss_func(model(xb), yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Refactor using DataLoader\n",
"------------------------------\n",
"\n",
"Pytorch's ``DataLoader`` is responsible for managing batches. You can\n",
"create a ``DataLoader`` from any ``Dataset``. ``DataLoader`` makes it easier\n",
"to iterate over batches. Rather than having to use ``train_ds[i*bs : i*bs+bs]``,\n",
"the DataLoader gives us each minibatch automatically.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"from torch.utils.data import DataLoader\n",
"\n",
"train_ds = TensorDataset(x_train, y_train)\n",
"train_dl = DataLoader(train_ds, batch_size=bs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Previously, our loop iterated over batches (xb, yb) like this:\n",
"::\n",
" for i in range((n-1)//bs + 1):\n",
" xb,yb = train_ds[i*bs : i*bs+bs]\n",
" pred = model(xb)\n",
"\n",
"Now, our loop is much cleaner, as (xb, yb) are loaded automatically from the data loader:\n",
"::\n",
" for xb,yb in train_dl:\n",
" pred = model(xb)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"model, opt = get_model()\n",
"\n",
"for epoch in range(epochs):\n",
" for xb, yb in train_dl:\n",
" pred = model(xb)\n",
" loss = loss_func(pred, yb)\n",
"\n",
" loss.backward()\n",
" opt.step()\n",
" opt.zero_grad()\n",
"\n",
"print(loss_func(model(xb), yb))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Thanks to Pytorch's ``nn.Module``, ``nn.Parameter``, ``Dataset``, and ``DataLoader``,\n",
"our training loop is now dramatically smaller and easier to understand. Let's\n",
"now try to add the basic features necessary to create effective models in practice.\n",
"\n",
"Add validation\n",
"-----------------------\n",
"\n",
"In section 1, we were just trying to get a reasonable training loop set up for\n",
"use on our training data. In reality, you **always** should also have\n",
"a `validation set <https://www.fast.ai/2017/11/13/validation-sets/>`_, in order\n",
"to identify if you are overfitting.\n",
"\n",
"Shuffling the training data is\n",
"`important <https://www.quora.com/Does-the-order-of-training-data-matter-when-training-neural-networks>`_\n",
"to prevent correlation between batches and overfitting. On the other hand, the\n",
"validation loss will be identical whether we shuffle the validation set or not.\n",
"Since shuffling takes extra time, it makes no sense to shuffle the validation data.\n",
"\n",
"We'll use a batch size for the validation set that is twice as large as\n",
"that for the training set. This is because the validation set does not\n",
"need backpropagation and thus takes less memory (it doesn't need to\n",
"store the gradients). We take advantage of this to use a larger batch\n",
"size and compute the loss more quickly.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"train_ds = TensorDataset(x_train, y_train)\n",
"train_dl = DataLoader(train_ds, batch_size=bs, shuffle=True)\n",
"\n",
"valid_ds = TensorDataset(x_valid, y_valid)\n",
"valid_dl = DataLoader(valid_ds, batch_size=bs * 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"We will calculate and print the validation loss at the end of each epoch.\n",
"\n",
"(Note that we always call ``model.train()`` before training, and ``model.eval()``\n",
"before inference, because these are used by layers such as ``nn.BatchNorm2d``\n",
"and ``nn.Dropout`` to ensure appropriate behaviour for these different phases.)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"model, opt = get_model()\n",
"\n",
"for epoch in range(epochs):\n",
" model.train()\n",
" for xb, yb in train_dl:\n",
" pred = model(xb)\n",
" loss = loss_func(pred, yb)\n",
"\n",
" loss.backward()\n",
" opt.step()\n",
" opt.zero_grad()\n",
"\n",
" model.eval()\n",
" with torch.no_grad():\n",
" valid_loss = sum(loss_func(model(xb), yb) for xb, yb in valid_dl)\n",
"\n",
" print(epoch, valid_loss / len(valid_dl))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Create fit() and get_data()\n",
"----------------------------------\n",
"\n",
"We'll now do a little refactoring of our own. Since we go through a similar\n",
"process twice of calculating the loss for both the training set and the\n",
"validation set, let's make that into its own function, ``loss_batch``, which\n",
"computes the loss for one batch.\n",
"\n",
"We pass an optimizer in for the training set, and use it to perform\n",
"backprop. For the validation set, we don't pass an optimizer, so the\n",
"method doesn't perform backprop.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"def loss_batch(model, loss_func, xb, yb, opt=None):\n",
" loss = loss_func(model(xb), yb)\n",
"\n",
" if opt is not None:\n",
" loss.backward()\n",
" opt.step()\n",
" opt.zero_grad()\n",
"\n",
" return loss.item(), len(xb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"``fit`` runs the necessary operations to train our model and compute the\n",
"training and validation losses for each epoch.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"import numpy as np\n",
"\n",
"def fit(epochs, model, loss_func, opt, train_dl, valid_dl):\n",
" for epoch in range(epochs):\n",
" model.train()\n",
" for xb, yb in train_dl:\n",
" loss_batch(model, loss_func, xb, yb, opt)\n",
"\n",
" model.eval()\n",
" with torch.no_grad():\n",
" losses, nums = zip(\n",
" *[loss_batch(model, loss_func, xb, yb) for xb, yb in valid_dl]\n",
" )\n",
" val_loss = np.sum(np.multiply(losses, nums)) / np.sum(nums)\n",
"\n",
" print(epoch, val_loss)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"``get_data`` returns dataloaders for the training and validation sets.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"def get_data(train_ds, valid_ds, bs):\n",
" return (\n",
" DataLoader(train_ds, batch_size=bs, shuffle=True),\n",
" DataLoader(valid_ds, batch_size=bs * 2),\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Now, our whole process of obtaining the data loaders and fitting the\n",
"model can be run in 3 lines of code:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"train_dl, valid_dl = get_data(train_ds, valid_ds, bs)\n",
"model, opt = get_model()\n",
"fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"You can use these basic 3 lines of code to train a wide variety of models.\n",
"Let's see if we can use them to train a convolutional neural network (CNN)!\n",
"\n",
"Switch to CNN\n",
"-------------\n",
"\n",
"We are now going to build our neural network with three convolutional layers.\n",
"Because none of the functions in the previous section assume anything about\n",
"the model form, we'll be able to use them to train a CNN without any modification.\n",
"\n",
"We will use Pytorch's predefined\n",
"`Conv2d <https://pytorch.org/docs/stable/nn.html#torch.nn.Conv2d>`_ class\n",
"as our convolutional layer. We define a CNN with 3 convolutional layers.\n",
"Each convolution is followed by a ReLU. At the end, we perform an\n",
"average pooling. (Note that ``view`` is PyTorch's version of numpy's\n",
"``reshape``)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"class Mnist_CNN(nn.Module):\n",
" def __init__(self):\n",
" super().__init__()\n",
" self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=2, padding=1)\n",
" self.conv2 = nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1)\n",
" self.conv3 = nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1)\n",
"\n",
" def forward(self, xb):\n",
" xb = xb.view(-1, 1, 28, 28)\n",
" xb = F.relu(self.conv1(xb))\n",
" xb = F.relu(self.conv2(xb))\n",
" xb = F.relu(self.conv3(xb))\n",
" xb = F.avg_pool2d(xb, 4)\n",
" return xb.view(-1, xb.size(1))\n",
"\n",
"lr = 0.1"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"`Momentum <https://cs231n.github.io/neural-networks-3/#sgd>`_ is a variation on\n",
"stochastic gradient descent that takes previous updates into account as well\n",
"and generally leads to faster training.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"model = Mnist_CNN()\n",
"opt = optim.SGD(model.parameters(), lr=lr, momentum=0.9)\n",
"\n",
"fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"nn.Sequential\n",
"------------------------\n",
"\n",
"``torch.nn`` has another handy class we can use to simplify our code:\n",
"`Sequential <https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential>`_ .\n",
"A ``Sequential`` object runs each of the modules contained within it, in a\n",
"sequential manner. This is a simpler way of writing our neural network.\n",
"\n",
"To take advantage of this, we need to be able to easily define a\n",
"**custom layer** from a given function. For instance, PyTorch doesn't\n",
"have a `view` layer, and we need to create one for our network. ``Lambda``\n",
"will create a layer that we can then use when defining a network with\n",
"``Sequential``.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"class Lambda(nn.Module):\n",
" def __init__(self, func):\n",
" super().__init__()\n",
" self.func = func\n",
"\n",
" def forward(self, x):\n",
" return self.func(x)\n",
"\n",
"\n",
"def preprocess(x):\n",
" return x.view(-1, 1, 28, 28)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"The model created with ``Sequential`` is simply:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"model = nn.Sequential(\n",
" Lambda(preprocess),\n",
" nn.Conv2d(1, 16, kernel_size=3, stride=2, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1),\n",
" nn.ReLU(),\n",
" nn.AvgPool2d(4),\n",
" Lambda(lambda x: x.view(x.size(0), -1)),\n",
")\n",
"\n",
"opt = optim.SGD(model.parameters(), lr=lr, momentum=0.9)\n",
"\n",
"fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Wrapping DataLoader\n",
"-----------------------------\n",
"\n",
"Our CNN is fairly concise, but it only works with MNIST, because:\n",
" - It assumes the input is a 28\\*28 long vector\n",
" - It assumes that the final CNN grid size is 4\\*4 (since that's the average\n",
"pooling kernel size we used)\n",
"\n",
"Let's get rid of these two assumptions, so our model works with any 2d\n",
"single channel image. First, we can remove the initial Lambda layer by\n",
"moving the data preprocessing into a generator:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"def preprocess(x, y):\n",
" return x.view(-1, 1, 28, 28), y\n",
"\n",
"\n",
"class WrappedDataLoader:\n",
" def __init__(self, dl, func):\n",
" self.dl = dl\n",
" self.func = func\n",
"\n",
" def __len__(self):\n",
" return len(self.dl)\n",
"\n",
" def __iter__(self):\n",
" batches = iter(self.dl)\n",
" for b in batches:\n",
" yield (self.func(*b))\n",
"\n",
"train_dl, valid_dl = get_data(train_ds, valid_ds, bs)\n",
"train_dl = WrappedDataLoader(train_dl, preprocess)\n",
"valid_dl = WrappedDataLoader(valid_dl, preprocess)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Next, we can replace ``nn.AvgPool2d`` with ``nn.AdaptiveAvgPool2d``, which\n",
"allows us to define the size of the *output* tensor we want, rather than\n",
"the *input* tensor we have. As a result, our model will work with any\n",
"size input.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"model = nn.Sequential(\n",
" nn.Conv2d(1, 16, kernel_size=3, stride=2, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1),\n",
" nn.ReLU(),\n",
" nn.AdaptiveAvgPool2d(1),\n",
" Lambda(lambda x: x.view(x.size(0), -1)),\n",
")\n",
"\n",
"opt = optim.SGD(model.parameters(), lr=lr, momentum=0.9)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Let's try it out:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Using your GPU\n",
"---------------\n",
"\n",
"If you're lucky enough to have access to a CUDA-capable GPU (you can\n",
"rent one for about $0.50/hour from most cloud providers) you can\n",
"use it to speed up your code. First check that your GPU is working in\n",
"Pytorch:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"print(torch.cuda.is_available())"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"And then create a device object for it:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"dev = torch.device(\n",
" \"cuda\") if torch.cuda.is_available() else torch.device(\"cpu\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Let's update ``preprocess`` to move batches to the GPU:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"def preprocess(x, y):\n",
" return x.view(-1, 1, 28, 28).to(dev), y.to(dev)\n",
"\n",
"\n",
"train_dl, valid_dl = get_data(train_ds, valid_ds, bs)\n",
"train_dl = WrappedDataLoader(train_dl, preprocess)\n",
"valid_dl = WrappedDataLoader(valid_dl, preprocess)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Finally, we can move our model to the GPU.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"model.to(dev)\n",
"opt = optim.SGD(model.parameters(), lr=lr, momentum=0.9)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"You should find it runs faster now:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
],
"source": [
"fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Closing thoughts\n",
"-----------------\n",
"\n",
"We now have a general data pipeline and training loop which you can use for\n",
"training many types of models using Pytorch. To see how simple training a model\n",
"can now be, take a look at the `mnist_sample` sample notebook.\n",
"\n",
"Of course, there are many things you'll want to add, such as data augmentation,\n",
"hyperparameter tuning, monitoring training, transfer learning, and so forth.\n",
"These features are available in the fastai library, which has been developed\n",
"using the same design approach shown in this tutorial, providing a natural\n",
"next step for practitioners looking to take their models further.\n",
"\n",
"We promised at the start of this tutorial we'd explain through example each of\n",
"``torch.nn``, ``torch.optim``, ``Dataset``, and ``DataLoader``. So let's summarize\n",
"what we've seen:\n",
"\n",
" - **torch.nn**\n",
"\n",
" + ``Module``: creates a callable which behaves like a function, but can also\n",
" contain state(such as neural net layer weights). It knows what ``Parameter`` (s) it\n",
" contains and can zero all their gradients, loop through them for weight updates, etc.\n",
" + ``Parameter``: a wrapper for a tensor that tells a ``Module`` that it has weights\n",
" that need updating during backprop. Only tensors with the `requires_grad` attribute set are updated\n",
" + ``functional``: a module(usually imported into the ``F`` namespace by convention)\n",
" which contains activation functions, loss functions, etc, as well as non-stateful\n",
" versions of layers such as convolutional and linear layers.\n",
" - ``torch.optim``: Contains optimizers such as ``SGD``, which update the weights\n",
" of ``Parameter`` during the backward step\n",
" - ``Dataset``: An abstract interface of objects with a ``__len__`` and a ``__getitem__``,\n",
" including classes provided with Pytorch such as ``TensorDataset``\n",
" - ``DataLoader``: Takes any ``Dataset`` and creates an iterator which returns batches of data.\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (Ubuntu Linux)",
"language": "python",
"name": "python3-ubuntu",
"resource_dir": "/usr/local/share/jupyter/kernels/python3-ubuntu"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|