Complete the code to zero the gradients of the optimizer before the backward pass.
optimizer.[1]()In PyTorch, optimizer.zero_grad() clears old gradients before computing new ones.
Complete the code to zero the gradients of the model's parameters before backpropagation.
for param in model.parameters(): if param.grad is not None: param.grad.[1]()
Each parameter's gradient tensor can be zeroed using param.grad.zero_() in PyTorch.
Fix the error in zeroing gradients before the backward pass.
optimizer.[1]()
loss.backward()
optimizer.step()Before calling loss.backward(), gradients must be cleared with optimizer.zero_grad().
Fill both blanks to zero gradients for all parameters in the model.
for param in model.parameters(): if param.grad is not None: param.grad.[1]() model.[2]()
Zero gradients of each parameter with param.grad.zero_() and zero model gradients with model.zero_grad().
Fill all three blanks to zero gradients, perform backward pass, and update weights.
optimizer.[1]() loss.[2]() optimizer.[3]()
First zero gradients with optimizer.zero_grad(), then compute gradients with loss.backward(), finally update weights with optimizer.step().