PyTorch Certified Associate (PTCA) Practice Questions

PyTorch Certified Associate exam preparation

PyTorch Certified Associate (PTCA) practice questions in English: 180 questions with explanations. The mock exam has 60 questions and 120 minutes.

PyTorch Certified Associate (PTCA) free question 2 / 10 · Domain 1: PyTorch Fundamentals

A pipeline holds a large NumPy array arr and needs a torch.Tensor over its data. Which call does the torch.Tensor documentation name for avoiding a copy?

  1. torch.as_tensor(arr), the call the documentation gives for avoiding a copy
  2. torch.tensor(arr), a constructor that shares the memory of the array
  3. torch.tensor(arr).detach(), a call that drops the copy the constructor made
  4. torch.tensor(arr).requires_grad_(), a call that turns the copy back into a view
Show the answer and explanation

Answer: A

Why this is correct
The documentation lists the constructors for building tensors from Python sequences and from NumPy arrays. torch.tensor() always copies data, so a large array is duplicated in memory. For a NumPy array where a copy should be avoided, the documentation says to use torch.as_tensor(). For a tensor you already have, requires_grad_() or detach() change the flag or the graph link without a copy.

Why the other options are wrong
"torch.tensor(arr), a constructor that shares the memory of the array" … The documentation warns that torch.tensor() always copies data. The copy is the reason it names torch.as_tensor() for the case where you want to avoid one.
"torch.tensor(arr).detach(), a call that drops the copy the constructor made" … By the time detach runs, torch.tensor() has already copied the data. The documentation gives detach() as a way to change a tensor's requires_grad flag without a copy, not as a way to undo the constructor's copy.
"torch.tensor(arr).requires_grad_(), a call that turns the copy back into a view" … requires_grad_() changes the flag on a tensor that already exists. It does not turn the copy made by torch.tensor() into memory shared with the array.

Where people trip
The name torch.tensor() suggests a wrapper, but it is a copying constructor. Reach for as_tensor() when the memory is large and sharing is acceptable.

Go to the PyTorch Certified Associate (PTCA) practice app (10 free questions; purchase unlocks all questions)

See the full practice set