Bird
Raised Fist0
PyTorchml~20 mins

Why automatic differentiation enables training in PyTorch - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Automatic Differentiation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is automatic differentiation important in training neural networks?

Imagine you want to teach a robot to recognize cats in photos. The robot uses a neural network that learns by adjusting numbers called weights. Why do we need automatic differentiation for this learning process?

AIt calculates the exact changes needed for weights by computing gradients automatically.
BIt randomly changes weights without any calculation to speed up training.
CIt stores all training data to use later for predictions.
DIt converts images into text so the robot can understand them.
Attempts:
2 left
💡 Hint

Think about how the robot knows how to improve itself after seeing mistakes.

❓ Predict Output
intermediate
2:00remaining
Output of PyTorch gradient calculation

What is the output of the following PyTorch code snippet?

PyTorch
import torch
x = torch.tensor(2.0, requires_grad=True)
y = x ** 3
z = y + 2 * x
z.backward()
print(x.grad.item())
A12.0
B14.0
C10.0
D8.0
Attempts:
2 left
💡 Hint

Recall the derivative of z = x^3 + 2x with respect to x.

❓ Model Choice
advanced
2:00remaining
Choosing the right model for automatic differentiation demonstration

You want to demonstrate automatic differentiation with a simple model that has a clear gradient. Which model is best?

AA linear model with one input and one output: y = wx + b
BA random forest with multiple decision trees
CA k-nearest neighbors model without parameters
DA clustering model like k-means
Attempts:
2 left
💡 Hint

Think about which model has parameters that can be differentiated easily.

❓ Hyperparameter
advanced
2:00remaining
Effect of learning rate on gradient-based training

During training with automatic differentiation, you adjust the learning rate. What happens if the learning rate is too high?

AThe gradients will become zero and stop updating weights.
BThe model will train slower but more accurately.
CThe model weights may change too much, causing training to become unstable.
DThe model will ignore the gradients and use random updates.
Attempts:
2 left
💡 Hint

Consider what happens if you take very large steps when trying to reach a target.

🔧 Debug
expert
2:00remaining
Why does this PyTorch code raise an error during backward pass?

Consider this PyTorch code:

import torch
x = torch.tensor(3.0, requires_grad=True)
y = x ** 2
z = y.detach() + 1
z.backward()

What error will occur and why?

AValueError because backward() requires a scalar but y is not scalar.
BTypeError because detach() returns a list instead of a tensor.
CNo error; the code runs and prints gradients correctly.
DRuntimeError because y.detach() breaks the computation graph, so z has no grad_fn.
Attempts:
2 left
💡 Hint

Think about what detach() does to the tensor in terms of gradient tracking.

Practice

(1/5)
1. Why is automatic differentiation important for training machine learning models in PyTorch?
easy
A. It stores the training data efficiently.
B. It generates random numbers for model initialization.
C. It visualizes the model architecture.
D. It calculates gradients automatically to update model parameters.

Solution

  1. Step 1: Understand the role of gradients in training

    Training adjusts model numbers to reduce errors, which requires knowing how errors change with each number.
  2. Step 2: Identify what automatic differentiation does

    Automatic differentiation calculates these changes (gradients) automatically, so the model can update correctly.
  3. Final Answer:

    It calculates gradients automatically to update model parameters. -> Option D
  4. Quick Check:

    Gradients = Automatic differentiation [OK]
Hint: Automatic differentiation = auto gradient calculation [OK]
Common Mistakes:
  • Confusing gradient calculation with data storage
  • Thinking it generates random numbers
  • Assuming it visualizes models
2. Which PyTorch code snippet correctly enables automatic differentiation for a tensor?
easy
A. tensor = torch.tensor([1.0, 2.0], requires_grad=False)
B. tensor = torch.tensor([1.0, 2.0], requires_grad=True)
C. tensor = torch.tensor([1.0, 2.0]).detach()
D. tensor = torch.tensor([1.0, 2.0]).numpy()

Solution

  1. Step 1: Check how to enable gradient tracking

    In PyTorch, setting requires_grad=True tells PyTorch to track operations for gradients.
  2. Step 2: Identify the correct code snippet

    tensor = torch.tensor([1.0, 2.0], requires_grad=True) sets requires_grad=True, enabling automatic differentiation.
  3. Final Answer:

    tensor = torch.tensor([1.0, 2.0], requires_grad=True) -> Option B
  4. Quick Check:

    requires_grad=True enables gradients [OK]
Hint: Use requires_grad=True to track gradients [OK]
Common Mistakes:
  • Using requires_grad=False disables gradients
  • Calling detach() stops gradient tracking
  • Converting to numpy removes gradient info
3. What will be the output of this PyTorch code?
import torch
x = torch.tensor(2.0, requires_grad=True)
y = x ** 3
z = y + 2 * x
z.backward()
print(x.grad.item())
medium
A. 8.0
B. 12.0
C. 14.0
D. 10.0

Solution

  1. Step 1: Calculate the function and its derivative

    The function is z = x^3 + 2x. Its derivative is dz/dx = 3x^2 + 2.
  2. Step 2: Compute the gradient at x=2.0

    Plug in x=2: dz/dx = 3*(2^2) + 2 = 3*4 + 2 = 12 + 2 = 14.
  3. Final Answer:

    14.0 -> Option C
  4. Quick Check:

    Derivative at 2 = 14 [OK]
Hint: Derivative of x³+2x at 2 is 14 [OK]
Common Mistakes:
  • Forgetting to apply chain rule
  • Calculating derivative incorrectly
  • Using wrong x value
4. Identify the error in this PyTorch code that prevents training:
import torch
x = torch.tensor([1.0, 2.0, 3.0])
y = x.sum()
y.backward()
print(x.grad)
medium
A. x does not have requires_grad=True, so gradients are not tracked.
B. Using sum() prevents gradient calculation.
C. backward() must be called on x, not y.
D. x.grad should be printed before backward() call.

Solution

  1. Step 1: Check if gradients are tracked

    By default, tensors have requires_grad=False, so no gradients are stored.
  2. Step 2: Identify why backward() fails to compute gradients

    Since x lacks requires_grad=True, x.grad remains None after backward.
  3. Final Answer:

    x does not have requires_grad=True, so gradients are not tracked. -> Option A
  4. Quick Check:

    requires_grad=True needed for gradients [OK]
Hint: Set requires_grad=True to track gradients [OK]
Common Mistakes:
  • Assuming sum() disables gradients
  • Calling backward() on wrong tensor
  • Printing grad before backward()
5. You want to train a model with parameters w and b using PyTorch. Which sequence correctly uses automatic differentiation to update parameters after computing loss?
hard
A. loss.backward() with torch.no_grad(): w -= learning_rate * w.grad b -= learning_rate * b.grad w.grad.zero_() b.grad.zero_()
B. loss.backward() w = w - learning_rate * w.grad b = b - learning_rate * b.grad w.grad.zero_() b.grad.zero_()
C. loss.backward() w.grad = None b.grad = None w -= learning_rate * w.grad b -= learning_rate * b.grad
D. loss.backward() w.grad.zero_() b.grad.zero_() w -= learning_rate * w.grad b -= learning_rate * b.grad

Solution

  1. Step 1: Understand gradient update with no_grad()

    Parameter updates must be done without tracking gradients, so use torch.no_grad() context.
  2. Step 2: Zero gradients after update

    After updating, gradients must be reset with grad.zero_() to avoid accumulation.
  3. Final Answer:

    Use loss.backward(), update inside torch.no_grad(), then zero gradients. -> Option A
  4. Quick Check:

    Update params inside no_grad, then zero grads [OK]
Hint: Update params inside no_grad, then zero grads [OK]
Common Mistakes:
  • Updating params outside no_grad() causes errors
  • Not zeroing gradients after update
  • Setting grad to None instead of zero_()
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