torch.autograd.variable

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

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

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

在新版的PyTorchVariable(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,例如

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

参考

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


torch.autograd.variable
http://yojayc.github.io/2021/08/03/torch-autograd-variable/
作者
Truman
发布于
2021年8月3日
更新于
2026年4月5日
许可协议