pytorch学习教程(一)——pytorch基础
本文介绍了pytorch相关操作的基础知识,例如自动求导例子,如何从numpy中导入数据,如何使用pytorch输入数据集,如何自己创建数据集,如何加载预训练模型,如何保存和加载模型。
pytorch基础 目录 1.基础自动求导例子1
2.基础自动求导例子2
3.从numpy中导入数据
4.Input pipline(提供数据)
5.Input pipline for custom dataset
6.预训练模型
7.保存和导入模型
1. 基础自动求导例子1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 创建张量 x = torch.tensor(1., requires_grad=True) y = torch.tensor(2., requires_grad=True) b = torch.tensor(3., requires_grad=True) # 创建计算图 y = w * x + b # y = 2 * x + 3 # 计算梯度 y.backward() # 打印梯度 print(x.grad) # x.grad = 2 print(w.grad) # w.grad = 1 print(b.grad) # b.grad = 1
2. 基础自动求导例子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 # 创建形状为(10, 3)和(10, 2)的张量 x = torch.randn(10, 3) y = torch.randn(10, 2) # 搭建一个全连接层 linear = nn.Linear(3, 2) print('w: ', linear.weight) print('b: ', linear.bias) # 构建loss函数和优化器 criterion = nn.MESLoss() optimizer = torch.optim.SGD(linear.paramters(), lr=0.01) # 前向传播 pred = linear(x) # 计算loss loss = criterion(pred, y) print('loss: ', loss.item()) # 反向传播 loss.backward() # 打印梯度 print('dL/dw: ', linear.weight.grad) print('dL/db: ', linear.bias.grad) # 一阶梯度下降 optimizer.step() # You can also perform gradient descent at the low level. # linear.weight.data.sub_(0.01 * linear.weight.grad.data) # linear.bias.data.sub_(0.01 * linear.bias.grad.data) # 输出loss在一阶梯度下降后的结果 pred = linear(x) loss = criterion(pred, y) print('loss after 1 step optimization: ', loss.item())
3. 从numpy中导入数据 1 2 3 4 5 6 7 8 # 创建numpy数组 x = np.array([1, 2], [3, 4]) # 将numpy数组转换为torch张量 y = torch.from_numpy(x) # 将torch张量转换为numpy数组 z = y.numpy()
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 # 下载并构建CIFAR-10 数据集 train_dataset = torchvision.datasets.CIFAR10(root='../../data/', train=True, transform=transforms.ToTensor(), download=True) # 提取一对数据(从磁盘读取数据) image, label = train_dataset[0] print(image.size()) print(label) # 数据加载器data loader——这以非常简单的方式提供队列和线程 train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True) # 当迭代开始,队列和线程开始从文件中导入数据 data_iter = iter(train_loader) # mini-batch 图片和label images, labels = data_iter.next() # 数据加载器的实际使用情况如下。 for images, labels in train_loader: # 训练的代码应该写在这里 pass
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): # TODO # 1. 初始化文件路径或一系列文件名 pass def __getitem__(self, index): # TODO # 1. 从文件中读取一个数据(例如,使用numpy.fromfile, PIL.Image.open) # 2. 数据预处理(例如,torchvision.Transform) # 3. 返回数据对(例如,image 和 label) pass def __len__(self): # 你应该将数据集的大小变为0 return 0 # 然后你就可以使用预先构建的数据加载器 custom_dataset = CustomDataset() train_loader = torch.utils.data.DataLoader(dataset=custom_dataset, batch_size=64, shuffle=True)
6. 预训练模型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 下载和导入ResNet-18的预训练模型 resnet = torchvision.models.resnet18(pretrained=True) # 如果你只想微调模型的top 层,可以安装如下方式设置: for param in resnet.paramters(): param.requires_grad = False # 替换top层用于微调 resnet.fc = nn.Linear(resnet.fc.in_features, 100) # 100只是一个例子 # 前向传播 images = torch.randn(64, 3, 224, 224) outputs = resnet(images) print(outputs.size()) # (64, 100)
7. 保存和导入模型 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')
原文链接:https://github.com/yunjey/pytorch-tutorial/tree/master/tutorials/01-basics/pytorch_basics/main.py