动手学习深度学习(6)模型选择与欠拟合和过拟合

茴香豆 Lv5

我们如何才能确定模型是真正发现了一种泛化的模式, 而不是简单地记住了数据呢? 我们的目标是发现某些模式, 这些模式捕捉到了我们训练集潜在总体的规律。 如果成功做到了这点,即使是对以前从未遇到过的个体, 模型也可以成功地评估风险。 如何发现可以泛化的模式是机器学习的根本问题。

误差:

  • 训练误差(training error):模型在训练数据集上计算得到的误差
  • 泛化误差(generalization error):模型应用在同样从原始样本的分布中抽取的无限多数据样本时模型误差的期望。简单来说,是模型在新数据上的误差。

数据集:(验证数据集一定不要和测试数据集混合在一起!

  • 验证数据集:一个用来评估模型好坏的数据集
  • 测试数据集:只用一次的数据集

通常没有足够数据进行使用,所以常采用K折交叉验证,一般取5或10

过拟合和欠拟合

数据简单 数据复杂
模型容量低 正常 欠拟合
模型容量高 过拟合 正常

模型容量:拟合各种函数的能力

  • 低容量的模型难以拟合训练数据
  • 高容量的模型可以记住所有的训练数据

实际中一般靠观察训练误差和验证误差的差值,过小,欠拟合,过大,过拟合。

一些正则化模型的技术

权重衰退(weight-decay)

通过限制参数值的选择范围来控制模型容量,达到避免过拟合的效果
MATHJAX-SSR-23

  • 通常不限制偏移b
  • 小的 \theta 意味着更强的正则项

可以使用均方范数作为柔性限制。对于每个 MATHJAX-SSR-24,都可以找到 \lambda 使得上面的目标函数等价于下面的公式
MATHJAX-SSR-25
超参数 \lambda 控制了正则项的重要程度

  • \lambda = 0 :无作用
  • \lambda -> \infty , 最优解 -> 0

从零实现

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
41
42
%matplotlib inline
import torch
from torch import nn
from d2l import torch as d2l
# 生成数据
n_train, n_test, num_inputs, batch_size = 20, 100, 200, 5
true_w, true_b = torch.ones((num_inputs, 1)) * 0.01, 0.05
train_data = d2l.synthetic_data(true_w, true_b, n_train)
train_iter = d2l.load_array(train_data, batch_size)
test_data = d2l.synthetic_data(true_w, true_b, n_test)
test_iter = d2l.load_array(test_data, batch_size, is_train=False)
# 随机初始化模型参数
def init_params():
w = torch.normal(0, 1, size=(num_inputs, 1), requires_grad=True)
b = torch.zeros(1, requires_grad=True)
return [w, b]
# 定义L2范数惩罚
def l2_penalty(w):
return torch.sum(w.pow(2)) / 2
# 定义训练代码实现
# 线性网络和平方损失没有变化, 所以我们通过d2l.linreg和d2l.squared_loss导入它们
def train(lambd):
w, b = init_params()
net, loss = lambda X: d2l.linreg(X, w, b), d2l.squared_loss
num_epochs, lr = 100, 0.003
animator = d2l.Animator(xlabel='epochs', ylabel='loss', yscale='log',
xlim=[5, num_epochs], legend=['train', 'test'])
for epoch in range(num_epochs):
for X, y in train_iter:
# 增加了L2范数惩罚项,
# 广播机制使l2_penalty(w)成为一个长度为batch_size的向量
l = loss(net(X), y) + lambd * l2_penalty(w)
l.sum().backward()
d2l.sgd([w, b], lr, batch_size)
if (epoch + 1) % 5 == 0:
animator.add(epoch + 1, (d2l.evaluate_loss(net, train_iter, loss),
d2l.evaluate_loss(net, test_iter, loss)))
print('w的L2范数是:', torch.norm(w).item())
# 忽略正则化直接训练
train(lambda=0)
# 使用权重衰减
train(lambda=3)

简洁实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def train_concise(wd):
net = nn.Sequential(nn.Linear(num_inputs, 1))
for param in net.parameters():
param.data.normal_()
loss = nn.MSELoss(reduction='none')
num_epochs, lr = 100, 0.003
# 偏置参数没有衰减
trainer = torch.optim.SGD([
{"params":net[0].weight,'weight_decay': wd},
{"params":net[0].bias}], lr=lr)
animator = d2l.Animator(xlabel='epochs', ylabel='loss', yscale='log',
xlim=[5, num_epochs], legend=['train', 'test'])
for epoch in range(num_epochs):
for X, y in train_iter:
trainer.zero_grad()
l = loss(net(X), y)
l.mean().backward()
trainer.step()
if (epoch + 1) % 5 == 0:
animator.add(epoch + 1,
(d2l.evaluate_loss(net, train_iter, loss),
d2l.evaluate_loss(net, test_iter, loss)))
print('w的L2范数:', net[0].weight.norm().item())

丢弃法(dropout)

丢弃法将一些输出项随机置0来控制模型复杂度,只用在训练过程中,预测过程会被去掉。

在数据中加入噪音,等价于一个正则。丢弃法是在全连接层间(后)加入噪音。通常将丢弃法作用在隐藏全连接层的输出上,即下一层的输入是上一层输出的进行dropout后的数据。
MATHJAX-SSR-26
根据此模型的设计,其期望值保持不变,即 𝐸[ℎ^′]=ℎ

从零开始实现

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
41
42
43
44
45
46
47
48
49
50
51
52
53
import torch
from torch import nn
from d2l import torch as d2l

def dropout_layer(X, dropout):
'''该函数以dropout的概率丢弃张量输入X中的元素'''
assert 0 <= dropout <= 1
# 在本情况中,所有元素都被丢弃
if dropout == 1:
return torch.zeros_like(X)
# 在本情况中,所有元素都被保留
if dropout == 0:
return X
mask = (torch.rand(X.shape) > dropout).float()
return mask * X / (1.0 - dropout)
# 定义模型参数
'''定义具有两个隐藏层的多层感知机,每个隐藏层包含256个单元'''
num_inputs, num_outputs, num_hiddens1, num_hiddens2 = 784, 10, 256, 256
# 定义模型
dropout1, dropout2 = 0.2, 0.5

class Net(nn.Module):
def __init__(self, num_inputs, num_outputs, num_hiddens1, num_hiddens2,
is_training = True):
super(Net, self).__init__()
self.num_inputs = num_inputs
self.training = is_training
self.lin1 = nn.Linear(num_inputs, num_hiddens1)
self.lin2 = nn.Linear(num_hiddens1, num_hiddens2)
self.lin3 = nn.Linear(num_hiddens2, num_outputs)
self.relu = nn.ReLU()

def forward(self, X):
H1 = self.relu(self.lin1(X.reshape((-1, self.num_inputs))))
# 只有在训练模型时才使用dropout
if self.training == True:
# 在第一个全连接层之后添加一个dropout层
H1 = dropout_layer(H1, dropout1)
H2 = self.relu(self.lin2(H1))
if self.training == True:
# 在第二个全连接层之后添加一个dropout层
H2 = dropout_layer(H2, dropout2)
out = self.lin3(H2)
return out


net = Net(num_inputs, num_outputs, num_hiddens1, num_hiddens2)
# 训练和测试
num_epochs, lr, batch_size = 10, 0.5, 256
loss = nn.CrossEntropyLoss(reduction='none')
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
trainer = torch.optim.SGD(net.parameters(), lr=lr)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

简洁实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
net = nn.Sequential(nn.Flatten(),
nn.Linear(784, 256),
nn.ReLU(),
# 在第一个全连接层之后添加一个dropout层
nn.Dropout(dropout1),
nn.Linear(256, 256),
nn.ReLU(),
# 在第二个全连接层之后添加一个dropout层
nn.Dropout(dropout2),
nn.Linear(256, 10))
# 权重
def init_weights(m):
if type(m) == nn.Linear:
nn.init.normal_(m.weight, std=0.01)

net.apply(init_weights);

# 训练和测试
trainer = torch.optim.SGD(net.parameters(), lr=lr)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
  • Title: 动手学习深度学习(6)模型选择与欠拟合和过拟合
  • Author: 茴香豆
  • Created at : 2022-10-28 16:17:57
  • Updated at : 2022-10-29 10:13:39
  • Link: https://hxiangdou.github.io/2022/10/28/DL_6/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
动手学习深度学习(6)模型选择与欠拟合和过拟合