PyTorch学习笔记-04

PyTorch简单的demo-线性回归

Posted by hmoytx on May 8, 2018

简介

线性回归是机器学习的一个重要的内容,应用十分广泛。线性回归利用数理统计中的回归分析来确定两种或两种以上变量间相互依赖的定量关系,表达形式是一次式y= wx + b ,损失函数是误差平方的一般。利用随机梯度下降法更新参数w和b来最小化损失函数,最终学得w和b。

具体代码


import torch as t
from torch.autograd import Variable as V
from matplotlib import pyplot as plt


t.manual_seed(100)

def get_data(batch_size = 8):
    x = t.rand(batch_size, 1) * 20
    y = x * 3 + (1 + t.randn(batch_size, 1)) * 5
    return x, y



w = V(t.rand(1,1), requires_grad=True)
b = V(t.rand(1,1), requires_grad=True)

lr = 0.001 #learning rate

for i in range(10000):
    x, y = get_data()
    x, y = V(x), V(y)

    y_pre = x.mm(w) + b.expand_as(y)
    loss = 0.5 * (y_pre - y) ** 2
    loss = loss.sum()

    loss.backward()

    w.data.sub_(lr * w.grad.data)
    b.data.sub_(lr * b.grad.data)

    w.grad.data.zero_()
    b.grad.data.zero_()

print(w.data.squeeze()[0], b.data.squeeze()[0])

x = t.arange(0,20).view(-1, 1)
y = x.mm(w.data) + b.data.expand_as(x)

plt.plot(x.numpy(), y.numpy())

x1, y1 = get_data(20)

plt.scatter(x1.numpy(), y1.numpy())
plt.show()

运行结果如下图 result

代码文件

github文件