當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python PyTorch Tensor.is_leaf用法及代碼示例


本文簡要介紹python語言中 torch.Tensor.is_leaf 的用法。

用法:

Tensor.is_leaf

按照慣例,所有具有 requires_grad False 的張量都將是葉張量。

對於具有 requires_grad True 的張量,如果它們是由用戶創建的,它們將是葉張量。這意味著它們不是操作的結果,因此grad_fn 為無。

在調用 backward() 期間,隻有葉張量的 grad 才會被填充。要為非葉張量填充 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

相關用法


注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.Tensor.is_leaf。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。