pytorch: 学习笔记10, pytorch拟合多项式

pytorch拟合多项式 y=1.2x − 3.4x2 +5.6x3 + 5, 并画出原始曲线及拟合曲线

代码:

import torch
import numpy as np
from matplotlib import pyplot as plt


def semilogy_draw(x_vals, y_vals, x_label, y_label, x2_vals=None, y2_vals=None,
             legend=None, figsize=(3.5, 2.5)):
    # plt.figure(1, 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.savefig('Polynomial_fitting.png')
    plt.show()

def fit_and_plot(train_features, test_features, train_labels, test_labels):
    net = torch.nn.Linear(train_features.shape[-1], 1)

    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_draw(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)  # weight: tensor([[ 1.1994, -3.4017,  5.6002]])  bias: tensor([5.0018])
    return net.weight.data.numpy()[0], net.bias.data.numpy()  # net.weight.data.numpy()是二维的,只取一维数据


if __name__ == '__main__':
    # y=1.2x − 3.4x**2 +5.6x**3 + 5 +ϵ
    n_train, n_test, true_w, true_b = 100, 100, [1.2, -3.4, 5.6], 5
    features = torch.randn((n_train + n_test, 1))
    poly_features = torch.cat((features, torch.pow(features, 2), torch.pow(features, 3)), 1)
    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)

    print(features[:2])
    print(poly_features[:2])
    print(labels[:2])

    num_epochs, loss = 100, torch.nn.MSELoss()

    train_w, train_b = fit_and_plot(poly_features[:n_train, :], poly_features[n_train:, :], labels[:n_train], labels[n_train:])

    # 画出自定义的多项式函数(y=1.2x − 3.4x**2 +5.6x**3 + 5)的 groud truth 的曲线
    x_raw = np.arange(0, 10, 0.01)  # 生成0-10内的x坐标数据
    y_raw = true_w[0] * x_raw + true_w[1] * x_raw**2 + true_w[2] * x_raw**3 + true_b  # 以给定的多项式参数w, b 得到y
    y_train = train_w[0] * x_raw + train_w[1] * x_raw**2 + train_w[2] * x_raw**3 + true_b  # 以训练的的多项式参数w, b 得到y_train
    plt.subplot(1,2,1)  # 第一幅子图中, 画出原始真实数据多项式曲线
    plt.plot(x_raw, y_raw, 1, c = 'r')
    plt.legend(['raw'])
    plt.subplot(1, 2, 2)  # 第二幅子图中, 画出训练处的数据多项式曲线
    plt.plot(x_raw, y_train, 1, linestyle=':')
    plt.legend(['train'])
    plt.savefig('Polynomial_line.png')
    plt.show()
-------------------

结果:
tensor([[0.2492],
[0.5163]])
tensor([[0.2492, 0.0621, 0.0155],
[0.5163, 0.2666, 0.1377]])
tensor([5.1587, 5.4833])
final epoch: train loss 0.00011039545643143356 test loss 9.992923878598958e-05
weight: tensor([[ 1.2005, -3.4007, 5.5997]])
bias: tensor([5.0027])

对应epoch的损失loss变化:
在这里插入图片描述

多项式曲线:
在这里插入图片描述
参考:
https://tangshusen.me/Dive-into-DL-PyTorch/#/chapter03_DL-basics/3.11_underfit-overfit?id=_3114-%e5%a4%9a%e9%a1%b9%e5%bc%8f%e5%87%bd%e6%95%b0%e6%8b%9f%e5%90%88%e5%ae%9e%e9%aa%8c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值