0
0
PyTorchml~5 mins

Zeroing gradients in PyTorch - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does zeroing gradients mean in PyTorch?
Zeroing gradients means setting all the gradients of model parameters to zero before starting a new backward pass. This prevents accumulation of gradients from multiple passes.
Click to reveal answer
beginner
Why do we need to zero gradients before calling backward() in PyTorch?
Because PyTorch accumulates gradients by default, zeroing them ensures that gradients from previous batches do not mix with the current batch's gradients.
Click to reveal answer
beginner
Which PyTorch method is commonly used to zero gradients?
The method optimizer.zero_grad() is commonly used to reset all gradients to zero before the backward pass.
Click to reveal answer
intermediate
What happens if you forget to zero gradients in a training loop?
Gradients will accumulate across batches, causing incorrect updates and making training unstable or ineffective.
Click to reveal answer
beginner
Show a simple PyTorch training loop snippet that includes zeroing gradients.
for data, target in dataloader: optimizer.zero_grad() # Reset gradients output = model(data) loss = loss_fn(output, target) loss.backward() # Compute gradients optimizer.step() # Update parameters
Click to reveal answer
What is the purpose of calling optimizer.zero_grad() in PyTorch?
ATo reset gradients to zero before computing new gradients
BTo update model parameters
CTo compute the loss value
DTo save the model state
If you skip zeroing gradients, what will happen during training?
AGradients will accumulate, causing incorrect updates
BModel parameters will not update
CLoss will always be zero
DTraining will be faster
Which PyTorch function computes gradients for model parameters?
Amodel.eval()
Boptimizer.step()
Closs.backward()
Doptimizer.zero_grad()
When should zeroing gradients be done in the training loop?
AAfter optimizer.step()
BBefore loss.backward()
CAfter loss.backward()
DAt the end of training
What does optimizer.step() do in PyTorch?
AComputes the loss
BResets gradients to zero
CLoads data into the model
DUpdates model parameters using current gradients
Explain why zeroing gradients is important in a PyTorch training loop.
Think about what happens if gradients keep adding up over batches.
You got /3 concepts.
    Describe the sequence of steps in a PyTorch training loop involving gradients.
    Focus on the order of zeroing, backward, and stepping.
    You got /5 concepts.