Convenience and detach_ (in case you have used build_graph=True):

  
      github.com
  
  
    pytorch/pytorch/blob/4460c8b034f8fd544ff9c271c4aa21698644d352/torch/optim/optimizer.py#L159-L166 3

def zero_grad(self):
    r"""Clears the gradients of all optimized :class:`torch.Tensor` s."""
    for group in self.param_groups:
        for p in group['params']:
            if p.grad is not None:
                p.grad.detach_()
                p.grad.zero_()





  
  
    
    
  
  


Best regards
Thomas