import torch import torchvision import torch.nn as nn import torchvision.transforms as transforms import numpy as np
Basic autograd example 1
# 创建tensors 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=2x 3
# 计算梯度 y.backward()
# 打印梯度 print(x.grad) print(w.grad) print(b.grad)
tensor(2.) tensor(1.) tensor(1.)
Basic autograd example 2
# 创建tensors (10,3) (10,2) x = torch.randn(10,3) y = torch.randn(10,2) print(x) print(y)
tensor([[ 0.0191, -0.0627, 0.9497], [ 0.4595, -0.2088, -0.2721], [-0.2952, 0.0117, -0.2056], [ 0.1166, 0.3428, 0.0982], [-0.0378, 0.5100, 0.1092], [ 0.7950, -0.9665, -0.4156], [ 0.0040, 0.4486, -0.0933], [ 0.4311, -1.4221, -0.9666], [-0.4988, -0.6366, 1.2200], [ 0.6022, -0.5120, -0.0679]]) tensor([[-0.3624, 0.3539], [-0.2107, 1.1979], [-0.0265, -0.1485], [-2.2705, -0.9377], [-0.2282, -0.4204], [-0.4186, 0.6560], [-0.6858, -1.7737], [-0.4479, 0.8466], [ 0.3069, -0.4114], [-0.0926, -1.4976]])
# 构建一个全连接层
linear = nn.Linear(3,2)
print(linear)
print('w:',linear.weight)
print('b:',linear.bias)
Linear(in_features=3, out_features=2, bias=True)
w: Parameter containing:
tensor([[ 0.2266, 0.4908, -0.2475],
[ 0.2061, 0.5138, 0.1486]], requires_grad=True)
b: Parameter containing:
tensor([-0.3633, -0.5637], requires_grad=True)
# 构建损失函数和优化器
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(linear.parameters(),lr=0.01)
# Forward pass
pred = linear(x)
# 计算损失
loss = criterion(pred,y)
print('loss:',loss.item())
loss: 1.0909489393234253
# Backward pass.
loss.backward()
# 打印梯度
print('dL/dw:',linear.weight.grad)
print('dL/db:',linear.bias.grad)
dL/dw: tensor([[ 0.0548, 0.2640, -0.1351],
[-0.2189, 0.5867, 0.2022]])
dL/db: tensor([-0.0148, -0.4402])
# 进行一次梯度下降
optimizer.step()
# 打印一次梯度下降后的损失
pred = linear(x)
loss = criterion(pred,y)
print('loss after 1 step optimization:',loss.item())
loss after 1 step optimization: 1.0838005542755127
Loading data from numpy
# 创建数组
x = np.array(([1,2],[3,4]))
# 将数组转化为tensor
y = torch.from_numpy(x)
# 将tensor转化为数组
z = y.numpy()
Input pipeline
# 下载和构造CIFAR10数据集
train_dataset = torchvision.datasets.CIFAR10(root='../../data/',
train=True,
transform=transforms.ToTensor(),
download=True)
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ../../data/cifar-10-python.tar.gz
26.4%IOPub message rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_msg_rate_limit`.
Current values:
NotebookApp.iopub_msg_rate_limit=1000.0 (msgs/sec)
NotebookApp.rate_limit_window=3.0 (secs)
90.7%IOPub message rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_msg_rate_limit`.
Current values:
NotebookApp.iopub_msg_rate_limit=1000.0 (msgs/sec)
NotebookApp.rate_limit_window=3.0 (secs)
100.0%
Extracting ../../data/cifar-10-python.tar.gz to ../../data/
# 读取数据 one data pair
image,label = train_dataset[0]
print(image.size())
print(label)
torch.Size([3, 32, 32])
6
# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=64,
shuffle=True)
# 迭代开始后,队列和线程将从文件里读取数据
data_iter = iter(train_loader)
# Mini-batch images and labels
images,labels = data_iter.next()
# 实际使用data loader如下
for images,labels in train_loader:
# 训练代码
pass
Input pipeline for custom dataset
# 构建自定义 数据集
class CustomDataset(torch.utils.data.Dataset):
def __init__(self):
# 初始化文件路径或者文件名
pass
def __getitem__(self,index):
# 1.从文件中读取数据
# 2.处理数据
# 3.返回数据对(图像和标签 )
pass
def __len__(self):
# 改变数据集总的大小
return 0
# 使用构建好的data loader
custom_dataset = CustomDataset()
train_loader = torch.utils.data.DataLoader(dataset=custom_dataset,
batch_size=64,
shuffle=True)
Pretrained model
# 下载和导入预训练模型 ResNet-18
resnet = torchvision.models.resnet18(pretrained=True)
Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to C:\Users\wydxr/.cache\torch\hub\checkpoints\resnet18-f37072fd.pth
100.0%
# If you want to finetune only the top layer of the model, set as below.
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])
Save and load the model
# 保存和导入整个模型
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'))
<All keys matched successfully>