Pytorch Tutorial 1 Rev 1
Pytorch Tutorial 1 Rev 1
Pytorch Tutorial
TA : 蕭淇元
2023.02.20
mlta-2023-spring@googlegroups.com
Outline
● Background: Prerequisites & What is Pytorch?
● Training & Testing Neural Networks in Pytorch
● Dataset & Dataloader
● Tensors
● torch.nn: Models, Loss Functions
● torch.optim: Optimization
● Save/load models
Prerequisites
● We assume you are already familiar with…
1. Python3
■ if-else, loop, function, file IO, class, ...
■ refs: link1, link2, link3
2. Deep Learning Basics
■ Prof. Lee’s 1st & 2nd lecture videos from last year
■ ref: link1, link2
Training
More info about the training process in last year's lecture video.
Training & Testing Neural Networks
● dataset = MyDataset(file)
● dataloader = DataLoader(dataset, batch_size, shuffle=True)
Training: True
Testing: False
class MyDataset(Dataset):
def init (self, file):
self.data = ... Read data & preprocess
def __len__(self):
return len(self.data) Returns the size of the dataset
Dataset & Dataloader
dataset = MyDataset(file)
DataLoader
getitem (0) 0
getitem (1) 1
Dataset getitem (2) 2 batch_size
getitem (3) 3
getitem (4) 4
mini-batch
Tensors
● High-dimensional matrices (arrays)
4
3
5
3
5 5
(5, ) (3, 5) (4, 5, 3)
● Addition ● Summation
z = x + y y = x.sum()
● Subtraction ● Mean
z = x - y y = x.mean()
● Power
y = x.pow(2)
Tensors – Common Operations
● Transpose: transpose two specified dimensions
>>> x = x.transpose(0, 1)
>>> x.shape 3
torch.Size([3, 2])
2
Tensors – Common Operations
● Squeeze: remove the specified dimension with length = 1
>>> x.shape 1
3
2
torch.Size([1, 2, 3])
>>> x = x.squeeze(0)
(dim = 0)
>>> x.shape 2
torch.Size([2, 3]) 3
Tensors – Common Operations
● Unsqueeze: expand a new dimension
>>> x.shape 2
torch.Size([2, 1, 3]) 3
1
x 2
Tensors – Common Operations 3
1
>>> w.shape
w
torch.Size([2, 6, 3]) 2
3
6
more operators: https://pytorch.org/docs/stable/tensors.html
Tensors – Data Type
● Using different data types for model and data will cause errors.
PyTorch NumPy
x.shape x.shape
x.dtype x.dtype
ref: https://github.com/wkentaro/pytorch-for-numpy-users
Tensors – PyTorch v.s. NumPy
● Many functions have the same names as well
PyTorch NumPy
x.reshape / x.view x.reshape
x.squeeze() x.squeeze()
x.unsqueeze(1) np.expand_dims(x, 1)
ref: https://github.com/wkentaro/pytorch-for-numpy-users
Tensors – Device
● Tensors & modules will be computed with CPU by default
torch.cuda.is_available()
2 >>> z = x.pow(2).sum()
3 >>> z.backward()
4 >>> x.grad
1 2
tensor([[ 2., 0.],
[-2., 2.]])
3 4
Optimization
Algorithm
torch.nn – Network Layers
● Linear Layer (Fully-connected Layer)
nn.Linear(in_features, out_features)
y1
x1
y2
x2
32 y3 64 W x + =
x3 x b y
(64x32)
...
...
x
32
y
64
torch.nn – Network Parameters
● Linear Layer (Fully-connected Layer)
>>> layer.weight.shape
torch.Size([64, 32]) W x x + b = y
(64x32)
>>> layer.bias.shape
torch.Size([64])
torch.nn – Non-Linear Activation Functions
● Sigmoid Activation
nn.Sigmoid()
● ReLU Activation
nn.ReLU()
class MyModel(nn.Module):
def init(self):
super(MyModel, self).init ()
self.net = nn.Sequential(
nn.Linear(10, 32), Initialize your model & define layers
nn.Sigmoid(),
nn.Linear(32, 1)
)
Optimization
Algorithm
torch.nn – Loss Functions
● Mean Squared Error (for regression tasks)
criterion = nn.MSELoss()
criterion = nn.CrossEntropyLoss()
Optimization
Algorithm
torch.optim
● Gradient-based optimization algorithms that adjust network parameters
to reduce error. (See Adaptive Learning Rate lecture video)
Load Data
Define Neural
Network
Optimization
Algorithm Step 5.
Entire Procedure
Neural Network Training Setup
total_loss = 0
preds = []
● with torch.no_grad()
Prevents calculations from being added into gradient computation graph.
Usually used to prevent accidental training on validation/testing data.
Save/Load Trained Models
● Save
torch.save(model.state_dict(), path)
● Load
ckpt = torch.load(path)
model.load_state_dict(ckpt)
More About PyTorch
● torchaudio
○ speech/audio processing
● torchtext
○ natural language processing
● torchvision
○ computer vision
● skorch
○ scikit-learn + pyTorch
More About PyTorch
● Useful github repositories using PyTorch
○ Huggingface Transformers (transformer models: BERT, GPT, ...)
○ Fairseq (sequence modeling for NLP & speech)
○ ESPnet (speech recognition, translation, synthesis, ...)
○ Most implementations of recent deep learning papers
○ ...
References
● Machine Learning 2022 Spring Pytorch Tutorial
● Official Pytorch Tutorials
● https://numpy.org/
Any questions?