pytorch基础

定义一个模型

image-20230808151532902

定义神经网络

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
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 5x5 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square you can only specify a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
print(net)

数据处理和计算

创建矩阵

1
2
3
4
5
6
x = torch.empty(5, 3)						# 构造5x3的矩阵,不初始化
x = torch.rand(5, 3) # 随机初始化
x = torch.zeros(5, 3, dtype=torch.long) # 数据全为0,数据结构为long
x = torch.tensor([5.5, 3]) # 构造张量,直接用数据
x = torch.randn_like(x, dtype=torch.float) # 构建一个基于x tensor的tensor
x.size() # 查看维度

加法

  • 直接使用**+**号

  • ```python
    torch.add(x, y)
    torch.add(x, y, out=result)

    adds x to y,类比使张量发生变化的操作都有前缀

    y.add_(x)

    改变tensor大小或形状

    y = x.view(16)
    z = x.view(-1, 8) # the size -1 is inferred from other dimensions(自判)
    x.item() # 获取tensor的value

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15



    ### 自动微分

    > 首先要开启跟踪,才可以跟踪计算,最后再调用backward()计算梯度。

    ```python
    # 创建一个张量,跟踪计算
    x = torch.ones(2, 2, requires_grad=True)
    # 向后传播
    out.backward() or out.backward(torch.tensor(1.))
    # 停止跟踪
    with torch.no_grad()

前向传播

1
2
3
4
5
6
7
8
9
10
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square you can only specify a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x

返模型参数

net.parameters()

1
2
3
params = list(net.parameters())
print(len(params))
print(params[0].size()) # conv1's .weight

随机梯度反向传播

1
2
3
4
5
# 将梯度缓存置零
net.zero_grad()
out.backward(torch.randn(1, 10))
# 反向传播损失
loss.backward()

更新网络参数

  1. 随机梯度下降
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
weight = weight - learning_rate * gradient
# python实现
learning_rate = 0.01
for f in net.parameters():
f.data.sub_(f.grad.data * learning_rate)

# pytorch实现
import torch.optim as optim
# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)
# in your training loop:
optimizer.zero_grad() # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step() # Does the update

显示图像

1
2
3
4
5
6
7
8
9
10
11
12
13
import matplotlib.pyplot as plt
import numpy as np
# functions to show an image
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
# show images
imshow(torchvision.utils.make_grid(images))

数据预处理

不同的数据类型用不同的方法

  • 对于图像,可以用 Pillow,OpenCV
    • 对于视觉,有torchvision包
  • 对于语音,可以用 scipy,librosa
  • 对于文本,可以直接用 Python 或 Cython 基础数据加载模块,或者用 NLTK 和 SpaCy

数据加载和归一化处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform)

trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)

testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

模型定义

LOSS定义

计算均方误差

1
2
criterion = nn.MSELoss()
loss = criterion(output, target)

模型训练

创建一个图像分类器步骤:

  1. 使用torchvision加载并且归一化CIFAR10的训练和测试数据集
  2. 定义一个卷积神经网络
  3. 定义一个损失函数
  4. 在训练样本数据上训练网络
  5. 在测试样本数据上测试网络

卷积神经网络

3通道的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x

定义损失函数和优化器

1
2
3
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

定义迭代循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')

pytorch基础
https://blog.xsaistudio.cn/Deep-learning/2023/08/07/deep learning/pytorch基础/
作者
YWM
发布于
2023年8月7日
许可协议