Pytorch is

编程入门 行业动态 更新时间:2024-10-11 03:19:04

<a href=https://www.elefans.com/category/jswz/34/1769961.html style=Pytorch is"/>

Pytorch is

转载更正自Pytorch框架之is_leaf–查看是否为叶张量

  • 所有被需要计算梯度的变量直接赋值(不进行任何操作)创建的都是叶张量,注意不要包含任何操作
a = torch.rand(10, requires_grad=True)  # 直接赋给a,所以a.is_leaf为True
a = torch.rand(10, requires_grad=True, device="cuda") # 直接创建赋给a的,所以为True
a = torch.rand(10, requires_grad=True) + 5  # 运算后赋给a,所以a.is_leaf为False
# .cuda()也是一个op节点
a = torch.rand(10, requires_grad=True).cuda()  # 将数据移到gpu上再赋值给a,所以也是False
  • 所有不需要计算的梯度张量都是叶张量,无论是否包含操作
# all_leaf is True
a = torch.rand(10)    # 非梯度tensor -- 总是为True
a = torch.rand(10) + 5
a = torch.rand(10).cuda()
  • 由不需要梯度的张量创建的新的需要梯度的张量是叶张量
# all_leaf is True
# 由非梯度tensor变成梯度tensor后直接赋给,可以成为叶张量
a = torch.rand(10).requires_grad_()
#由非梯度tensor移动到gpu上再变成梯度tensor后直接赋给,可以成为叶张量
a = torch.rand(10).cuda().requires_grad_()

更多推荐

Pytorch is

本文发布于:2024-02-14 15:21:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1764120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Pytorch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!