今天看啥  ›  专栏  ›  逐梦er

pytorch学习笔记(十二):权重衰减

逐梦er  · CSDN  ·  · 2020-08-02 10:15


本节介绍应对过拟合问题的常用方法:权重衰减(weight decay)。

1. 方法

权重衰减等价于 L 2 范数正则化(regularization)。正则化通过为模型损失函数添加惩罚项使学出的模型参数值较小,是应对过拟合的常用手段。我们先描述 L 2 范数正则化,再解释它为何又称权重衰减。

L 2 范数正则化在模型原损失函数基础上添加 L 2 范数惩罚项,从而得到训练所需要最小化的函数。 L 2 范数惩罚项指的是模型权重参数每个元素的平方和与一个正的常数的乘积。以3.1节(线性回归)中的线性回归损失函数

( w 1 , w 2 , b ) = 1 n n i = 1 1 2 ( x ( i ) 1 w 1 + x ( i ) 2 w 2 + b y ( i ) ) 2

为例,其中 w 1 , w 2 是权重参数, b 是偏差参数,样本 i 的输入为 x ( i ) 1 , x ( i ) 2 ,标签为 y ( i ) ,样本数为 n 。将权重参数用向量 w = [ w 1 , w 2 ] 表示,带有 L 2 范数惩罚项的新损失函数为

( w 1 , w 2 , b ) + λ 2 n w 2 ,

其中超参数 λ > 0 。当权重参数均为0时,惩罚项最小。当 λ 较大时,惩罚项在损失函数中的比重较大,这通常会使学到的权重参数的元素较接近0。当 λ 设为0时,惩罚项完全不起作用。上式中 L 2 范数平方 w 2 展开后得到 w 2 1 + w 2 2 。有了 L 2 范数惩罚项后,在小批量随机梯度下降中,我们将线性回归一节中权重 w 1 w 2 的迭代方式更改为

w 1 ( 1 η λ B ) w 1 η B i B x ( i ) 1 ( x ( i ) 1 w 1 + x ( i ) 2 w 2 + b y ( i ) ) , w 2 ( 1 η λ B ) w 2 η B i B x ( i ) 2 ( x ( i ) 1 w 1 + x ( i ) 2 w 2 + b y ( i ) ) .

可见, L 2 范数正则化令权重 w 1 w 2 先自乘小于1的数,再减去不含惩罚项的梯度。因此, L 2 范数正则化又叫权重衰减。权重衰减通过惩罚绝对值较大的模型参数为需要学习的模型增加了限制,这可能对过拟合有效。实际场景中,我们有时也在惩罚项中添加偏差元素的平方和。

2. 高维线性回归实验

下面,我们以高维线性回归为例来引入一个过拟合问题,并使用权重衰减来应对过拟合。设数据样本特征的维度为 p 。对于训练数据集和测试数据集中特征为 x 1 , x 2 , , x p 的任一样本,我们使用如下的线性函数来生成该样本的标签:

y = 0.05 + p i = 1 0.01 x i + ϵ

其中噪声项 ϵ 服从均值为0、标准差为0.01的正态分布。为了较容易地观察过拟合,我们考虑高维线性回归问题,如设维度 p = 200 ;同时,我们特意把训练数据集的样本数设低,如20。

%matplotlib inline
import torch
import torch.nn as nn
import numpy as np
import sys
sys.path.append("..") 

n_train, n_test, num_inputs = 20, 100, 200
true_w, true_b = torch.ones(num_inputs, 1) * 0.01, 0.05

features = torch.randn((n_train + n_test, num_inputs))
labels = torch.matmul(features, true_w) + true_b
labels += torch.tensor(np.random.normal(0, 0.01, size=labels.size()), dtype=torch.float)
train_features, test_features = features[:n_train, :], features[n_train:, :]
train_labels, test_labels = labels[:n_train], labels[n_train:]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3. 从零开始实现

下面先介绍从零开始实现权重衰减的方法。我们通过在目标函数后添加 L 2 范数惩罚项来实现权重衰减。

3.1 初始化模型参数

首先,定义随机初始化模型参数的函数。该函数为每个参数都附上梯度。

def init_params():
    w = torch.randn((num_inputs, 1), requires_grad=True)
    b = torch.zeros(1, requires_grad=True)
    return [w, b]
  • 1
  • 2
  • 3
  • 4

3.2 定义 L 2 范数惩罚项

下面定义 L 2 范数惩罚项。这里只惩罚模型的权重参数。

def l2_penalty(w):
    return (w**2).sum() / 2
  • 1
  • 2

3.3 定义训练和测试

下面定义如何在训练数据集和测试数据集上分别训练和测试模型。与前面几节中不同的是,这里在计算最终的损失函数时添加了 L 2 范数惩罚项。

def linreg(X, w, b):
    return torch.mm(X, w) + b
    
def squared_loss(y_hat, y): 
    return ((y_hat - y.view(y_hat.size())) ** 2) / 2

def sgd(params, lr, batch_size):
    for param in params:
        param.data -= lr * param.grad / batch_size # 注意这里更改param时用的param.data

def semilogy(x_vals, y_vals, x_label, y_label, x2_vals=None, y2_vals=None,
             legend=None, figsize=(3.5, 2.5)):
    set_figsize(figsize)
    plt.xlabel(x_label)
    plt.ylabel(y_label)
    plt.semilogy(x_vals, y_vals)
    if x2_vals and y2_vals:
        plt.semilogy(x2_vals, y2_vals, linestyle=':')
        plt.legend(legend)
    plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
batch_size, num_epochs, lr = 1, 100, 0.003
net, loss = linreg, squared_loss

dataset = torch.utils.data.TensorDataset(train_features, train_labels)
train_iter = torch.utils.data.DataLoader(dataset, batch_size, shuffle=True)

def fit_and_plot(lambd):
    w, b = init_params()
    train_ls, test_ls = [], []
    for _ in range(num_epochs):
        for X, y in train_iter:
            # 添加了L2范数惩罚项
            l = loss(net(X, w, b), y) + lambd * l2_penalty(w)
            l = l.sum()
            
            if w.grad is not None:
                w.grad.data.zero_()
                b.grad.data.zero_()
            l.backward()
            d2l.sgd([w, b], lr, batch_size)
        train_ls.append(loss(net(train_features, w, b), train_labels).mean().item())
        test_ls.append(loss(net(test_features, w, b), test_labels).mean().item())
    d2l.semilogy(range(1, num_epochs + 1), train_ls, 'epochs', 'loss',
                 range(1, num_epochs + 1), test_ls, ['train', 'test'])
    print('L2 norm of w:', w.norm().item())
  • 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

3.4 观察过拟合

接下来,让我们训练并测试高维线性回归模型。当 lambd 设为0时,我们没有使用权重衰减。结果训练误差远小于测试集上的误差。这是典型的过拟合现象。

fit_and_plot(lambd=0)
  • 1

输出:

L2 norm of w: 15.114808082580566
  • 1

在这里插入图片描述

3.5 使用权重衰减

下面我们使用权重衰减。可以看出,训练误差虽然有所提高,但测试集上的误差有所下降。过拟合现象得到一定程度的缓解。另外,权重参数的 L 2 范数比不使用权重衰减时的更小,此时的权重参数更接近0。

fit_and_plot(lambd=3)
  • 1

输出:

L2 norm of w: 0.035220853984355927
  • 1

在这里插入图片描述

4. 简洁实现

这里我们直接在构造优化器实例时通过 weight_decay 参数来指定权重衰减超参数。默认下,PyTorch会对权重和偏差同时衰减。我们可以分别对权重和偏差构造优化器实例,从而只对权重衰减。

def fit_and_plot_pytorch(wd):
    # 对权重参数衰减。权重名称一般是以weight结尾
    net = nn.Linear(num_inputs, 1)
    nn.init.normal_(net.weight, mean=0, std=1)
    nn.init.normal_(net.bias, mean=0, std=1)
    optimizer_w = torch.optim.SGD(params=[net.weight], lr=lr, weight_decay=wd) # 对权重参数衰减
    optimizer_b = torch.optim.SGD(params=[net.bias], lr=lr)  # 不对偏差参数衰减
    
    train_ls, test_ls = [], []
    for _ in range(num_epochs):
        for X, y in train_iter:
            l = loss(net(X), y).mean()
            optimizer_w.zero_grad()
            optimizer_b.zero_grad()
            
            l.backward()
            
            # 对两个optimizer实例分别调用step函数,从而分别更新权重和偏差
            optimizer_w.step()
            optimizer_b.step()
        train_ls.append(loss(net(train_features), train_labels).mean().item())
        test_ls.append(loss(net(test_features), test_labels).mean().item())
    d2l.semilogy(range(1, num_epochs + 1), train_ls, 'epochs', 'loss',
                 range(1, num_epochs + 1), test_ls, ['train', 'test'])
    print('L2 norm of w:', net.weight.data.norm().item())

  • 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

与从零开始实现权重衰减的实验现象类似,使用权重衰减可以在一定程度上缓解过拟合问题。

fit_and_plot_pytorch(0)

  • 1
  • 2

输出:

L2 norm of w: 12.86785888671875

  • 1
  • 2

在这里插入图片描述

fit_and_plot_pytorch(3)

  • 1
  • 2

输出:

L2 norm of w: 0.09631537646055222
  • 1

在这里插入图片描述

小结

  • 正则化通过为模型损失函数添加惩罚项使学出的模型参数值较小,是应对过拟合的常用手段。
  • 权重衰减等价于 L 2 范数正则化,通常会使学到的权重参数的元素较接近0。
  • 权重衰减可以通过优化器中的 weight_decay 超参数来指定。
  • 可以定义多个优化器实例对不同的模型参数使用不同的迭代方法。



原文地址:访问原文地址
快照地址: 访问文章快照