【发布时间】:2021-03-17 17:14:06
【问题描述】:
来自 PyTorch documentation:
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
但是为什么 e 和 f 叶张量,当它们都从 CPU 张量转换成 Cuda 张量(一个操作)?
是因为张量 e 在就地操作requires_grad_() 之前 被转换为 Cuda 吗?
因为f 是通过赋值device="cuda" 而不是通过方法.cuda() 转换的?
【问题讨论】:
标签: python pytorch documentation