Linear Regression with PyTorch

Posted fengyubo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linear Regression with PyTorch相关的知识,希望对你有一定的参考价值。

Linear Regression with PyTorch

Problem Description

初始化一组数据 ((x,y)),使其满足这样的线性关系 (y = w x + b) 。然后基于反向传播法,用均方误差(mean squared error)去拟合这组数据。

Notice

self.prediction = torch.nn.Linear(1, 1)

这一行代码,实际是维护了两个变量,其描述了这样的一种关系:

[prediction_{1 imes1} = weight_{1 imes1} imes input_{1 imes1} + bias_{1 imes1}]

其中,每个参数都是 (1 imes1) 维的。

Code

import torch


epoch = 10000
lr = 0.01
w = 10
b = 5

x = torch.unsqueeze(torch.linspace(1, 10, 20), 1)
y = w*x + b + torch.rand(x.size())


class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.prediction = torch.nn.Linear(1, 1)

    def forward(self, x):
        out = self.prediction(x)
        return out


net = Net()
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
criticism = torch.nn.MSELoss()


for i in range(epoch):
    y_pred = net(x)
    loss = criticism(y_pred, y)  # 先是 y_pred 然后是 y_true 参数顺序不能乱

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

print(loss.data)
print(net.state_dict()[‘prediction.weight‘])
print(net.state_dict()[‘prediction.bias‘])

输出:

tensor(1.00000e-07 *
       5.3597)
tensor([[ 1.2002]])
tensor([ 0.9984])

以上是关于Linear Regression with PyTorch的主要内容,如果未能解决你的问题,请参考以下文章

Linear Regression with PyTorch

Coursera《machine learning》--单变量线性回归(Linear Regression with One Variable)

Linear regression with one variable - Model representation

机器学习笔记-1 Linear Regression with Multiple Variables(week 2)

Coursera - machine learning Linear regression with one variable-quiz

#Week2 Linear Regression with One Variable