Assess your foundational knowledge of PyTorch basics, focusing on tensors, automatic differentiation with autograd, and constructing simple training loops. This quiz is ideal for learners seeking to reinforce their understanding of key PyTorch operations, gradient computation, and model training workflows.
Which of the following lines correctly creates a 2D tensor of zeros with shape (3, 2)?
Explanation: The correct syntax for creating a tensor filled with zeros in a specified shape is torch.zeros(), passing the shape as a tuple. The option tensor.zeros(3,2) is incorrect because 'tensor' is not the right namespace and the shape should be a tuple. torch.zeroes([3, 2]) contains a typo in the function name ('zeroes' should be 'zeros'). Tensor.zeros([3,2]) uses the wrong case and object reference. Only torch.zeros((3, 2)) accurately follows the common Python and PyTorch syntax.
How can you enable a tensor to track gradients for automatic differentiation?
Explanation: Setting requires_grad=True during tensor creation tells the autograd system to track operations on this tensor for gradient computation. Calling grad() does not enable gradient tracking; instead, it's a property to access computed gradients. Multiplying the tensor or saving it as a NumPy array has no effect on gradient tracking. Only the correct option enables the tensor to be part of the computation graph.
After computing a loss value in a training step, what method is commonly called to compute gradients for parameters involved?
Explanation: The backward() method is used to backpropagate the error and compute gradients of tensors with requires_grad set to True. Neither compute_gradients() nor backprop() are valid PyTorch methods, and autograd() is not a function. The backward() method specifically triggers the automatic differentiation process.
Where can the gradients of a parameter tensor be found after calling backward()?
Explanation: After calling backward(), gradients are populated in the .grad attribute of each tensor with requires_grad set to True. There is no .backprop property, nor are gradients stored in a separate array by default. Running the optimizer does not store gradients but typically uses them to update parameters. The .grad attribute is the direct way to access these values.
What is the correct way to convert a PyTorch tensor 'x' to a NumPy array?
Explanation: The method for converting a tensor to a NumPy array is x.numpy(), which is efficient if the tensor is on the CPU. x.to_numpy() and x.asnumpy() are not valid PyTorch tensor methods. np.array(x) works but creates a copy and does not share memory, which can be less efficient. Using x.numpy() is the direct and recommended method.
Why is it important to zero gradients (e.g., optimizer.zero_grad()) in a training loop?
Explanation: Gradients accumulate by default to enable gradient accumulation over multiple backward passes, so they must be manually zeroed at the start of each iteration. This is not done to save memory or initialize parameters, nor does it directly make training faster. Forgetting to zero gradients may lead to incorrect parameter updates.
Given tensors a = [1, 2, 3] and b = [4, 5, 6], what is the result of a + b?
Explanation: The + operator for tensors performs element-wise addition, so the result is [1+4, 2+5, 3+6] which is [5, 7, 9]. Concatenation would result in [1, 2, 3, 4, 5, 6], which is not what this operator does. The option [4, 5, 6] only lists one of the original tensors, and [1, 2, 3, 10, 12, 15] does not match any valid operation. Element-wise addition gives [5, 7, 9].
Which context disables gradient tracking for the code within its block?
Explanation: The correct way to prevent gradient tracking is to use 'with torch.no_grad()' as a context manager. torch.nograd(), torch.stop_grad(), and no_grad() are invalid names and will cause errors. Only 'with torch.no_grad():' is recognized for this purpose, making it useful for inference or evaluation phases.
Which method is commonly called on the optimizer to update model parameters after gradients are calculated?
Explanation: Calling optimizer.step() applies the computed gradients to update the parameters. optimizer.update() and optimizer.apply_gradients() do not exist in this library's API, and optimizer.backward() is incorrect because backward() is a method on tensors, not optimizers. Only optimizer.step() has the effect of performing a parameter update.
How can you check whether a tensor is currently stored on the CPU or the GPU?
Explanation: Accessing the tensor.device property reveals whether the tensor is on the CPU or GPU. The tensor.is_gpu() function does not exist, and tensor.location or tensor.check_device() are not standard methods or attributes. Only tensor.device provides this information concisely.