Pytorch学习开始
入门的材料来自两个地方:
第一个是官网教程:WELCOME TO PYTORCH TUTORIALS,特别是官网的六十分钟入门教程 DEEP LEARNING WITH PYTORCH: A 60 MINUTE BLITZ。
第二个是韩国大神Yunjey Choi的Repo:pytorch-tutorial,代码写得干净整洁。
目的:我是直接把Yunjey的教程的python代码挪到Jupyter Notebook上来,一方面可以看到运行结果,另一方面可以添加注释和相关资料链接。方便后面查阅。
顺便一题,我的Pytorch的版本是0.4.1
1 2
| import torch print(torch.__version__)
|
0.4.1
PyTorch 基础篇(1):Pytorch基础
参考代码
yunjey的 pytorch tutorial系列
1 2 3 4 5 6
| import torch import torchvision import torch.nn as nn import numpy as np import torchvision.transforms as transforms
|
autograd(自动求导/求梯度) 基础案例1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| x = torch.tensor(1., requires_grad=True) w = torch.tensor(2., requires_grad=True) b = torch.tensor(3., requires_grad=True)
y = w * x + b
y.backward()
print(x.grad) print(w.grad) print(b.grad)
|
tensor(2.)
tensor(1.)
tensor(1.)
autograd(自动求导/求梯度) 基础案例2
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
| x = torch.randn(10, 3) y = torch.randn(10, 2)
linear = nn.Linear(3, 2) print ('w: ', linear.weight) print ('b: ', linear.bias)
criterion = nn.MSELoss() optimizer = torch.optim.SGD(linear.parameters(), lr=0.01)
pred = linear(x)
loss = criterion(pred, y) print('loss: ', loss.item())
loss.backward()
print ('dL/dw: ', linear.weight.grad) print ('dL/db: ', linear.bias.grad)
optimizer.step()
pred = linear(x) loss = criterion(pred, y) print('loss after 1 step optimization: ', loss.item())
|
w: Parameter containing:
tensor([[ 0.5180, 0.2238, -0.5470],
[ 0.1531, 0.2152, -0.4022]], requires_grad=True)
b: Parameter containing:
tensor([-0.2110, -0.2629], requires_grad=True)
loss: 0.8057981729507446
dL/dw: tensor([[-0.0315, 0.1169, -0.8623],
[ 0.4858, 0.5005, -0.0223]])
dL/db: tensor([0.1065, 0.0955])
loss after 1 step optimization: 0.7932316660881042
从Numpy装载数据
1 2 3 4 5 6 7 8 9 10 11
| x = np.array([[1, 2], [3, 4]]) print(x)
y = torch.from_numpy(x) print(y)
z = y.numpy() print(z)
|
[[1 2]
[3 4]]
tensor([[1, 2],
[3, 4]])
[[1 2]
[3 4]]
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
|
train_dataset = torchvision.datasets.CIFAR10(root='../../../data/', train=True, transform=transforms.ToTensor(), download=True)
image, label = train_dataset[0] print (image.size()) print (label)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
data_iter = iter(train_loader)
images, labels = data_iter.next()
for images, labels in train_loader: pass
|
Files already downloaded and verified
torch.Size([3, 32, 32])
6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class CustomDataset(torch.utils.data.Dataset): def __init__(self): pass def __getitem__(self, index): pass def __len__(self): return 0
custom_dataset = CustomDataset() train_loader = torch.utils.data.DataLoader(dataset=custom_dataset, batch_size=64, shuffle=True)
|
预训练模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| resnet = torchvision.models.resnet18(pretrained=True)
for param in resnet.parameters(): param.requires_grad = False
resnet.fc = nn.Linear(resnet.fc.in_features, 100)
images = torch.randn(64, 3, 224, 224) outputs = resnet(images) print (outputs.size())
|
torch.Size([64, 100])
保存和加载模型
1 2 3 4 5 6 7
| torch.save(resnet, 'model.ckpt') model = torch.load('model.ckpt')
torch.save(resnet.state_dict(), 'params.ckpt') resnet.load_state_dict(torch.load('params.ckpt'))
|