pytorch實(shí)現(xiàn)多項(xiàng)式回歸,供大家參考,具體內(nèi)容如下
一元線性回歸模型雖然能擬合出一條直線,但精度依然欠佳,擬合的直線并不能穿過(guò)每個(gè)點(diǎn),對(duì)于復(fù)雜的擬合任務(wù)需要多項(xiàng)式回歸擬合,提高精度。多項(xiàng)式回歸擬合就是將特征的次數(shù)提高,線性回歸的次數(shù)使一次的,實(shí)際我們可以使用二次、三次、四次甚至更高的次數(shù)進(jìn)行擬合。由于模型的復(fù)雜度增加會(huì)帶來(lái)過(guò)擬合的風(fēng)險(xiǎn),因此需要采取正則化損失的方式減少過(guò)擬合,提高模型泛化能力。希望大家可以自己動(dòng)手,通過(guò)一些小的訓(xùn)練掌握pytorch(案例中有些觀察數(shù)據(jù)格式的代碼,大家可以自己注釋掉)
# 相較于一元線性回歸模型,多項(xiàng)式回歸可以很好的提高擬合精度,但要注意過(guò)擬合風(fēng)險(xiǎn) # 多項(xiàng)式回歸方程 f(x) = -1.13x-2.14x^2+3.12x^3-0.01x^4+0.512 import torch import matplotlib.pyplot as plt import numpy as np # 數(shù)據(jù)準(zhǔn)備(測(cè)試數(shù)據(jù)) x = torch.linspace(-2,2,50) print(x.shape) y = -1.13*x - 2.14*torch.pow(x,2) + 3.15*torch.pow(x,3) - 0.01*torch.pow(x,4) + 0.512 plt.scatter(x.data.numpy(),y.data.numpy()) plt.show() # 此時(shí)輸入維度為4維 # 為了拼接輸入數(shù)據(jù),需要編寫輔助數(shù)據(jù),輸入標(biāo)量x,使其變?yōu)榫仃?,使用torch.cat拼接 def features(x): # 生成矩陣 # [x,x^2,x^3,x^4] x = x.unsqueeze(1) print(x.shape) return torch.cat([x ** i for i in range(1,5)], 1) result = features(x) print(result.shape) # 目標(biāo)公式用于計(jì)算輸入特征對(duì)應(yīng)的標(biāo)準(zhǔn)輸出 # 目標(biāo)公式的權(quán)重如下 x_weight = torch.Tensor([-1.13,-2.14,3.15,-0.01]).unsqueeze(1) b = torch.Tensor([0.512]) # 得到x數(shù)據(jù)對(duì)應(yīng)的標(biāo)準(zhǔn)輸出 def target(x): return x.mm(x_weight) + b.item() # 新建一個(gè)隨機(jī)生成輸入數(shù)據(jù)和輸出數(shù)據(jù)的函數(shù),用于生成訓(xùn)練數(shù)據(jù) def get_batch_data(batch_size): # 生成batch_size個(gè)隨機(jī)的x batch_x = torch.randn(batch_size) # 對(duì)于每個(gè)x要生成一個(gè)矩陣 features_x = features(batch_x) target_y = target(features_x) return features_x,target_y # 創(chuàng)建模型 class PolynomialRegression(torch.nn.Module): def __init__(self): super(PolynomialRegression, self).__init__() # 輸入四維度 輸出一維度 self.poly = torch.nn.Linear(4,1) def forward(self, x): return self.poly(x) # 開始訓(xùn)練模型 epochs = 10000 batch_size = 32 model = PolynomialRegression() criterion = torch.nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(),0.001) for epoch in range(epochs): print("{}/{}".format(epoch+1,epochs)) batch_x,batch_y = get_batch_data(batch_size) out = model(batch_x) loss = criterion(out,batch_y) optimizer.zero_grad() loss.backward() # 更新梯度 optimizer.step() if (epoch % 100 == 0): print("Epoch:[{}/{}],loss:{:.6f}".format(epoch,epochs,loss.item())) if (epoch % 1000 == 0): predict = model(features(x)) print(x.shape) print(predict.shape) print(predict.squeeze(1).shape) plt.plot(x.data.numpy(),predict.squeeze(1).data.numpy(),"r") loss = criterion(predict,y) plt.title("Loss:{:.4f}".format(loss.item())) plt.xlabel("X") plt.ylabel("Y") plt.scatter(x,y) plt.show()
擬合結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:駐馬店 衡水 股票 江蘇 呼和浩特 湖州 畢節(jié) 中山
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytorch實(shí)現(xiàn)多項(xiàng)式回歸》,本文關(guān)鍵詞 pytorch,實(shí)現(xiàn),多項(xiàng)式,回歸,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。