有两种方式直接把模型的参数梯度设成 0 :
model.zero_grad() optimizer.zero_grad()
如果想要把某一Variable的梯度置为 0,只需用以下语句:
Variable.grad.data.zero_()