文字版说明:
Variable在 Pytorch0.4.0之后以及被tensor(requires_grad=True)所取代了.
The Variable API has been deprecated in Pytorch0.4.0: Variables are no longer necessary to use autograd with tensors. Autograd automatically supports Tensors with requires_grad
set to True
. Below please find a quick guide on what has changed:
-
Variable(tensor)
andVariable(tensor, requires_grad)
still work as expected, but they return Tensors instead of Variables. -
var.data
is the same thing astensor.data
. - Methods such as
var.backward(), var.detach(), var.register_hook()
now work on tensors with the same method names.
In addition, one can now create tensors with requires_grad=True
using factory methods such as torch.randn(), torch.zeros(), torch.ones(), and others like the following:
autograd_tensor = torch.randn((2, 3, 4), requires_grad=True)
You can also find some explanations about the difference between a tensor and a Variable in What is the difference between Tensors and Variables in Pytorch?
功能相同代码验证:(验证可以同样来使用)
Variable版本:
requires_grad=True
版本: