Videos Link
PyTorch_Tutorial1 https://www.youtube.com/watch?v=85uJ9hSaXig
PyTorch_Tutorial2 https://www.youtube.com/watch?v=VbqNn20FoHM
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
Tutorial1
Prerequisites
- We assume u r already familiar with…
- Python3
if-else, loop, function, file IO, class, - Deep Learning Basics
Prof. Lee’s 1st & 2nd lecture videos.
Some knowledge of NumPy will also be useful!
PyTorch
- An machine learningframework in Python
- Two main features:
- N-dimensional Tensor computation(like NumPy) on GPUS
- Automatic differentiation for training deep neural networks
Training Neural Networks
Step1–Load dataset
- Dataset: stores data samples and expected values
- Dataloader: groups data in batches, enables multiprocessing
1 | dataset = MyDataset(file) |
Dataset & Dataloader
1 | from torch.utils.data import Dataset, DataLoader |
Tensor
Tensor–Shape of Tensor
- Check with.shape()
Note: dim in Pytorch == axis in NumPy
Tensor–Creating Tensors
- Directly from data(list or numpy.ndarray)
1
2
3
4
5
6x = torch.tensor([[1, -1], [-1, 1]])
x = torch.from_numpy(np.array([[1, -1], [-1, 1]]))
Output:
tensor([1., -1.],
[-1, 1.]) - Tensor of constant zeros & ones
1
2
3
4
5
6
7
8x = torch.zeros([2, 2])
x = torch.ones([1, 2, 5])
Output:
tensor([[0.,0.],
[0.,0.]])
tensor([[[1.,1.,1.,1.,1.],
[1.,1.,1.,1.,1.]]])
Tensors–Comon Operations
Common arithmetic func are supported, such as:
- Addition z = x + y
- Subtraction z = x - y
- Power y = x.pow(2)
- Summation y = x.sum()
- Mean y = x.mean()
Transpose: transpose 2 specified dimensionsSqueeze: remove the specified dimension with length = 11
2
3
4
5
62, 3]) x = torch.zeros([
x.shape
torch.Size([2, 3])
0, 1) x = x.transpose(
x.shape
torch.Size([3, 2])Unsqueeze: expand a new dimension1
2
3
4
5
6
71, 2, 3]) x = torch.zeros([
x.shape
torch.Size([1, 2, 3])
0) x = x.squeeze(
# 消除第一纬度
x.shape
torch.Size([2, 3])1
2
3
4
5
62, 3]) x = torch.zeros([
x.shape
torch.Size([2, 3])
1) # dim = 1 x = x.unsqueeze(
x.shape
torch.Size([2, 1, 3])
Cat: concatenate multiple tensors
1 | 2, 1, 3]) x = torch.zeros([ |
Tensor–Data Type
- Using different data types for model and data will cause errors
Data type | dtype | tensor |
---|---|---|
32-bit floating point | torch.float | torch.Floattensor |
64-bit integer (signed) | torch.long | torch.LongTensor |
- Similar attributes & same func
PyTorch | NumPy |
---|---|
x.shape | x.shape |
x.dtype | x.dtype |
x.reshape/x.view | x.reshape |
x.squeeze() | x.squeeze() |
x.unsqueeze(1) | np.expand_dims(x,1) |
Tensor–Device
Tensors & modules will be computed with CPU by default
Use.to() to move tensors to appropriate devices.
CPU
x = x.to(‘cpu’)GPU
x = x.to(‘cuda’)
Tensors–Device(GPU)
Check if your computer has NVIDIA GPI
1
torch.cuda.is_available()
Multiple GPUs: specify ‘cuda:0’, ‘cuda:1’, ‘cuda:2’,…
Why use GPUs?
- Parallel computing with more cores for arithmetic calculations
- see What is a GPU and do you need one in Deep Learning?
Tensors–Gradient Calculation
1 | 1. , 0.], [-1., 1.]], requires_grad = True) x = torch.tensor([[ |
Step2–Build NN
torch.nn–Network Layers
- Linear Layer(Fully-connected Layer)
nn.Linear(in_features, out_features)
1 | 32, 64) layer = torch.nn.Linear( |
torch.nn – Non-Linear Activation Function
- Sigmoid Activation
nn.Sigmoid - ReLU Activation
nn.ReLU
torch.nn – Build Neural Network
Step3–Loss Func & Optim
torch.nn–Loss Functions
- Mean Squared Error(for regression tasks)
criterion = nn.MSELoss() - Cross Entropy(for classification tasks)
criterion = nn.CrossEntropyLoss() - loss = criterion(model_out, expected_value)
torch.optim
Gradient based optimization algorithms that adjust network
parameters to reduce error.E.g. Stochastic Gradient Descent(SGD)
torch.optim.SGD(model.parameters(), lr, momentum = 0)
optimizer = torch.optim.SGD(model.parameters(), lr, momentum = 0)
- For every batch of data:
- Call optimizer.zero_grad() to reset gradients of model parameters.
- Call loss.backward() to backpropagate gradients of prediction loss.
- Call optimizer.step() to adjust model parameters.
Step4 Train&Test&Pred
Train
1 | # Training setup |
Test
1 | model.eval() # set model to evaluation mode |
Predict
1 | model.eval() # set model to evaluation mode |
Notice
eval & no_grad
load & save
Tutorial2
load data/Preprocessing
Load data: you can use pandas to load a csv file.
1 | train_data = pd.read_csv("./covid.train.csv").drop(columns=['date']).values |
Preprocessing: Get model inputs and labels.
1 | x_train, y_train = train_data[:,:-1], train_data[:,-1] # python的索引来说,右边】不包含 |
Dataset
- init: Read data and preprocess
- getitem: Return one sample at a time. In this case, one sample includes a 117 dimensional feature and a label
- len: Return the size of the dataset. In this case, it is 2699
Dataloader
1 | train_loader = DataLoader(train_dataset, ba) |
- Group data into batches
- If you set shuffle = True, dataloader will permutes the indices of all samples automatically.
- We often set shuffle = True during training
- You can check this page Advantage to shuffle a dataset if u are curious about why
Model Building
1 | class My_model(nn.Module): |
Criterion
1 | # 调用 Loss Func |
Optimizer
1 | optimizer = torch.optim.SGD(model.parameters(), lr = 1e-5, momentum=0.9) |
Training loop
1 | for epoch in range(3000): |
- Post title: PyTorch_Tutorial (TA Yuan Tseng)
- Create time: 2022-03-21 11:33:24
- Post link: Tutorial/pytorch-tutorial-ta-yuan-tseng/
- Copyright notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.