torch.Tensor.is_leaf
-
Tensor.is_leaf -
Все тензоры, у которых
requires_gradравноFalseпо умолчанию являются листовыми тензорами.Для тензоров, у которых
requires_gradравноTrue, они будут листовыми тензорами, если были созданы пользователем. Это означает, что они не являются результатом операции, и поэтомуgrad_fnравно None.Только листовые тензоры будут иметь заполненное значение
gradво время вызоваbackward(). Чтобы получить заполненное значение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
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/1.13/generated/torch.Tensor.is_leaf.html