源码:github code
1.自动求导基础例子 1
2.自动求导基础例子 2
3.numpy转tensor;tensor转numpy
4.输入管道 and自定义自用数据输入管道
5.预训练模型(模型微调)、保存、加载模型(整个模型or仅模型的参数)
1.自动求导基础例子 1
import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms
# Create tensors.创建张量
x = torch.tensor(9., requires_grad=True)
w = torch.tensor(6., requires_grad=True)
b = torch.tensor(7., requires_grad=True)
# Build a computational graph.建立计算图
y = w * x + b # y = 6 * x + 7
# Compute gradients.计算梯度
y.backward()
# Print out the gradients.打印梯度
print(x.grad) # x.grad 对x求导
print(w.grad) # w.grad 对w求导
print(b.grad) # b.grad 对b求导
print(y.grad) #y.grad 对y求导
tensor(6.)
tensor(9.)
tensor(1.)
None
2.自动求导基础例子 2
import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms
# Create tensors of shape (10, 3) and (10, 2).
# 创建10组数据,x源数据特征维度为3,y目标数据维度为2
x = torch.randn(10, 3)
y = torch.randn(10, 2)
# Build a fully connected layer.
# 建立一个全连接层
linear = nn.Linear(3, 2)
print('w: ', linear.weight)
print('b: ', linear.bias)
# Build loss function and optimizer.
# 建立损失函数和梯度优化器
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(linear.parameters(), lr=0.01)
# Forward pass.
# 前向传递
pred = linear(x)
# Compute loss.
# 计算损失值
loss = criterion(pred, y)
print('loss: ', loss.item())
# Backward pass.
# 反向传递
loss.backward()
# Print out the gradients.
# 打印梯度
print('dL/dw: ', linear.weight.grad)
print('dL/db: ', linear.bias.grad)
# 1-step gradient descent.
# 梯度下降优化
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)
# Print out the loss after 1-step gradient descent.
# 打印第一步梯度优化后的loss值
pred = linear(x)
loss = criterion(pred, y)
print('loss after 1 step optimization: ', loss.item())
w: Parameter containing:
tensor([[-0.2890, 0.4896, 0.5295], [-0.5184, 0.0823, -0.0142]], requires_grad=True)
b: Parameter containing:
tensor([-0.2531, 0.1505], requires_grad=True)
loss: 2.2962872982025146
dL/dw: tensor([[-0.2974, -0.1092, 0.8263], [-1.0187, -0.3140, 0.0013]])
dL/db: tensor([-0.2196, 1.0152])
loss after 1 step optimization: 2.2665205001831055
3.numpy转tensor;tensor转numpy
import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms
# Create a numpy array.
x = np.array([[1, 2], [3, 4]])
print('numpy x:',x)
# Convert the numpy array to a torch tensor.
# numpy数组转换为tensor
y = torch.from_numpy(x)
print('tensor y:',y)
# Convert the torch tensor to a numpy array.
# tensor转换为numpy数组
z = y.numpy()
print('numpy z:',z)
numpy x: [[1 2] [3 4]]
tensor y: tensor([[1, 2], [3, 4]])
numpy z: [[1 2] [3 4]]
4.输入管道 and自定义自用数据输入管道
import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms
# Download and construct CIFAR-10 dataset.
# 下载10分类图像数据
train_dataset = torchvision.datasets.CIFAR10(root='../../data/',
train=True,
transform=transforms.ToTensor(),
download=True)
# Fetch one data pair (read data from disk).
# 取一个数据对
image, label = train_dataset[0]
print(image.size())
print(label)
# Data loader (this provides queues and threads in a very simple way).
# 加载数据
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=64,
shuffle=True)
# When iteration starts, queue and thread start to load data from files.
# 使数据变成可迭代的数据
data_iter = iter(train_loader)
# Mini-batch images and labels.
# 通过.next()获取一小批次数据
images, labels = data_iter.next()
# Actual usage of the data loader is as below.
for images, labels in train_loader:
# Training code should be written here.
pass
# ----------自定义数据输入管道------------------ #
# You should build your custom dataset as below.
class CustomDataset(torch.utils.data.Dataset):
def __init__(self):
# TODO
# 1. Initialize file paths or a list of file names.
pass
def __getitem__(self, index):
# TODO
# 1. Read one data from file (e.g. using numpy.fromfile, PIL.Image.open).
# 2. Preprocess the data (e.g. torchvision.Transform).
# 3. Return a data pair (e.g. image and label).
pass
def __len__(self):
# You should change 0 to the total size of your dataset.
return 0
# You can then use the prebuilt data loader.
custom_dataset = CustomDataset()
train_loader = torch.utils.data.DataLoader(dataset=custom_dataset,
batch_size=64,
shuffle=True)
5.预训练模型、保存、加载模型
import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms
# Download and load the pretrained ResNet-18.
# resnet = torchvision.models.resnet18(pretrained=True)
resnet = torchvision.models.resnet18(pretrained=False)
# If you want to finetune only the top layer of the model, set as below.
for param in resnet.parameters():
param.requires_grad = False
print(type(param.data), param.size())
# Replace the top layer for finetuning.
resnet.fc = nn.Linear(resnet.fc.in_features, 100) # 100 is an example.
# Forward pass.
# 一小批图片,resnet模型预测
images = torch.randn(64, 3, 224, 224)
outputs = resnet(images)
print(outputs.size()) # (64, 100)
# Forward pass.
# 一张图片,resnet模型预测
images = torch.randn(1, 3, 224, 224)
outputs = resnet(images)
print(outputs) # (64, 100)
# Save and load the entire model.
# 保存、加载整个模型
torch.save(resnet, 'model.ckpt')
model = torch.load('model.ckpt')
# Save and load only the model parameters (recommended).
# 仅保存、加载模型的参数
torch.save(resnet.state_dict(), 'params.ckpt')
resnet.load_state_dict(torch.load('params.ckpt'))
<class 'torch.Tensor'> torch.Size([64, 3, 7, 7])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([128, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128, 128, 3, 3])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128, 64, 1, 1])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128, 128, 3, 3])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128, 128, 3, 3])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([256, 128, 3, 3])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256, 256, 3, 3])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256, 128, 1, 1])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256, 256, 3, 3])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256, 256, 3, 3])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([512, 256, 3, 3])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512, 512, 3, 3])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512, 256, 1, 1])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512, 512, 3, 3])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512, 512, 3, 3])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([1000, 512])
<class 'torch.Tensor'> torch.Size([1000])
torch.Size([64, 100])
tensor([[-0.1283, -1.1639, 0.0621, -0.2726, 0.8435, -0.4389, -0.1071, 0.4760,
0.6909, -0.2902, -0.4895, 0.1199, -0.0188, 0.2916, 0.1144, 0.1217,
-0.1636, 0.5199, 0.9729, -0.2111, -0.6451, 0.7002, -0.9556, 0.0440,
1.0738, -0.5497, 0.3759, 0.2009, 0.1486, 0.2877, -0.0455, -0.3775,
-0.3144, -0.3720, -0.6317, 0.2952, 0.5883, 0.0979, -0.0203, 0.0584,
0.4892, -0.0672, 0.8977, -0.1919, 0.7094, 0.2328, -0.6055, 0.4562,
0.0236, -0.6255, -0.6796, 0.2483, 0.9799, -0.7623, 0.4199, 0.0543,
-1.0435, -0.5571, -0.4757, -0.4201, -0.0206, -1.0947, 0.1431, 0.0288,
0.0987, 0.4257, -0.3387, -0.3624, 0.3040, -0.5715, -1.3718, 0.2144,
-0.3756, -1.2408, -0.3292, -0.3726, 0.1383, -0.8428, -0.4842, 0.2166,
0.7600, 0.1267, -0.3318, 0.5117, 0.1809, -0.1803, 0.3676, 0.8243,
-0.3718, -0.3557, 0.0967, 0.2485, 0.0497, -0.0762, 0.2572, -0.0783,
-0.3285, -0.2424, -0.0206, -0.2670]], grad_fn=<AddmmBackward>)