代码:
a = torch.randn((),dtype=dtype, device=device, requires_grad=True)
b = torch.randn((),dtype=dtype, device=device, requires_grad=True)
with torch.no_grad():
a -= learning_rate * a.grad
z = a+b
print(f"a with no gradient:{a.requires_grad}")
print(f"z with no gradient:{z.requires_grad}")
输出:
a with no gradient:True
z with no gradient:False
with torch.no_grad(): 仅仅使得这个代码块中的tensor计算都不带有gradient。对于已经设置requires_grad=True的tensor(如a),不会改变其状态,对于由已有tensor生成的新的tensor(如z),其requires_grad=False