多項式回歸
import torch
import numpy
defmake_features
(x):
'''獲取 [x, x^2, x^3]...的矩陣'''
x = x.unsqueeze(1)
#將一維資料變為(n,1)二維矩陣形式
return torch.cat(
[x ** i for i in
range(1
,4)]
,1)#按列拼接
deff
(x):
w_target = torch.tensor(
[0.5,3
.,2.4]
).unsqueeze(1)
b_target = torch.tensor(
[0.9])
return x.mm(w_target)
+ b_target # 表示式:f(x) = x * w_target + b_target
batch_size=
32random = torch.randn(batch_size)
defget_batch
(batch_size=32)
:''' 獲取32個資料對:(x, f(x)) '''
# random = torch.randn(batch_size)
x = make_features(random)
y = f(x)
return torch.autograd.variable(x)
, torch.autograd.variable(y)
class
poly_model
(torch.nn.module)
:''' 定義多項式模型 '''
def__init__
(self)
:super
(poly_model, self)
.__init__(
) self.poly = torch.nn.linear(3,
1)#輸入3維[x, x^2, x^3],輸出1維y
defforward
(self, x)
: out = self.poly(x)
return out
model = poly_model(
)criterion = torch.nn.mseloss(
)optimizer = torch.optim.sgd(model.parameters(
), lr=1e-
3)epoch =
0#get data
batch_x, batch_y = get_batch(
)while
true
:#forward
out = model(batch_x)
loss = criterion(out, batch_y)
print_loss = loss.data
optimizer.zero_grad(
) loss.backward(
) optimizer.step(
) epoch +=
1if print_loss <1e-
2:print
('epoch:'
,epoch)
break
print
(list
(model.parameters())
)#列印最後學習到的引數w, b
學習結果如下:
epoch:
1179
[parameter containing:
tensor([[
0.5059
,3.0444
,2.3960]]
, requires_grad=true)
, parameter containing:
tensor(
[0.7748
], requires_grad=true)
]
繪製曲線:
多項式回歸
import numpy as np import matplotlib.pyplot as plt x np.random.uniform 3,3,size 100 x x.reshape 1,1 y 0.5 x 2 x 2 np.random.normal 0,1,100 plt.scatter...
多項式回歸
多項式回歸 import torch import numpy defmake features x 獲取 x,x 2,x 3 的矩陣 x x.unsqueeze 1 將一維資料變為 n,1 二維矩陣形式 return torch.cat x i for i in range 1 4 1 按列拼接 ...
多項式回歸
線性回歸適用於資料成線性分布的回歸問題,如果樣本是非線性分布,線性回歸就不再使用,轉而可以採用非線性模型進行回歸,比如多項式回歸 多項式回歸模型定義 與線性模型,多項式模型引入了高次項 y w 0 w1 x w2 x2 w 3x3 wnxn y w 0 w 1x w 2x 2 w 3x 3 w nx...