首页 > 其他 > 详细

02-pytorch

时间:2019-07-07 12:17:45      阅读:123      评论:0      收藏:0      [点我收藏+]
import torch
from pprint import pprint
from torch.autograd import Variable

变成图纸中的一个节点

tensor = torch.FloatTensor([[1,2],[3,4]])
variable = Variable(tensor,requires_grad=True)
pprint(tensor)
pprint(variable)
tensor([[1., 2.],
        [3., 4.]])
tensor([[1., 2.],
        [3., 4.]], requires_grad=True)

反向传播误差

t_out = torch.mean(tensor*tensor)  # 求x^2的平均值
v_out = torch.mean(variable*variable)
# v_out = 1/4 *sum(var*var) 
v_out.backward()  # 反向传播误差
# d(v_out)/d(var)    1/4*2*variable = variable/2

print(variable.grad)
tensor([[0.5000, 1.0000],
        [1.5000, 2.0000]])
# variable.data 才是 tensor 的形式
print(variable.data)
tensor([[1., 2.],
        [3., 4.]])

02-pytorch

原文:https://www.cnblogs.com/liu247/p/11145511.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!