CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
pytorch

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: pytorch/tutorials
Path: blob/main/beginner_source/basics/quickstart_tutorial.py
Views: 494
1
"""
2
`Learn the Basics <intro.html>`_ ||
3
**Quickstart** ||
4
`Tensors <tensorqs_tutorial.html>`_ ||
5
`Datasets & DataLoaders <data_tutorial.html>`_ ||
6
`Transforms <transforms_tutorial.html>`_ ||
7
`Build Model <buildmodel_tutorial.html>`_ ||
8
`Autograd <autogradqs_tutorial.html>`_ ||
9
`Optimization <optimization_tutorial.html>`_ ||
10
`Save & Load Model <saveloadrun_tutorial.html>`_
11
12
Quickstart
13
===================
14
This section runs through the API for common tasks in machine learning. Refer to the links in each section to dive deeper.
15
16
Working with data
17
-----------------
18
PyTorch has two `primitives to work with data <https://pytorch.org/docs/stable/data.html>`_:
19
``torch.utils.data.DataLoader`` and ``torch.utils.data.Dataset``.
20
``Dataset`` stores the samples and their corresponding labels, and ``DataLoader`` wraps an iterable around
21
the ``Dataset``.
22
23
"""
24
25
import torch
26
from torch import nn
27
from torch.utils.data import DataLoader
28
from torchvision import datasets
29
from torchvision.transforms import ToTensor
30
31
######################################################################
32
# PyTorch offers domain-specific libraries such as `TorchText <https://pytorch.org/text/stable/index.html>`_,
33
# `TorchVision <https://pytorch.org/vision/stable/index.html>`_, and `TorchAudio <https://pytorch.org/audio/stable/index.html>`_,
34
# all of which include datasets. For this tutorial, we will be using a TorchVision dataset.
35
#
36
# The ``torchvision.datasets`` module contains ``Dataset`` objects for many real-world vision data like
37
# CIFAR, COCO (`full list here <https://pytorch.org/vision/stable/datasets.html>`_). In this tutorial, we
38
# use the FashionMNIST dataset. Every TorchVision ``Dataset`` includes two arguments: ``transform`` and
39
# ``target_transform`` to modify the samples and labels respectively.
40
41
# Download training data from open datasets.
42
training_data = datasets.FashionMNIST(
43
root="data",
44
train=True,
45
download=True,
46
transform=ToTensor(),
47
)
48
49
# Download test data from open datasets.
50
test_data = datasets.FashionMNIST(
51
root="data",
52
train=False,
53
download=True,
54
transform=ToTensor(),
55
)
56
57
######################################################################
58
# We pass the ``Dataset`` as an argument to ``DataLoader``. This wraps an iterable over our dataset, and supports
59
# automatic batching, sampling, shuffling and multiprocess data loading. Here we define a batch size of 64, i.e. each element
60
# in the dataloader iterable will return a batch of 64 features and labels.
61
62
batch_size = 64
63
64
# Create data loaders.
65
train_dataloader = DataLoader(training_data, batch_size=batch_size)
66
test_dataloader = DataLoader(test_data, batch_size=batch_size)
67
68
for X, y in test_dataloader:
69
print(f"Shape of X [N, C, H, W]: {X.shape}")
70
print(f"Shape of y: {y.shape} {y.dtype}")
71
break
72
73
######################################################################
74
# Read more about `loading data in PyTorch <data_tutorial.html>`_.
75
#
76
77
######################################################################
78
# --------------
79
#
80
81
################################
82
# Creating Models
83
# ------------------
84
# To define a neural network in PyTorch, we create a class that inherits
85
# from `nn.Module <https://pytorch.org/docs/stable/generated/torch.nn.Module.html>`_. We define the layers of the network
86
# in the ``__init__`` function and specify how data will pass through the network in the ``forward`` function. To accelerate
87
# operations in the neural network, we move it to the GPU or MPS if available.
88
89
# Get cpu, gpu or mps device for training.
90
device = (
91
"cuda"
92
if torch.cuda.is_available()
93
else "mps"
94
if torch.backends.mps.is_available()
95
else "cpu"
96
)
97
print(f"Using {device} device")
98
99
# Define model
100
class NeuralNetwork(nn.Module):
101
def __init__(self):
102
super().__init__()
103
self.flatten = nn.Flatten()
104
self.linear_relu_stack = nn.Sequential(
105
nn.Linear(28*28, 512),
106
nn.ReLU(),
107
nn.Linear(512, 512),
108
nn.ReLU(),
109
nn.Linear(512, 10)
110
)
111
112
def forward(self, x):
113
x = self.flatten(x)
114
logits = self.linear_relu_stack(x)
115
return logits
116
117
model = NeuralNetwork().to(device)
118
print(model)
119
120
######################################################################
121
# Read more about `building neural networks in PyTorch <buildmodel_tutorial.html>`_.
122
#
123
124
125
######################################################################
126
# --------------
127
#
128
129
130
#####################################################################
131
# Optimizing the Model Parameters
132
# ----------------------------------------
133
# To train a model, we need a `loss function <https://pytorch.org/docs/stable/nn.html#loss-functions>`_
134
# and an `optimizer <https://pytorch.org/docs/stable/optim.html>`_.
135
136
loss_fn = nn.CrossEntropyLoss()
137
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)
138
139
140
#######################################################################
141
# In a single training loop, the model makes predictions on the training dataset (fed to it in batches), and
142
# backpropagates the prediction error to adjust the model's parameters.
143
144
def train(dataloader, model, loss_fn, optimizer):
145
size = len(dataloader.dataset)
146
model.train()
147
for batch, (X, y) in enumerate(dataloader):
148
X, y = X.to(device), y.to(device)
149
150
# Compute prediction error
151
pred = model(X)
152
loss = loss_fn(pred, y)
153
154
# Backpropagation
155
loss.backward()
156
optimizer.step()
157
optimizer.zero_grad()
158
159
if batch % 100 == 0:
160
loss, current = loss.item(), (batch + 1) * len(X)
161
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
162
163
##############################################################################
164
# We also check the model's performance against the test dataset to ensure it is learning.
165
166
def test(dataloader, model, loss_fn):
167
size = len(dataloader.dataset)
168
num_batches = len(dataloader)
169
model.eval()
170
test_loss, correct = 0, 0
171
with torch.no_grad():
172
for X, y in dataloader:
173
X, y = X.to(device), y.to(device)
174
pred = model(X)
175
test_loss += loss_fn(pred, y).item()
176
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
177
test_loss /= num_batches
178
correct /= size
179
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
180
181
##############################################################################
182
# The training process is conducted over several iterations (*epochs*). During each epoch, the model learns
183
# parameters to make better predictions. We print the model's accuracy and loss at each epoch; we'd like to see the
184
# accuracy increase and the loss decrease with every epoch.
185
186
epochs = 5
187
for t in range(epochs):
188
print(f"Epoch {t+1}\n-------------------------------")
189
train(train_dataloader, model, loss_fn, optimizer)
190
test(test_dataloader, model, loss_fn)
191
print("Done!")
192
193
######################################################################
194
# Read more about `Training your model <optimization_tutorial.html>`_.
195
#
196
197
######################################################################
198
# --------------
199
#
200
201
######################################################################
202
# Saving Models
203
# -------------
204
# A common way to save a model is to serialize the internal state dictionary (containing the model parameters).
205
206
torch.save(model.state_dict(), "model.pth")
207
print("Saved PyTorch Model State to model.pth")
208
209
210
211
######################################################################
212
# Loading Models
213
# ----------------------------
214
#
215
# The process for loading a model includes re-creating the model structure and loading
216
# the state dictionary into it.
217
218
model = NeuralNetwork().to(device)
219
model.load_state_dict(torch.load("model.pth", weights_only=True))
220
221
#############################################################
222
# This model can now be used to make predictions.
223
224
classes = [
225
"T-shirt/top",
226
"Trouser",
227
"Pullover",
228
"Dress",
229
"Coat",
230
"Sandal",
231
"Shirt",
232
"Sneaker",
233
"Bag",
234
"Ankle boot",
235
]
236
237
model.eval()
238
x, y = test_data[0][0], test_data[0][1]
239
with torch.no_grad():
240
x = x.to(device)
241
pred = model(x)
242
predicted, actual = classes[pred[0].argmax(0)], classes[y]
243
print(f'Predicted: "{predicted}", Actual: "{actual}"')
244
245
246
######################################################################
247
# Read more about `Saving & Loading your model <saveloadrun_tutorial.html>`_.
248
#
249
250