-
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
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"`Learn the Basics <intro.html>`_ ||\n",
"**Quickstart** ||\n",
"`Tensors <tensorqs_tutorial.html>`_ ||\n",
"`Datasets & DataLoaders <data_tutorial.html>`_ ||\n",
"`Transforms <transforms_tutorial.html>`_ ||\n",
"`Build Model <buildmodel_tutorial.html>`_ ||\n",
"`Autograd <autogradqs_tutorial.html>`_ ||\n",
"`Optimization <optimization_tutorial.html>`_ ||\n",
"`Save & Load Model <saveloadrun_tutorial.html>`_\n",
"\n",
"Quickstart\n",
"===================\n",
"This section runs through the API for common tasks in machine learning. Refer to the links in each section to dive deeper.\n",
"\n",
"Working with data\n",
"-----------------\n",
"PyTorch has two `primitives to work with data <https://pytorch.org/docs/stable/data.html>`_:\n",
"``torch.utils.data.DataLoader`` and ``torch.utils.data.Dataset``.\n",
"``Dataset`` stores the samples and their corresponding labels, and ``DataLoader`` wraps an iterable around\n",
"the ``Dataset``.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"import torch\n",
"from torch import nn\n",
"from torch.utils.data import DataLoader\n",
"from torchvision import datasets\n",
"from torchvision.transforms import ToTensor, Lambda, Compose\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"PyTorch offers domain-specific libraries such as `TorchText <https://pytorch.org/text/stable/index.html>`_,\n",
"`TorchVision <https://pytorch.org/vision/stable/index.html>`_, and `TorchAudio <https://pytorch.org/audio/stable/index.html>`_,\n",
"all of which include datasets. For this tutorial, we will be using a TorchVision dataset.\n",
"\n",
"The ``torchvision.datasets`` module contains ``Dataset`` objects for many real-world vision data like\n",
"CIFAR, COCO (`full list here <https://pytorch.org/vision/stable/datasets.html>`_). In this tutorial, we\n",
"use the FashionMNIST dataset. Every TorchVision ``Dataset`` includes two arguments: ``transform`` and\n",
"``target_transform`` to modify the samples and labels respectively.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Download training data from open datasets.\n",
"training_data = datasets.FashionMNIST(\n",
" root=\"data\",\n",
" train=True,\n",
" download=True,\n",
" transform=ToTensor(),\n",
")\n",
"\n",
"# Download test data from open datasets.\n",
"test_data = datasets.FashionMNIST(\n",
" root=\"data\",\n",
" train=False,\n",
" download=True,\n",
" transform=ToTensor(),\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We pass the ``Dataset`` as an argument to ``DataLoader``. This wraps an iterable over our dataset, and supports\n",
"automatic batching, sampling, shuffling and multiprocess data loading. Here we define a batch size of 64, i.e. each element\n",
"in the dataloader iterable will return a batch of 64 features and labels.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torchvision.datasets.mnist.FashionMNIST"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(training_data)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shape of X [N, C, H, W]: torch.Size([200, 1, 28, 28])\n",
"Shape of y: torch.Size([200]) torch.int64\n"
]
}
],
"source": [
"batch_size = 200\n",
"\n",
"# Create data loaders.\n",
"train_dataloader = DataLoader(training_data, batch_size=batch_size)\n",
"test_dataloader = DataLoader(test_data, batch_size=batch_size)\n",
"\n",
"for X, y in test_dataloader:\n",
" print(\"Shape of X [N, C, H, W]: \", X.shape)\n",
" print(\"Shape of y: \", y.shape, y.dtype)\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read more about `loading data in PyTorch <data_tutorial.html>`_.\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------------\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Creating Models\n",
"------------------\n",
"To define a neural network in PyTorch, we create a class that inherits\n",
"from `nn.Module <https://pytorch.org/docs/stable/generated/torch.nn.Module.html>`_. We define the layers of the network\n",
"in the ``__init__`` function and specify how data will pass through the network in the ``forward`` function. To accelerate\n",
"operations in the neural network, we move it to the GPU if available.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using cpu device\n",
"NeuralNetwork(\n",
" (flatten): Flatten(start_dim=1, end_dim=-1)\n",
" (linear_relu_stack): Sequential(\n",
" (0): Linear(in_features=784, out_features=1024, bias=True)\n",
" (1): ReLU()\n",
" (2): Linear(in_features=1024, out_features=1024, bias=True)\n",
" (3): ReLU()\n",
" (4): Linear(in_features=1024, out_features=10, bias=True)\n",
" (5): ReLU()\n",
" )\n",
")\n"
]
}
],
"source": [
"# Get cpu or gpu device for training.\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"print(\"Using {} device\".format(device))\n",
"\n",
"# Define model\n",
"class NeuralNetwork(nn.Module):\n",
" def __init__(self):\n",
" super(NeuralNetwork, self).__init__()\n",
" self.flatten = nn.Flatten()\n",
" self.linear_relu_stack = nn.Sequential(\n",
" nn.Linear(28*28, 1024),\n",
" nn.ReLU(),\n",
" nn.Linear(1024, 1024),\n",
" nn.ReLU(),\n",
" nn.Linear(1024, 10),\n",
" nn.ReLU()\n",
" )\n",
"\n",
" def forward(self, x):\n",
" x = self.flatten(x)\n",
" logits = self.linear_relu_stack(x)\n",
" return logits\n",
"\n",
"model = NeuralNetwork().to(device)\n",
"print(model)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read more about `building neural networks in PyTorch <buildmodel_tutorial.html>`_.\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------------\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Optimizing the Model Parameters\n",
"----------------------------------------\n",
"To train a model, we need a `loss function <https://pytorch.org/docs/stable/nn.html#loss-functions>`_\n",
"and an `optimizer <https://pytorch.org/docs/stable/optim.html>`_.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"loss_fn = nn.CrossEntropyLoss()\n",
"optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In a single training loop, the model makes predictions on the training dataset (fed to it in batches), and\n",
"backpropagates the prediction error to adjust the model's parameters.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"def train(dataloader, model, loss_fn, optimizer):\n",
" size = len(dataloader.dataset)\n",
" for batch, (X, y) in enumerate(dataloader):\n",
" X, y = X.to(device), y.to(device)\n",
"\n",
" # Compute prediction error\n",
" pred = model(X)\n",
" loss = loss_fn(pred, y)\n",
"\n",
" # Backpropagation\n",
" optimizer.zero_grad()\n",
" loss.backward()\n",
" optimizer.step()\n",
"\n",
" if batch % 100 == 0:\n",
" loss, current = loss.item(), batch * len(X)\n",
" print(f\"loss: {loss:>7f} [{current:>5d}/{size:>5d}]\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We also check the model's performance against the test dataset to ensure it is learning.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"def test(dataloader, model, loss_fn):\n",
" size = len(dataloader.dataset)\n",
" num_batches = len(dataloader)\n",
" model.eval()\n",
" test_loss, correct = 0, 0\n",
" with torch.no_grad():\n",
" for X, y in dataloader:\n",
" X, y = X.to(device), y.to(device)\n",
" pred = model(X)\n",
" test_loss += loss_fn(pred, y).item()\n",
" correct += (pred.argmax(1) == y).type(torch.float).sum().item()\n",
" test_loss /= num_batches\n",
" correct /= size\n",
" print(f\"Test Error: \\n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The training process is conducted over several iterations (*epochs*). During each epoch, the model learns\n",
"parameters to make better predictions. We print the model's accuracy and loss at each epoch; we'd like to see the\n",
"accuracy increase and the loss decrease with every epoch.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1\n",
"-------------------------------\n",
"loss: 2.301592 [ 0/60000]\n",
"loss: 2.289894 [20000/60000]\n",
"loss: 2.280160 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 30.9%, Avg loss: 2.269258 \n",
"\n",
"Epoch 2\n",
"-------------------------------\n",
"loss: 2.268190 [ 0/60000]\n",
"loss: 2.259592 [20000/60000]\n",
"loss: 2.251709 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 34.5%, Avg loss: 2.238048 \n",
"\n",
"Epoch 3\n",
"-------------------------------\n",
"loss: 2.236049 [ 0/60000]\n",
"loss: 2.230108 [20000/60000]\n",
"loss: 2.223023 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 34.4%, Avg loss: 2.204923 \n",
"\n",
"Epoch 4\n",
"-------------------------------\n",
"loss: 2.201698 [ 0/60000]\n",
"loss: 2.199297 [20000/60000]\n",
"loss: 2.192280 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 34.7%, Avg loss: 2.168218 \n",
"\n",
"Epoch 5\n",
"-------------------------------\n",
"loss: 2.163056 [ 0/60000]\n",
"loss: 2.165860 [20000/60000]\n",
"loss: 2.158360 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 35.1%, Avg loss: 2.126790 \n",
"\n",
"Epoch 6\n",
"-------------------------------\n",
"loss: 2.118984 [ 0/60000]\n",
"loss: 2.128839 [20000/60000]\n",
"loss: 2.120741 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 35.5%, Avg loss: 2.080902 \n",
"\n",
"Epoch 7\n",
"-------------------------------\n",
"loss: 2.069693 [ 0/60000]\n",
"loss: 2.088246 [20000/60000]\n",
"loss: 2.080452 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 35.9%, Avg loss: 2.032795 \n",
"\n",
"Epoch 8\n",
"-------------------------------\n",
"loss: 2.017604 [ 0/60000]\n",
"loss: 2.045965 [20000/60000]\n",
"loss: 2.040246 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 36.2%, Avg loss: 1.986216 \n",
"\n",
"Epoch 9\n",
"-------------------------------\n",
"loss: 1.966350 [ 0/60000]\n",
"loss: 2.004781 [20000/60000]\n",
"loss: 2.002753 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 36.6%, Avg loss: 1.944349 \n",
"\n",
"Epoch 10\n",
"-------------------------------\n",
"loss: 1.919495 [ 0/60000]\n",
"loss: 1.967277 [20000/60000]\n",
"loss: 1.969415 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 37.1%, Avg loss: 1.908297 \n",
"\n",
"Epoch 11\n",
"-------------------------------\n",
"loss: 1.878579 [ 0/60000]\n",
"loss: 1.934277 [20000/60000]\n",
"loss: 1.940233 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 38.0%, Avg loss: 1.877542 \n",
"\n",
"Epoch 12\n",
"-------------------------------\n",
"loss: 1.843474 [ 0/60000]\n",
"loss: 1.905277 [20000/60000]\n",
"loss: 1.914412 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 38.9%, Avg loss: 1.850760 \n",
"\n",
"Epoch 13\n",
"-------------------------------\n",
"loss: 1.812682 [ 0/60000]\n",
"loss: 1.879232 [20000/60000]\n",
"loss: 1.890868 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 39.6%, Avg loss: 1.826784 \n",
"\n",
"Epoch 14\n",
"-------------------------------\n",
"loss: 1.785155 [ 0/60000]\n",
"loss: 1.855434 [20000/60000]\n",
"loss: 1.868896 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 40.1%, Avg loss: 1.804814 \n",
"\n",
"Epoch 15\n",
"-------------------------------\n",
"loss: 1.760130 [ 0/60000]\n",
"loss: 1.833383 [20000/60000]\n",
"loss: 1.848235 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 40.5%, Avg loss: 1.784439 \n",
"\n",
"Epoch 16\n",
"-------------------------------\n",
"loss: 1.737113 [ 0/60000]\n",
"loss: 1.812643 [20000/60000]\n",
"loss: 1.828667 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 40.9%, Avg loss: 1.765419 \n",
"\n",
"Epoch 17\n",
"-------------------------------\n",
"loss: 1.715825 [ 0/60000]\n",
"loss: 1.792752 [20000/60000]\n",
"loss: 1.809863 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 41.2%, Avg loss: 1.747591 \n",
"\n",
"Epoch 18\n",
"-------------------------------\n",
"loss: 1.695961 [ 0/60000]\n",
"loss: 1.773857 [20000/60000]\n",
"loss: 1.791943 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 41.5%, Avg loss: 1.723384 \n",
"\n",
"Epoch 19\n",
"-------------------------------\n",
"loss: 1.669920 [ 0/60000]\n",
"loss: 1.726300 [20000/60000]\n",
"loss: 1.758728 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 42.2%, Avg loss: 1.677625 \n",
"\n",
"Epoch 20\n",
"-------------------------------\n",
"loss: 1.626003 [ 0/60000]\n",
"loss: 1.674431 [20000/60000]\n",
"loss: 1.733464 [40000/60000]\n",
"Test Error: \n",
" Accuracy: 43.5%, Avg loss: 1.645380 \n",
"\n",
"Done!\n"
]
}
],
"source": [
"epochs = 20\n",
"for t in range(epochs):\n",
" print(f\"Epoch {t+1}\\n-------------------------------\")\n",
" train(train_dataloader, model, loss_fn, optimizer)\n",
" test(test_dataloader, model, loss_fn)\n",
"print(\"Done!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read more about `Training your model <optimization_tutorial.html>`_.\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------------\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Saving Models\n",
"-------------\n",
"A common way to save a model is to serialize the internal state dictionary (containing the model parameters).\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved PyTorch Model State to model.pth\n"
]
}
],
"source": [
"torch.save(model.state_dict(), \"model.pth\")\n",
"print(\"Saved PyTorch Model State to model.pth\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Loading Models\n",
"----------------------------\n",
"\n",
"The process for loading a model includes re-creating the model structure and loading\n",
"the state dictionary into it.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"<All keys matched successfully>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = NeuralNetwork()\n",
"model.load_state_dict(torch.load(\"model.pth\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This model can now be used to make predictions.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicted: \"Sandal\", Actual: \"Ankle boot\"\n",
"Predicted: \"Shirt\", Actual: \"Pullover\"\n",
"Predicted: \"Trouser\", Actual: \"Trouser\"\n",
"Predicted: \"Trouser\", Actual: \"Trouser\"\n",
"Predicted: \"Shirt\", Actual: \"Shirt\"\n",
"Predicted: \"Trouser\", Actual: \"Trouser\"\n",
"Predicted: \"Shirt\", Actual: \"Coat\"\n",
"Predicted: \"Shirt\", Actual: \"Shirt\"\n",
"Predicted: \"Sneaker\", Actual: \"Sandal\"\n",
"Predicted: \"Sneaker\", Actual: \"Sneaker\"\n",
"Predicted: \"T-shirt/top\", Actual: \"Coat\"\n",
"Predicted: \"Sandal\", Actual: \"Sandal\"\n",
"Predicted: \"Sandal\", Actual: \"Sneaker\"\n",
"Predicted: \"Dress\", Actual: \"Dress\"\n",
"Predicted: \"Shirt\", Actual: \"Coat\"\n",
"Predicted: \"Trouser\", Actual: \"Trouser\"\n",
"Predicted: \"Shirt\", Actual: \"Pullover\"\n",
"Predicted: \"Shirt\", Actual: \"Coat\"\n",
"Predicted: \"Bag\", Actual: \"Bag\"\n",
"Predicted: \"T-shirt/top\", Actual: \"T-shirt/top\"\n",
"Predicted: \"Shirt\", Actual: \"Pullover\"\n",
"Predicted: \"Sneaker\", Actual: \"Sandal\"\n",
"Predicted: \"Sneaker\", Actual: \"Sneaker\"\n",
"Predicted: \"Sneaker\", Actual: \"Ankle boot\"\n",
"Predicted: \"Trouser\", Actual: \"Trouser\"\n",
"Predicted: \"Shirt\", Actual: \"Coat\"\n",
"Predicted: \"Shirt\", Actual: \"Shirt\"\n",
"Predicted: \"Dress\", Actual: \"T-shirt/top\"\n",
"Predicted: \"Sandal\", Actual: \"Ankle boot\"\n",
"Predicted: \"Dress\", Actual: \"Dress\"\n",
"Predicted: \"Bag\", Actual: \"Bag\"\n",
"Predicted: \"Bag\", Actual: \"Bag\"\n",
"Predicted: \"Dress\", Actual: \"Dress\"\n",
"Predicted: \"Dress\", Actual: \"Dress\"\n",
"Predicted: \"Bag\", Actual: \"Bag\"\n",
"Predicted: \"T-shirt/top\", Actual: \"T-shirt/top\"\n",
"Predicted: \"Sneaker\", Actual: \"Sneaker\"\n",
"Predicted: \"Sandal\", Actual: \"Sandal\"\n",
"Predicted: \"Sneaker\", Actual: \"Sneaker\"\n",
"Predicted: \"T-shirt/top\", Actual: \"Ankle boot\"\n",
"Predicted: \"T-shirt/top\", Actual: \"Shirt\"\n",
"Predicted: \"Trouser\", Actual: \"Trouser\"\n",
"Predicted: \"Dress\", Actual: \"Dress\"\n",
"Predicted: \"Sneaker\", Actual: \"Sneaker\"\n",
"Predicted: \"Shirt\", Actual: \"Shirt\"\n",
"Predicted: \"Sandal\", Actual: \"Sneaker\"\n",
"Predicted: \"Shirt\", Actual: \"Pullover\"\n",
"Predicted: \"Trouser\", Actual: \"Trouser\"\n",
"Predicted: \"Trouser\", Actual: \"Pullover\"\n",
"Predicted: \"Shirt\", Actual: \"Pullover\"\n"
]
}
],
"source": [
"classes = [\n",
" \"T-shirt/top\",\n",
" \"Trouser\",\n",
" \"Pullover\",\n",
" \"Dress\",\n",
" \"Coat\",\n",
" \"Sandal\",\n",
" \"Shirt\",\n",
" \"Sneaker\",\n",
" \"Bag\",\n",
" \"Ankle boot\",\n",
"]\n",
"\n",
"model.eval()\n",
"for i in range(0, 50):\n",
" x, y = test_data[i][0], test_data[i][1]\n",
" with torch.no_grad():\n",
" pred = model(x)\n",
" predicted, actual = classes[pred[0].argmax(0)], classes[y]\n",
" print(f'Predicted: \"{predicted}\", Actual: \"{actual}\"')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "PyTorch",
"language": "python",
"name": "pytorch"
},
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}