Pytorch学习笔记

非线性回归问题的参数求解,反向求导基本流程。Variable 计算时, 它在后台一步步默默地搭建着一个庞大的系统, 叫做计算图, computational graph. 这个图将所有的计算步骤 (节点) 都连接起来, 最后进行误差反向传递的时候, 一次性将所有 variable 里面的修改幅度 (梯度) 都计算出来, 而 tensor 就没有这个能力。

 1 from torch.autograd import Variable
 2 dtype = torch.FloatTensor
 3 N, D_in, H, D_out = 64, 1000, 100, 10
 4 x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False)
 5 y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False)
 6 w1 = Variable(torch.randn(D_in, H).type(dtype), requires_grad=True)
 7 w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True)
 8 learning_rate = 1e-6
 9 for t in range(500):
10     y_pred = x.mm(w1).clamp(min=0).mm(w2)
11     loss = (y_pred - y).pow(2).sum()
12     print(t, loss.data[0])
13     # Manually zero the gradients before running the backward pass
14     if t > 0:
15         w1.grad.data.zero_()
16         w2.grad.data.zero_()
17     loss.backward()
18     w1.data -= learning_rate * w1.grad.data
19     w2.data -= learning_rate * w2.grad.data

 

转载于:https://www.cnblogs.com/demian/p/8012035.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值