Skip to content
masterclass Buddy

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.

Subscribe
  • optimizer.zero_grad()
  • model.zero_grad()
  • param.grad
  • backward()
  • gradient descent

Source: Zeroing out gradients in PyTorch. Checked 2026-11-03.