pytorch:多项式回归
生活随笔
收集整理的這篇文章主要介紹了
pytorch:多项式回归
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import numpy as np
import torch
from torch.autograd import Variable
from torch import nn, optim
import matplotlib.pyplot as plt# 設(shè)置字體為中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False# 構(gòu)造成次方矩陣
def make_fertures(x):x = x.unsqueeze(1)return torch.cat([x ** i for i in range(1, 4)], 1)# y = 0.9+0.5*x+3*x*x+2.4x*x*x
W_target = torch.FloatTensor([0.5, 3, 2.4]).unsqueeze(1)
b_target = torch.FloatTensor([0.9])# 計(jì)算x*w+b
def f(x):return x.mm(W_target) + b_target.item()def get_batch(batch_size=32):random = torch.randn(batch_size)random = np.sort(random)random = torch.Tensor(random)x = make_fertures(random)y = f(x)if (torch.cuda.is_available()):return Variable(x).cuda(), Variable(y).cuda()else:return Variable(x), Variable(y)# 多項(xiàng)式模型
class poly_model(nn.Module):def __init__(self):super(poly_model, self).__init__()self.poly = nn.Linear(3, 1) # 輸入時(shí)3維,輸出是1維def forward(self, x):out = self.poly(x)return outif torch.cuda.is_available():model = poly_model().cuda()
else:model = poly_model()
# 均方誤差,隨機(jī)梯度下降
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=1e-3)epoch = 0 # 統(tǒng)計(jì)訓(xùn)練次數(shù)
ctn = []
lo = []
while True:batch_x, batch_y = get_batch()output = model(batch_x)loss = criterion(output, batch_y)print_loss = loss.item()optimizer.zero_grad()loss.backward()optimizer.step()ctn.append(epoch)lo.append(print_loss)epoch += 1if (print_loss < 1e-3):breakprint("Loss: {:.6f} after {} batches".format(loss.item(), epoch))
print("==> Learned function: y = {:.2f} + {:.2f}*x + {:.2f}*x^2 + {:.2f}*x^3".format(model.poly.bias[0], model.poly.weight[0][0],model.poly.weight[0][1],model.poly.weight[0][2]))
print("==> Actual function: y = {:.2f} + {:.2f}*x + {:.2f}*x^2 + {:.2f}*x^3".format(b_target[0], W_target[0][0],W_target[1][0], W_target[2][0]))
# 1.可視化真實(shí)數(shù)據(jù)
predict = model(batch_x)
x = batch_x.numpy()[:, 0] # x~1 x~2 x~3
plt.plot(x, batch_y.numpy(), 'ro')
plt.title(label='可視化真實(shí)數(shù)據(jù)')
plt.show()
# 2.可視化擬合函數(shù)
predict = predict.data.numpy()
plt.plot(x, predict, 'b')
plt.plot(x, batch_y.numpy(), 'ro')
plt.title(label='可視化擬合函數(shù)')
plt.show()
# 3.可視化訓(xùn)練次數(shù)和損失
plt.plot(ctn,lo)
plt.xlabel('訓(xùn)練次數(shù)')
plt.ylabel('損失值')
plt.title(label='訓(xùn)練次數(shù)與損失關(guān)系')
plt.show()
實(shí)驗(yàn)結(jié)果:
注意:批量產(chǎn)生數(shù)據(jù)后,進(jìn)行一個(gè)排序,否則可視化時(shí),不是按照x軸從小到大繪制,出現(xiàn)很多折線。對應(yīng)代碼:
random = np.sort(random)random = torch.Tensor(random)?
總結(jié)
以上是生活随笔為你收集整理的pytorch:多项式回归的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pytorch:一维线性回归(一)
- 下一篇: pytorch:Logistic回归