a = torch.nn.Parameter(torch.ones(5, 5))
a = a.cuda()
print(a.requires_grad)
b = a
b = b - 2
print('a ', a)
print('b ', b)
loss = (b - 1).pow(2).sum()
loss.backward()
print(a.grad)
print(b.grad)
Run Code Online (Sandbox Code Playgroud)
执行代码后,虽然a.grad是。但如果把代码去掉,则可向后丢失。Nonea.requires_gradTruea = a.cuda()a.grad