It tells PyTorch whether to track operations on a tensor for automatic differentiation. If requires_grad=True, PyTorch records operations to compute gradients later.
Click to reveal answer
beginner
How do you set a tensor to not track gradients in PyTorch?
You set requires_grad=False when creating the tensor or use tensor.detach() to stop tracking gradients.
Click to reveal answer
intermediate
Why might you want to set requires_grad=False for some tensors?
To save memory and computation when you don't need gradients, like for input data or fixed parameters.
Click to reveal answer
beginner
What happens if you perform operations on a tensor with requires_grad=True?
PyTorch builds a computation graph to track those operations, enabling gradient calculation during backpropagation.
Click to reveal answer
beginner
How can you check if a tensor is tracking gradients?
By accessing the tensor's requires_grad attribute, which returns True or False.
Click to reveal answer
What does setting requires_grad=True on a tensor do?
AConverts the tensor to a numpy array
BPrevents the tensor from being used in calculations
CDeletes the tensor after use
DTracks operations for gradient computation
✗ Incorrect
Setting requires_grad=True tells PyTorch to track operations on the tensor for automatic differentiation.
How can you stop a tensor from tracking gradients after it was created with requires_grad=True?
ACall <code>tensor.backward()</code>
BSet <code>requires_grad=True</code> again
CUse <code>tensor.detach()</code>
DConvert it to a list
✗ Incorrect
tensor.detach() creates a new tensor that does not track gradients.
Why might you want to set requires_grad=False for some tensors?
ATo increase the tensor size
BTo save memory and speed up computation
CTo make the tensor immutable
DTo convert the tensor to CPU
✗ Incorrect
Not tracking gradients saves memory and computation when gradients are not needed.
Which attribute tells you if a tensor tracks gradients?
Atensor.requires_grad
Btensor.grad
Ctensor.shape
Dtensor.dtype
✗ Incorrect
tensor.requires_grad is a boolean indicating if gradients are tracked.
If a tensor has requires_grad=True, what does PyTorch build during operations?
AA computation graph
BA list of numbers
CA new tensor on CPU
DA random tensor
✗ Incorrect
PyTorch builds a computation graph to compute gradients during backpropagation.
Explain the purpose of the requires_grad flag in PyTorch and how it affects tensor operations.
Think about how PyTorch knows which tensors to compute gradients for.
You got /4 concepts.
Describe scenarios when you would set requires_grad=False for a tensor in a machine learning model.
Consider parts of the model that do not learn or update.
You got /4 concepts.
Practice
(1/5)
1. What does setting requires_grad=True on a PyTorch tensor do?
easy
A. It tells PyTorch to track operations on the tensor for gradient calculation.
B. It freezes the tensor so it won't change during training.
C. It converts the tensor to a NumPy array.
D. It deletes the tensor from memory.
Solution
Step 1: Understand the purpose of requires_grad
The flag requires_grad=True tells PyTorch to remember all operations on the tensor so it can compute gradients later.
Step 2: Identify what happens when requires_grad is True
This tracking is essential for training models because gradients are used to update weights.
Final Answer:
It tells PyTorch to track operations on the tensor for gradient calculation. -> Option A
Quick Check:
requires_grad=True means track gradients [OK]
Hint: True means track gradients for learning, False means no tracking [OK]
Common Mistakes:
Thinking requires_grad freezes the tensor
Confusing requires_grad with tensor conversion
Assuming it deletes the tensor
2. Which of the following is the correct way to create a tensor with gradient tracking enabled?
easy
A. torch.tensor([1.0, 2.0, 3.0], grad=True)
B. torch.tensor([1.0, 2.0, 3.0], requires_grad=False)
C. torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
D. torch.tensor([1.0, 2.0, 3.0], track_grad=True)
Solution
Step 1: Recall the correct argument name
The correct argument to enable gradient tracking is requires_grad=True.
Step 2: Check each option
torch.tensor([1.0, 2.0, 3.0], requires_grad=True) uses requires_grad=True, which is correct. Options C and D use invalid argument names, and B disables gradient tracking.
Final Answer:
torch.tensor([1.0, 2.0, 3.0], requires_grad=True) -> Option C
Quick Check:
Use requires_grad=True to track gradients [OK]
Hint: Use requires_grad=True exactly to enable gradients [OK]
Common Mistakes:
Using incorrect argument names like grad or track_grad
Setting requires_grad to False by mistake
Forgetting to set requires_grad at tensor creation
3. What will be the output of this code snippet?
import torch
x = torch.tensor([2.0, 3.0], requires_grad=True)
y = x * 2
y.backward(torch.tensor([1.0, 1.0]))
print(x.grad)
medium
A. tensor([2., 2.])
B. tensor([1., 1.])
C. tensor([4., 6.])
D. None
Solution
Step 1: Understand the operations and gradient flow
y = x * 2 means each element in x is multiplied by 2. The gradient of y with respect to x is 2 for each element.
Step 2: Apply backward with gradient vector
Calling y.backward with tensor([1.0, 1.0]) sums gradients weighted by 1. So x.grad will be tensor([2., 2.]).
Final Answer:
tensor([2., 2.]) -> Option A
Quick Check:
Gradient of 2*x is 2 [OK]
Hint: Gradient of x*2 is 2, so x.grad = [2, 2] [OK]
Common Mistakes:
Expecting gradient to be the original tensor values
Confusing output with input tensor
Forgetting to call backward()
4. Identify the error in this code snippet related to gradient tracking:
import torch
x = torch.tensor([1.0, 2.0, 3.0])
y = x * 3
y.backward()
medium
A. x was not created with requires_grad=True, so backward() will fail.
B. There is no error; the code runs fine.
C. The backward() call is missing a gradient argument.
D. The tensor y is not a scalar, so backward() cannot be called.
Solution
Step 1: Check requires_grad flag on x
x was created without requires_grad=True, so it does not track gradients. This means y also does not track gradients, causing backward() to fail.
Step 2: Understand backward() requirements
The error occurs because tensors need grad_fn (from requires_grad=True) to call backward(). Additionally, non-scalar tensors need a gradient argument, so calling backward() without it on a vector tensor causes an error.
Final Answer:
The tensor y is not a scalar, so backward() cannot be called. -> Option D
Quick Check:
Non-scalar tensors require gradient argument in backward() [OK]
Hint: Non-scalar tensors need gradient argument in backward() [OK]
Common Mistakes:
Forgetting requires_grad=True on tensors before backward()
Ignoring requires_grad flag importance
Not providing gradient argument for vector tensors
5. You want to freeze the first layer of a neural network during training to save memory. Which code snippet correctly disables gradient tracking for that layer's parameters?
hard
A. model.layer1.grad = None
B. for param in model.layer1.parameters():
param.requires_grad = False
C. model.layer1.requires_grad = False
D. for param in model.layer1.parameters():
param.requires_grad = True
Solution
Step 1: Understand how to freeze layers
Freezing means stopping gradient calculation for parameters by setting their requires_grad to False.
Step 2: Check correct way to set requires_grad
Parameters are tensors inside the layer. We must set param.requires_grad = False for each parameter. for param in model.layer1.parameters():
param.requires_grad = False does this correctly.
Final Answer:
for param in model.layer1.parameters():
param.requires_grad = False -> Option B
Quick Check:
Set requires_grad=False on parameters to freeze [OK]
Hint: Freeze by setting requires_grad=False on each parameter [OK]
Common Mistakes:
Trying to set requires_grad on the whole layer object
Setting requires_grad=True instead of False
Confusing grad attribute with requires_grad
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.