torch.Tensor.is_leaf
- Tensor.is_leaf
-
所有
requires_grad
属性为False
的张量将按惯例视为叶张量。对于具有
requires_grad
属性且值为True
的张量,如果它们是由用户创建的,则这些张量将被视为叶张量。这意味着它们不是某个操作的结果,因此grad_fn
为None。只有叶子 Tensor 在调用
backward()
时才会填充其grad
属性。为了使非叶子 Tensor 的grad
属性被填充,你可以使用retain_grad()
。示例:
>>> a = torch.rand(10, requires_grad=True) >>> a.is_leaf True >>> b = torch.rand(10, requires_grad=True).cuda() >>> b.is_leaf False # b was created by the operation that cast a cpu Tensor into a cuda Tensor >>> c = torch.rand(10, requires_grad=True) + 2 >>> c.is_leaf False # c was created by the addition operation >>> d = torch.rand(10).cuda() >>> d.is_leaf True # d does not require gradients and so has no operation creating it (that is tracked by the autograd engine) >>> e = torch.rand(10).cuda().requires_grad_() >>> e.is_leaf True # e requires gradients and has no operations creating it >>> f = torch.rand(10, requires_grad=True, device="cuda") >>> f.is_leaf True # f requires grad, has no operation creating it