PS:本文中代码在jupyter中实现

前置工作

必要包含库

1
2
3
4
5
import torch
from IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random

生成随机数据集

1
2
3
4
5
6
7
8
num_inputs = 2 # 样本维数
num_examples = 1000 # 样本大小
true_w = [2, -3.4] # 样本实际参数
true_b = 4.2

features = torch.tensor(np.random.normal(0, 1, (num_examples, num_inputs)), dtype=torch.float) # 生成特征值
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b # 生成标签
labels += torch.tensor(np.random.normal(0, 0.01, size=labels.size()), dtype=torch.float) # 加入随机噪声

样本数据集读取

1
2
3
4
5
6
7
8
9
10
11
12
import torch.utils.data as Data

batch_size = 10 # 每次读取数据集个数
# 组合训练数据集的特征和标签
dataset = Data.TensorDataset(features, labels)
# 随机读取数据集
data_iter = Data.DataLoader(dataset, batch_size, shuffle=True)

# 使用方法
for X, y in data_iter:
print(X, y)
break

输出结果:

1
2
3
4
5
6
7
8
9
10
11
tensor([[ 0.2228, -0.5939],
[ 0.3221, -1.1835],
[ 0.7504, 0.6778],
[-0.2592, -0.0356],
[-1.3732, -0.1442],
[-2.1898, 2.0988],
[-0.6950, -1.0098],
[-1.3058, -0.5089],
[ 0.7621, -0.0108],
[ 0.8707, -1.9234]]) tensor([ 6.6686, 8.8609, 3.4102, 3.8032, 1.9515, -7.3237, 6.2377, 3.3101,
5.7704, 12.4803])

定义模型参数

定义模型

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
'''
class LinearNet(nn.Module):
def __init__(self, n_feature):
super(LinearNet, self).__init__()
self.linear = nn.Linear(n_feature, 1)

# forward 定义前向传播
def forward(self, x):
y = self.linear(x)
return y

net = LinearNet(num_inputs)
其他定义方法
# 写法一
net = nn.Sequential(
nn.Linear(num_inputs, 1)
# 此处还可以传入其他层
)

# 写法二
net = nn.Sequential()
net.add_module('linear', nn.Linear(num_inputs, 1))
# net.add_module ......

# 写法三
from collections import OrderedDict
net = nn.Sequential(OrderedDict([
('linear', nn.Linear(num_inputs, 1))
# ......
]))
'''
# 使用方法一
net = nn.Sequential(
nn.Linear(num_inputs, 1)
)
print(net)

# 查看模型所有可学习参数
for param in net.parameters():
print(param)

初始化模型

1
2
3
4
from torch.nn import init

init.normal_(net[0].weight, mean=0, std=0.01)
init.constant_(net[0].bias, val=0)

注:若使用class定义模型net[0] 应替换为net的相应属性(按此代码中应该改为net.linear)

定义损失函数及优化算法

1
2
3
4
5
6
7
# 定义损失函数
loss = nn.MSELoss()

# 定义优化算法
import torch.optim as optim
optimizer = optim.SGD(net.parameters(), lr=0.03)
print(optimizer)

输出结果:

1
2
3
4
5
6
7
8
SGD (
Parameter Group 0
dampening: 0
lr: 0.03
momentum: 0
nesterov: False
weight_decay: 0
)

训练模型

1
2
3
4
5
6
7
8
9
num_epochs = 3
for epoch in range(1, num_epochs + 1):
for X, y in data_iter:
output = net(X)
l = loss(output, y.view(-1, 1))
optimizer.zero_grad() # 梯度清零
l.backward()
optimizer.step()
print('epoch %d, loss: %f' % (epoch, l.item()))

训练结果:

1
2
3
epoch 1, loss: 0.000327
epoch 2, loss: 0.000098
epoch 3, loss: 0.000112