Oma*_*idi 10 python documentation pytorch
来自 PyTorch文档:
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
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
Run Code Online (Sandbox Code Playgroud)
但是,当e
和f
叶张量也都是从 CPU 张量转换为 Cuda 张量(一种运算)时,为什么它们是 Cuda 张量?
是因为Tensor在就地操作之前e
就被投进了Cuda吗?requires_grad_()
因为f
是通过赋值device="cuda"
而不是通过方法来转换的.cuda()
?
小智 17
当张量第一次创建时,它成为叶节点。
基本上,神经网络的所有输入和权重都是计算图的叶节点。
当对张量执行任何操作时,它就不再是叶节点。
b = torch.rand(10, requires_grad=True) # create a leaf node
b.is_leaf # True
b = b.cuda() # perform a casting operation
b.is_leaf # False
Run Code Online (Sandbox Code Playgroud)
requires_grad_()
cuda()
与其他操作不同。
它创建一个新的张量,因为需要梯度(可训练权重)的张量不能依赖于其他任何东西。
e = torch.rand(10) # create a leaf node
e.is_leaf # True
e = e.cuda() # perform a casting operation
e.is_leaf # False
e = e.requires_grad_() # this creates a NEW tensor
e.is_leaf # True
Run Code Online (Sandbox Code Playgroud)
此外,detach()
操作创建一个不需要梯度的新张量:
b = torch.rand(10, requires_grad=True)
b.is_leaf # True
b = b.detach()
b.is_leaf # True
Run Code Online (Sandbox Code Playgroud)
在最后一个示例中,我们创建了一个已存在于 cuda 设备上的新张量。
我们不需要任何操作来投射它。
f = torch.rand(10, requires_grad=True, device="cuda") # create a leaf node on cuda
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8155 次 |
最近记录: |