1.介紹
線性回歸是利用數(shù)理統(tǒng)計(jì)中回歸分析推汽,來(lái)確定兩種或兩種以上變量間相互依賴的定量關(guān)系的一種統(tǒng)計(jì)分析方法,運(yùn)用十分廣泛。其表達(dá)形式為y = w'x+e猬错,e為誤差服從均值為0的正態(tài)分布。
回歸分析中茸歧,只包括一個(gè)自變量和一個(gè)因變量倦炒,且二者的關(guān)系可用一條直線近似表示,這種回歸分析稱為一元線性回歸分析软瞎。如果回歸分析中包括兩個(gè)或兩個(gè)以上的自變量逢唤,且因變量和自變量之間是線性關(guān)系拉讯,則稱為多元線性回歸分析。
2.模型訓(xùn)練
# -*- coding: utf-8 -*-
import torch
from torch import nn, optim
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt
num_epoches = 10000
learning_rate = 1e-3
class LinearRegression(nn.Module):
"""線性回歸模型定義"""
def __init__(self):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(1, 1)
def forward(self, x):
# 前向傳播
output = self.linear(x)
return output
# 模型初始化
model = LinearRegression()
# 定義loss和優(yōu)化函數(shù)
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=learning_rate)
# 輸入數(shù)據(jù)
x_train = np.array([1, 2, 3, 4, 5], dtype=np.float32).reshape(-1, 1)
y_train = np.array([2, 4, 6, 8, 10], dtype=np.float32).reshape(-1, 1)
# 將np.array轉(zhuǎn)換成Tensor
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)
# 模型訓(xùn)練
for epoch in range(num_epoches):
inputs = Variable(x_train)
target = Variable(y_train)
# forward
out = model(inputs)
loss = criterion(out, target)
# backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
# print loss
if (epoch+1) % 100 == 0:
print('Epoch[{}/{}], loss: {:.6f}'.format(epoch+1, num_epoches, loss.item()))
# 模型保存
torch.save(model.state_dict(), './Linear_Regression.model')
# 模型評(píng)估
model.eval()
predict = model(Variable(x_train))
predict = predict.data.numpy()
# 畫圖
plt.plot(x_train.numpy(), y_train.numpy(), 'ro', label='Original data')
plt.plot(x_train.numpy(), predict, label='Fitting Line')
plt.legend()
plt.show()