多项式拟合实验,进行模型复杂度和过拟合欠拟合的关系

Posted tacit-lxs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多项式拟合实验,进行模型复杂度和过拟合欠拟合的关系相关的知识,希望对你有一定的参考价值。

多项式拟合实验,进行模型复杂度和过拟合、欠拟合的关系

%matplotlib inline
import torch
import numpy as np
from IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random
#生成数据
n_train, n_test, true_w, true_b = 100, 100, [1.2, -3.4, 5.6], 5 #训练集和测试集均为100,拟合多项式y = 1.2x - 3.4x^2 + 5.6x^3 + 5
features = torch.randn((n_train + n_test, 1))
poly_features = torch.cat((features, torch.pow(features, 2), torch.pow(features, 3)), 1) #x、x^2、x^3
labels = (true_w[0] * poly_features[:, 0] + true_w[1] * poly_features[:, 1] + true_w[2] * poly_features[:, 2] + true_b) # 生成标签
labels += torch.tensor(np.random.normal(0, 0.01, size=labels.size()), dtype=torch.float) # 给标签加上噪声
def set_figsize(figsize=(3.5, 2.5)):
    use_svg_display()
    # 设置图的尺寸
    plt.rcParams['figure.figsize'] = figsize

def use_svg_display():
    """Use svg format to display plot in jupyter"""
    display.set_matplotlib_formats('svg')
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)
num_epochs, loss = 100, torch.nn.MSELoss()

def fit_and_plot(train_features, test_features, train_labels, test_labels):
    net = torch.nn.Linear(train_features.shape[-1], 1)
    # 通过Linear文档可知,pytorch已经将参数初始化了,所以我们这里就不手动初始化了
    
    batch_size = min(10, train_labels.shape[0])    
    dataset = torch.utils.data.TensorDataset(train_features, train_labels)
    train_iter = torch.utils.data.DataLoader(dataset, batch_size, shuffle=True)
    
    optimizer = torch.optim.SGD(net.parameters(), lr=0.01)
    train_ls, test_ls = [], []
    for _ in range(num_epochs):
        for X, y in train_iter:
            l = loss(net(X), y.view(-1, 1))
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
        train_labels = train_labels.view(-1, 1)
        test_labels = test_labels.view(-1, 1)
        train_ls.append(loss(net(train_features), train_labels).item())
        test_ls.append(loss(net(test_features), test_labels).item())
    print('final epoch: train loss', train_ls[-1], 'test loss', test_ls[-1])
    semilogy(range(1, num_epochs + 1), train_ls, 'epochs', 'loss',
             range(1, num_epochs + 1), test_ls, ['train', 'test'])
    print('weight:', net.weight.data,
          '\\nbias:', net.bias.data)

fit_and_plot(poly_features[:n_train, :], poly_features[n_train:, :], 
            labels[:n_train], labels[n_train:]) #三阶拟合

fit_and_plot(features[:n_train, :], features[n_train:, :], labels[:n_train],
             labels[n_train:]) # 线性拟合 产生欠拟合,模型复杂度不够

fit_and_plot(poly_features[0:2, :], poly_features[n_train:, :], labels[0:2],
             labels[n_train:]) # 训练样本不足的三阶拟合,产生过拟合

以上是关于多项式拟合实验,进行模型复杂度和过拟合欠拟合的关系的主要内容,如果未能解决你的问题,请参考以下文章

模型选择欠拟合和过拟合(underfit-overfit)

动手深度学习12- 模型选择,欠拟合和过拟合

神经网络:欠拟合和过拟合

三欠拟合和过拟合

欠拟合和过拟合的一般解决方法

机器学习欠拟合和过拟合