Bird
Raised Fist0
PyTorchml~20 mins

Detaching from computation graph in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Detach Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PyTorch code snippet?

Consider the following PyTorch code that creates a tensor, performs an operation, and detaches it from the computation graph. What will be the value of detached_tensor.requires_grad?

PyTorch
import torch
x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
y = x * 2
detached_tensor = y.detach()
print(detached_tensor.requires_grad)
AFalse
BRaises an error
CTrue
DNone
Attempts:
2 left
💡 Hint

Think about what detaching a tensor means for gradient tracking.

🧠 Conceptual
intermediate
2:00remaining
Why do we use detach() in PyTorch?

Which of the following best explains the purpose of using detach() on a tensor in PyTorch?

ATo create a tensor that shares data but does not track operations for gradient computation
BTo permanently delete the tensor from memory
CTo convert a tensor to a NumPy array
DTo enable gradient tracking on a tensor that previously did not require gradients
Attempts:
2 left
💡 Hint

Think about how detaching affects the computation graph and gradient flow.

🔧 Debug
advanced
2:00remaining
Why does this code raise an error when calling backward()?

Examine the code below. Why does calling loss.backward() raise an error?

PyTorch
import torch
x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
y = x * 2
detached_y = y.detach()
loss = detached_y.sum()
loss.backward()
ABecause <code>loss</code> is not a scalar tensor
BBecause <code>detached_y</code> is not connected to the computation graph, so gradients cannot be computed
CBecause <code>x</code> does not require gradients
DBecause <code>detach()</code> modifies the original tensor in-place
Attempts:
2 left
💡 Hint

Consider what happens to the graph when you detach a tensor and then try to backpropagate.

❓ Model Choice
advanced
2:00remaining
When should you use detach() in a training loop?

Which scenario below correctly describes when to use detach() in a PyTorch training loop?

AWhen you want to initialize model weights randomly
BWhen you want to convert tensors to CPU for faster computation
CWhen you want to increase the learning rate dynamically
DWhen you want to stop gradients from flowing through part of the model to save memory or prevent updates
Attempts:
2 left
💡 Hint

Think about controlling which parts of the model get updated during training.

❓ Metrics
expert
3:00remaining
How does detaching affect gradient computation and memory usage?

Consider a deep neural network where you detach intermediate tensors during forward pass. What is the effect on gradient computation and memory usage?

ADetaching intermediate tensors increases memory usage and speeds up gradient computation
BDetaching intermediate tensors has no effect on memory or gradients
CDetaching intermediate tensors reduces memory usage but prevents gradients from flowing back through those tensors, possibly affecting training
DDetaching intermediate tensors causes the model to train faster without any side effects
Attempts:
2 left
💡 Hint

Think about the trade-off between saving memory and allowing gradients to flow.

Practice

(1/5)
1. What does the detach() method do to a PyTorch tensor?
easy
A. It creates a new tensor that shares data but is not tracked for gradients.
B. It deletes the tensor from memory immediately.
C. It converts the tensor to a NumPy array.
D. It resets the tensor's values to zero.

Solution

  1. Step 1: Understand what detach() does

    The detach() method creates a new tensor that shares the same data but is removed from the computation graph, so gradients are not tracked.
  2. Step 2: Compare options with detach() behavior

    Deleting the tensor from memory immediately, converting to a NumPy array, and resetting values to zero are all unrelated to detach(). Only creating a new tensor that shares data but is not tracked for gradients correctly describes the behavior.
  3. Final Answer:

    It creates a new tensor that shares data but is not tracked for gradients. -> Option A
  4. Quick Check:

    detach() = stops gradient tracking [OK]
Hint: detach() stops gradients but shares data memory [OK]
Common Mistakes:
  • Thinking detach() deletes the tensor
  • Confusing detach() with conversion to NumPy
  • Assuming detach() resets tensor values
2. Which of the following is the correct syntax to detach a tensor x in PyTorch?
easy
A. x.detach()
B. detach(x)
C. x.detach
D. x.detach[]

Solution

  1. Step 1: Recall the method call syntax in PyTorch

    Methods are called with parentheses, so detach() must be called as x.detach().
  2. Step 2: Check each option

    x.detach() uses correct method call syntax. detach(x) is a function call which does not exist. x.detach misses parentheses, so it returns a method object, not a tensor. x.detach[] uses invalid syntax.
  3. Final Answer:

    x.detach() -> Option A
  4. Quick Check:

    Method call needs parentheses [OK]
Hint: Use parentheses to call detach() method [OK]
Common Mistakes:
  • Forgetting parentheses after detach
  • Using detach as a standalone function
  • Using invalid bracket syntax
3. Consider the following code:
import torch
x = torch.tensor([1.0, 2.0], requires_grad=True)
y = x * 2
y_detached = y.detach()
z = y_detached.sum()

What happens if you call z.backward()?
medium
A. It computes gradients for x successfully.
B. It raises an error because y_detached is detached from the graph.
C. It computes gradients for y_detached only.
D. It does nothing because z is a scalar.

Solution

  1. Step 1: Understand detach() effect on computation graph

    Calling detach() on y creates a tensor not tracked for gradients, so y_detached has no gradient history.
  2. Step 2: Analyze backward() on detached tensor

    Calling backward() on z (which depends on y_detached) will raise an error because y_detached is not connected to x or any requires_grad=True tensor.
  3. Final Answer:

    It raises an error because y_detached is detached from the graph. -> Option B
  4. Quick Check:

    Backward on detached tensor = error [OK]
Hint: Backward fails on detached tensors without grad history [OK]
Common Mistakes:
  • Assuming backward computes gradients through detached tensors
  • Thinking detached tensors still track gradients
  • Ignoring that detach breaks gradient flow
4. You have the following code snippet:
import torch
x = torch.tensor([3.0], requires_grad=True)
y = x * 4
y_detached = y.detach()
z = y_detached + 2
y.backward()

What is the error in this code?
medium
A. Using detach() before backward() causes runtime error.
B. Calling backward() on y_detached instead of y.
C. Calling backward() on y which depends on detached tensor y_detached.
D. No error; code runs fine and computes gradients.

Solution

  1. Step 1: Identify which tensor backward() is called on

    Backward is called on y, which is connected to x and requires gradients.
  2. Step 2: Check if detach() affects backward on y

    Detach is called on y to create y_detached, but backward is not called on y_detached. So, gradient computation for y and x works fine.
  3. Final Answer:

    No error; code runs fine and computes gradients. -> Option D
  4. Quick Check:

    Backward on original tensor works despite detach() elsewhere [OK]
Hint: Backward on original tensor works even if detached tensor exists [OK]
Common Mistakes:
  • Confusing which tensor backward() is called on
  • Assuming detach() breaks all gradient computations
  • Thinking detach() affects original tensor's graph
5. You want to save memory during training by stopping gradient tracking on intermediate tensor h but still use its values for further computation. Which approach correctly applies detach() in this scenario?
import torch
x = torch.tensor([2.0], requires_grad=True)
h = (x ** 2) + 3
# Your code here
y = h * 5
y.backward()
hard
A. Use h = h.clone() before computing y.
B. Call h.requires_grad_(False) instead of detach().
C. Replace h with h = h.detach() before computing y.
D. Do not use detach(); just call y.backward() directly.

Solution

  1. Step 1: Understand the goal of stopping gradient tracking

    To save memory and stop gradients on h, we must detach it from the computation graph before using it further.
  2. Step 2: Evaluate each option

    Replace h with h = h.detach() before computing y. correctly uses h.detach() to stop gradient tracking. Call h.requires_grad_(False) instead of detach(). changes requires_grad but does not detach from graph. Do not use detach(); just call y.backward() directly. keeps gradients tracked, not saving memory. Use h = h.clone() before computing y. clones data but does not stop gradient tracking.
  3. Final Answer:

    Replace h with h.detach() before computing y. -> Option C
  4. Quick Check:

    detach() stops gradients and saves memory [OK]
Hint: Use h = h.detach() to stop gradients but keep data [OK]
Common Mistakes:
  • Using requires_grad_(False) instead of detach()
  • Not detaching and expecting memory savings
  • Using clone() which does not stop gradients
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard