PyTorch · Method
model.zero_grad
Clear gradients before each training step because PyTorch accumulates them when backward() runs. Use optimizer.zero_grad(), or model.zero_grad() when the optimizer contains all model parameters.
Last updated
Syntax
optimizer.zero_grad()In-depth API docs are only for subscribers: parameters, examples, common mistakes and interview practice.
SubscribeRelated
optimizer.zero_grad()model.zero_grad()param.gradbackward()gradient descent