多项式回归

多项式回归

import torch
import numpy

def make_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)  #按列拼接

def f(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=32
random = torch.randn(batch_size)
def get_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

    def forward(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 += 1
    if 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 matplotlib.pyplot as plt

model.eval()
predict = model(torch.autograd.Variable(batch_x))
predict = predict.data.numpy()

plt.plot(sorted(random), sorted(batch_y.numpy()), 'ro', label='real curve')
plt.plot(sorted(random), sorted(predict.flatten()), label= 'Fitting curve')
plt.legend()
plt.show()

image-20201005130633508

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

[小G]

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值