PyTorch0.4.0之后Variable 已经被PyTroch弃用

Variable不再是张量使用autograd的必要条件

只需要将张量的requires_grad设为True该张量就会自动支持autograd运算

在新版的PyTorch中Variable(tensor)和Varialbe(tensor, requires_grad)还能继续使用,但是返回的是tensor变量,而不是Variable变量

使用Variable.data的效果和使用tensor.data一样

诸如Variable.backward(),Variable.detach(),Varialbe.register_hook()等方法直接移植到了tensor上,在tensor上可以直接使用同名的函数

此外,现在可以直接使用torch.randn(),torch.zeros(),torch.ones()等工厂方法直接用requires_gard=True属性生成tensor,例如

autograd_tensor = torch.randn((2, 3, 4), requires_grad=True)

参考:
https://pytorch.org/docs/stable/autograd.html

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐