线性回归(PyTorch)

运行环境为:
Ubuntu16.04 Python2.7 PyTorch-cpu版本

pip install torch1.3.1+cpu torchvision0.4.2+cpu -f https://download.pytorch.org/whl/torch_stable.html

PyTorch 自动求导机制
实例测试

# -*- coding:utf-8 -*-
import torch as t
from torch.autograd import Variable as V
from matplotlib import pyplot as plt
from IPython import display

t.manual_seed(1000)
def get_fake_data(batch_size=8):
    x=t.rand(batch_size,1)*20
    y=x*2+(1+t.randn(batch_size,1))*3
    return x,y

    #print(y)
x,y=get_fake_data()
plt.scatter(x.squeeze().numpy(),y.squeeze().numpy())
#plt.show()

#随机初始化参数
w=V(t.rand(1,1),requires_grad=True)
b=V(t.zeros(1,1),requires_grad=True)
lr=0.001

for ii in range(8000):
    x,y=get_fake_data()
    x,y=V(x),V(y)
    #forward:计算loss
    y_pred=x.mm(w)+b.expand_as(y)
    loss=0.5*(y_pred-y)**2
    loss=loss.sum()

    #backward: 自动计算梯度
    loss.backward()

    #更新参数
    w.data.sub_(lr*w.grad.data)
    b.data.sub_(lr*b.grad.data)
    
    #梯度清零
    w.grad.data.zero_()
    b.grad.data.zero_()

    if ii%1000==0:
        #画图
        display.clear_output(wait=True)
        x=t.arange(0,20).view(-1,1)
        y=x.mm(w.data)+b.data.expand_as(x)
       	
        plt.plot(x.numpy(),y.numpy())

        x2,y2=get_fake_data(batch_size=20)
        plt.scatter(x2.numpy(),y2.numpy())

        plt.xlim(0,20)
        plt.ylim(0,41)
        plt.show()
        plt.pause(0.5)

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


源代码有一个报错:
File “variable_linear_regression.py”, line 47, in
y=x.mm(w.data)+b.data.expand_as(x)
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 ‘mat2’ in call to _th_mm

修改更正为:

y=x.mm(w.data.type('torch.LongTensor'))+b.data.expand_as(x)

小试牛刀,亲测可用。理解PyTorch中,autograd模块中,反向传播中的自动求导。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值