Bird
Raised Fist0
PyTorchml~20 mins

Why tensors are PyTorch's core data structure - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tensor Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why are tensors important in PyTorch?
Which of the following best explains why tensors are the core data structure in PyTorch?
ATensors are simple Python lists that PyTorch uses to store data without any special features.
BTensors allow PyTorch to perform fast mathematical operations on GPUs and CPUs with automatic differentiation support.
CTensors are used only for visualization purposes in PyTorch.
DTensors are only used to store images and cannot be used for other data types.
Attempts:
2 left
💡 Hint
Think about what makes PyTorch fast and flexible for machine learning.
❓ Predict Output
intermediate
2:00remaining
Output of tensor operation with GPU
What will be the output of this PyTorch code snippet?
PyTorch
import torch
x = torch.tensor([1.0, 2.0, 3.0])
if torch.cuda.is_available():
    x = x.to('cuda')
print(x.device)
AError: device not found
Bcpu
Ccuda:1
Dcuda:0
Attempts:
2 left
💡 Hint
Check if CUDA is available and where the tensor is moved.
❓ Model Choice
advanced
2:00remaining
Choosing tensor shape for image data
You want to input a batch of 32 RGB images of size 64x64 into a PyTorch model. Which tensor shape correctly represents this batch?
A[32, 3, 64, 64]
B[3, 32, 64, 64]
C[64, 64, 3, 32]
D[32, 64, 64, 3]
Attempts:
2 left
💡 Hint
PyTorch expects batch size first, then channels, then height and width.
❓ Hyperparameter
advanced
2:00remaining
Effect of tensor dtype on model training
How does changing a tensor's data type (dtype) from float32 to float64 affect PyTorch model training?
AIt has no effect on training speed or memory.
BIt speeds up training and reduces memory usage.
CIt increases precision but slows down training and uses more memory.
DIt causes the model to ignore gradients during backpropagation.
Attempts:
2 left
💡 Hint
Consider the trade-off between precision and computational resources.
🔧 Debug
expert
2:00remaining
Identifying error in tensor operation
What error will this PyTorch code raise?
PyTorch
import torch
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5])
z = x + y
ARuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 0
BTypeError: unsupported operand type(s) for +: 'int' and 'list'
CValueError: operands could not be broadcast together with shapes (3,) (2,)
DNo error, output tensor is [5, 7, 3]
Attempts:
2 left
💡 Hint
Check if the tensors have the same shape for element-wise addition.

Practice

(1/5)
1. Why are tensors considered the core data structure in PyTorch?
easy
A. Because they are the only way to create variables in Python
B. Because they efficiently store and process numerical data for machine learning
C. Because they are only used for storing text data
D. Because they replace Python lists for all programming tasks

Solution

  1. Step 1: Understand the role of tensors in PyTorch

    Tensors are designed to hold numbers and perform math operations efficiently, which is essential for machine learning.
  2. Step 2: Compare tensors with other data types

    Unlike text or general variables, tensors are optimized for numerical computation on CPUs and GPUs.
  3. Final Answer:

    Because they efficiently store and process numerical data for machine learning -> Option B
  4. Quick Check:

    Tensors = Core numerical data structure [OK]
Hint: Tensors store numbers for fast math in ML [OK]
Common Mistakes:
  • Thinking tensors store only text
  • Confusing tensors with Python variables
  • Assuming tensors replace all Python data types
2. Which of the following is the correct way to create a tensor in PyTorch?
easy
A. torch.array([1, 2, 3])
B. torch.list([1, 2, 3])
C. torch.tensor([1, 2, 3])
D. torch.create([1, 2, 3])

Solution

  1. Step 1: Recall PyTorch tensor creation syntax

    The correct function to create a tensor from a list is torch.tensor().
  2. Step 2: Identify incorrect options

    Functions like torch.list(), torch.array(), and torch.create() do not exist in PyTorch.
  3. Final Answer:

    torch.tensor([1, 2, 3]) -> Option C
  4. Quick Check:

    Use torch.tensor() to create tensors [OK]
Hint: Use torch.tensor() to make tensors [OK]
Common Mistakes:
  • Using non-existent torch.list()
  • Confusing with numpy.array()
  • Trying torch.create() which is invalid
3. What will be the output of this PyTorch code?
import torch
x = torch.tensor([1, 2, 3])
y = x + 5
print(y)
medium
A. Error: cannot add int to tensor
B. [6, 7, 8]
C. tensor([1, 2, 3, 5])
D. tensor([6, 7, 8])

Solution

  1. Step 1: Understand tensor addition with a scalar

    Adding 5 to tensor x adds 5 to each element, resulting in [6, 7, 8].
  2. Step 2: Check the print output format

    PyTorch prints tensors with the 'tensor()' wrapper, so output is tensor([6, 7, 8]).
  3. Final Answer:

    tensor([6, 7, 8]) -> Option D
  4. Quick Check:

    Tensor + scalar adds to each element [OK]
Hint: Adding scalar to tensor adds to each element [OK]
Common Mistakes:
  • Expecting a Python list output
  • Thinking addition concatenates elements
  • Assuming error when adding int to tensor
4. Identify the error in this PyTorch code snippet:
import torch
x = torch.tensor([1, 2, 3])
y = x + '5'
print(y)
medium
A. Cannot add a string to a tensor
B. Tensor must be created with floats
C. Missing import statement
D. Tensor size mismatch error

Solution

  1. Step 1: Analyze the addition operation

    The code tries to add a string '5' to a tensor, which is not allowed.
  2. Step 2: Understand PyTorch type rules

    PyTorch tensors support addition with numbers, not strings, causing a TypeError.
  3. Final Answer:

    Cannot add a string to a tensor -> Option A
  4. Quick Check:

    Tensor + string causes error [OK]
Hint: Tensors add only numbers, not strings [OK]
Common Mistakes:
  • Thinking tensor needs floats only
  • Ignoring type mismatch in addition
  • Assuming missing import causes error
5. You want to speed up your machine learning model training by using GPU. Why is using tensors important in PyTorch for this?
hard
A. Because tensors can be moved to GPU memory for fast math operations
B. Because tensors automatically convert code to GPU instructions without user action
C. Because tensors store data as strings which GPUs process faster
D. Because tensors replace the need for GPUs entirely

Solution

  1. Step 1: Understand tensor compatibility with hardware

    Tensors can be moved to GPU memory, allowing fast parallel math operations.
  2. Step 2: Clarify user control over device placement

    Users must explicitly move tensors to GPU; tensors do not auto-convert code.
  3. Final Answer:

    Because tensors can be moved to GPU memory for fast math operations -> Option A
  4. Quick Check:

    Tensors + GPU = faster ML math [OK]
Hint: Move tensors to GPU for speed [OK]
Common Mistakes:
  • Thinking tensors auto-use GPU without code
  • Believing tensors store strings for speed
  • Assuming tensors replace GPUs
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