MENU

深度学习Pytorch学习笔记(2)-实现多项式回归

• April 1, 2022 • Read: 9490 • Python,深度学习

数据准备

import numpy as np
import matplotlib.pyplot as plt
import torch as tr
x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168], [9.779], [6.182], [7.59], [2.137], [7.042], [10.791], [5.313], [7.997], [3.1]], dtype=np.float32)
y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573], [3.366], [2.596], [2.53], [1.221], [2.827], [3.465], [1.65], [2.904], [1.3]], dtype=np.float32)

if tr.cuda.is_available():
    device = 'cuda:0'
else:
    device = 'cpu'

device
'cuda:0'
x_data = tr.from_numpy(x_train).to(device)
y_data = tr.from_numpy(y_train).to(device)
fig, ax = plt.subplots()
ax.scatter(x_data.to('cpu').numpy(), y_data.to('cpu').numpy(), color='tab:red')
ax.set_xlabel("x_train")
ax.set_ylabel("y_train")
plt.show()


png

构建ANN

class MyModel1d(tr.nn.Module):
    def __init__(self):
        super(MyModel1d, self).__init__()
        self.linear = tr.nn.Linear(1, 1)

    def forward(self, x):
        x = self.linear(x)
        return x

model1d = MyModel1d().to(device)

损失函数和优化器

criterion = tr.nn.MSELoss(reduction='mean')
optimizer = tr.optim.SGD(model1d.parameters(), lr=1e-3)

训练

loss_value = []
for epoch in range(10000):
    y_pred = model1d(x_data).to(device)
    loss = criterion(y_pred, y_data).to(device)
    loss_value.append(loss.item())
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

fig, ax = plt.subplots(figsize=(20, 5))
ax.plot(np.arange(len(loss_value)), loss_value)
ax.set_xlabel("Epoch")
ax.set_ylabel("Loss")
plt.show()


png

print("w = ", model1d.linear.weight.item())
print("b = ", model1d.linear.bias.item())
print("拟合函数表达式:", ":%f * x + :%f" % (model1d.linear.weight.item(), model1d.linear.bias.item()))
w =  0.2663855254650116
b =  0.7033794522285461
拟合函数表达式: :0.266386 * x + :0.703379

结果检验

x_test = tr.from_numpy(np.linspace(2, 11, 50, dtype=np.float32).reshape(-1, 1)).to(device)
y_test = model1d(x_test)
fig, ax = plt.subplots()
ax.plot(x_test.to('cpu').numpy(),y_test.to('cpu').data.numpy(), label='fitting line')
ax.scatter(x_data.to('cpu').numpy(), y_data.to('cpu').numpy(), color='tab:red', label='original data')
ax.legend(loc='best')
ax.set_xlabel("x_train")
ax.set_ylabel("y_train")


png

非线性多项式拟合

线性拟合的函数是一个一次函数,即一元一次函数即可表达。如果需要更高阶的多项式,如二次、三次等等时,计算的思路基本相同。若要拟合的多项式类型为:

$$ y = b + w_1 x + w_2 x^2 + w_3 x^3 $$

从数学角度来看,输入量即自变量仍然只有一个,即$x$。但是从神经网络的角度来看,可以将其看成三个输入量,即将$x$的不同阶次均当作一个单独的输入量,即认为:

$$ x_1 = x\\ x_2 = x^2\\ x_3 = x^3 $$

由此可知,神经网络具有三个输入,但是输出仍然只有一个,依据相同的思路构建NN,并进行训练即可。

数据准备

若原始的数据或者函数为三次多项式

$$ y = 0.9 + 0.5 x + 3 x^2 + 2.4 x^3 $$

利用原始的函数生成数据,并添加一些噪声进行扰动,构建出训练需要的数据

"""原始数据的生成"""

def func0(x):
    sol = 0.9 + 0.5 * x + 3 * x**2 + 2.4 * x**3
    return sol
x0 = np.linspace(-5, 5, 100)
y0 = func0(x0)

fig, ax = plt.subplots()
ax.plot(x0, y0, color='k', linewidth=2)
ax.grid(True, ls=':')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title("Originnal Function Curve")


png

'''对原始数据点添加噪声'''
np.random.seed(1025)
y_noise = np.random.normal(size=y0.shape[0]) * 50 + y0
y_noise = np.float32(y_noise)

fig, ax = plt.subplots()
ax.plot(x0, y0, color='k', linewidth=2, label='Original Function Curve')
ax.scatter(x0, y_noise, color='tab:red', s=20, label='Points Data')
ax.legend(loc='best')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(ls=':')


png

x1 = x0
x2 = x0**2
x3 = x0**3
x123 = np.float32(np.c_[x1, x2, x3])
x_data2 = tr.from_numpy(x123).to(device)
y_data2 = tr.from_numpy(y_noise.reshape(-1, 1)).to(device)

构建神经网络

class NonlinearModel(tr.nn.Module):
    def __init__(self):
        super(NonlinearModel, self).__init__()
        self.linear = tr.nn.Linear(in_features=3, out_features=1)

    def forward(self, x):
        x = self.linear(x)
        return x

model2 = NonlinearModel().to(device)
criterion2 = tr.nn.MSELoss(reduction='mean')
optimizer2 = tr.optim.SGD(model2.parameters(), lr=0.00001)

loss_value2 = []
for epoch in range(1000):
    y_pred2 = model2(x_data2).to(device)
    loss2 = criterion2(y_pred2, y_data2).to(device)
    loss_value2.append(loss2.item())
    optimizer2.zero_grad()
    loss2.backward()
    optimizer2.step()
print(model2.linear.weight.data)
print(model2.linear.bias.data)
tensor([[0.3810, 2.9940, 2.3216]], device='cuda:0')
tensor([0.6980], device='cuda:0')
plt.plot(loss_value2)

png

plt.plot(x0, model2(x_data2).to('cpu').data.numpy())
plt.plot(x0, y0)


png

Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment

24 Comments
  1. 探究传奇私服服务器承载极限:最多能开多少服?:https://501h.com/fugu/2024-09-29/38693.html

  2. 《里希神探》欧美剧高清在线免费观看:https://www.jgz518.com/xingkong/17287.html

  3. 《蝙蝠侠:黑暗骑士崛起》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/26987.html

  4. 真棒!

  5. 你的才华让人瞩目,期待你的更多文章。 http://www.55baobei.com/d2zOiLTyEX.html

  6. 你的文章让我感受到了艺术的魅力,谢谢! http://www.55baobei.com/wm3xQC4AwK.html

  7. 《银线号大血案》动作片高清在线免费观看:https://www.jgz518.com/xingkong/96936.html

  8. 《麻辣女生》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/4359.html

  9. 你的文章内容非常专业,让人佩服。 http://www.55baobei.com/jGEGz7tcdC.html

  10. 《水啸雾都》动作片高清在线免费观看:https://www.jgz518.com/xingkong/31973.html

  11. 新盘新项目,不再等待,现在就是最佳上车机会!

  12. 新车首发,新的一年,只带想赚米的人

  13. 2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

  14. 华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099

  15. 华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099
    华纳公司合作开户所需材料?电话号码15587291507 微信STS5099

  16. 华纳东方明珠客服电话是多少?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠开户专线联系方式?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    如何联系华纳东方明珠客服?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠官方客服联系方式?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠客服热线?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠开户客服电话?(▲182(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠24小时客服电话?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠客服邮箱?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠官方客服在线咨询?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠客服微信?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】

  17. 华纳东方明珠客服电话是多少?(??155--8729--1507?《?薇-STS5099】【?扣6011643?】
    华纳东方明珠开户专线联系方式?(??155--8729--1507?《?薇-STS5099】【?扣6011643?】

  18. 新盛开户前客服电话咨询材料清单【1558--7291-507薇同1】
    新盛开户官方客服电话 【1558--7291-507 薇同1】
    新盛开户业务办理电话 【1558--7291-507 薇同1】
    新盛开户联系电话大全 【1558--7291-507 薇同1】

  19. 新盛开户前客服电话咨询材料清单【1558--7291-507薇同1】
    新盛开户官方客服电话 【1558--7291-507 薇同1】
    新盛开户业务办理电话 【1558--7291-507 薇同1】
    新盛开户联系电话大全 【1558--7291-507 薇同1】

  20. 新盛开户前客服电话咨询材料清单【1558--7291-507薇同1】
    新盛开户官方客服电话 【1558--7291-507 薇同1】
    新盛开户业务办理电话 【1558--7291-507 薇同1】
    新盛开户联系电话大全 【1558--7291-507 薇同1】

  21. 华纳客服开户流程,华纳客服开户流程图(?183-8890--9465—《?薇-STS5099】

  22. 华纳万宝路客服电话是多少?(?183-8890-9465—《?薇-STS5099】【?扣6011643??】
    华纳万宝路开户专线联系方式?(?183-8890--9465—《?薇-STS5099】【?扣6011643??】
    华纳圣淘沙客服开户电话全攻略,让娱乐更顺畅!(?183-8890--9465—《?薇-STS5099】客服开户流程,华纳圣淘沙客服开户流程图(?183-8890--9465—《?薇-STS5099】

  23. 华纳万宝路客服电话是多少?(?183-8890-9465—《?薇-STS5099】【?扣6011643??】
    华纳万宝路开户专线联系方式?(?183-8890--9465—《?薇-STS5099】【?扣6011643??】
    华纳圣淘沙客服开户电话全攻略,让娱乐更顺畅!(?183-8890--9465—《?薇-STS5099】客服开户流程,华纳圣淘沙客服开户流程图(?183-8890--9465—《?薇-STS5099】

  24. 新盛客服电话是多少?(?183-8890-9465—《?薇-STS5099】【
    新盛开户专线联系方式?(?183-8890--9465—《?薇-STS5099】【?扣6011643??】
    新盛客服开户电话全攻略,让娱乐更顺畅!(?183-8890--9465—《?薇-STS5099】客服开户流程,华纳新盛客服开户流程图(?183-8890--9465—《?薇-STS5099】