Which of the following best explains the main advantage of dynamic computation graphs in PyTorch compared to static graphs?
Think about how dynamic graphs let you build the model step-by-step as data flows through.
Dynamic computation graphs are built on-the-fly during each forward pass, allowing the model to change structure based on input data. This flexibility is useful for tasks like processing sequences of different lengths.
What will be the output of the following PyTorch code snippet?
import torch x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True) y = x * 2 if x.sum() > 5 else x + 2 print(y)
Check the sum of x and which branch of the if-else runs.
The sum of x is 6, which is greater than 5, so y = x * 2. The output tensor has gradient tracking because x requires grad.
You want to build a model that processes sentences of different lengths without padding. Which model type benefits most from dynamic computation graphs?
Think about models that naturally handle sequences of varying lengths.
RNNs process inputs step-by-step and can handle variable-length sequences naturally. Dynamic graphs in PyTorch allow building the graph differently for each sequence length.
How does using a dynamic computation graph in PyTorch typically affect training speed compared to static graphs?
Consider the overhead of building the graph each time.
Dynamic graphs are rebuilt every forward pass, which adds overhead and can slow training compared to static graphs that are compiled once.
What error will this PyTorch code raise when run?
import torch
x = torch.tensor([1.0, 2.0], requires_grad=True)
with torch.no_grad():
y = x * 2
z = y.sum()
z.backward()Think about what torch.no_grad() does to the computation graph.
Inside torch.no_grad(), operations do not track gradients. So y does not require grad, causing backward() to fail with RuntimeError.