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

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐