Spec-Zone.ru › PyTorch 1

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

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API