A tensor is a multi-dimensional array used to store data. It can have any number of dimensions, like a list (1D), matrix (2D), or higher.
Click to reveal answer
beginner
What does the shape of a tensor represent?
The shape shows the size of the tensor in each dimension. For example, shape (3, 4) means 3 rows and 4 columns in a 2D tensor.
Click to reveal answer
beginner
How do you get the number of dimensions of a tensor in PyTorch?
Use the `.dim()` method. It returns how many dimensions the tensor has.
Click to reveal answer
beginner
What is the difference between a 1D and 2D tensor?
A 1D tensor is like a list of numbers (shape like [5]). A 2D tensor is like a table or matrix with rows and columns (shape like [3, 4]).
Click to reveal answer
intermediate
How can you change the shape of a tensor without changing its data?
You can use the `.view()` or `.reshape()` methods to change the shape while keeping the same data.
Click to reveal answer
What does the shape (2, 3, 4) of a tensor mean?
A2 rows, 3 columns, and 4 layers
B2 matrices, each with 3 rows and 4 columns
CA 2D tensor with 3 rows and 4 columns
DA 1D tensor with 24 elements
✗ Incorrect
Shape (2, 3, 4) means 2 blocks (matrices), each having 3 rows and 4 columns.
Which PyTorch method returns the number of dimensions of a tensor?
A.dim()
B.shape
C.size()
D.length()
✗ Incorrect
The `.dim()` method returns the number of dimensions of a tensor.
If a tensor has shape (5,), what kind of tensor is it?
A1D tensor
B3D tensor
C2D tensor
DScalar
✗ Incorrect
Shape (5,) means a 1D tensor with 5 elements.
Which method can you use to change the shape of a tensor without changing its data?
A.change_shape()
B.convert()
C.reshape()
D.resize()
✗ Incorrect
The `.reshape()` method changes the shape without altering the data.
What will `tensor.shape` return for a tensor with 3 rows and 4 columns?
A(4, 3)
B7
C12
D(3, 4)
✗ Incorrect
The shape is (3, 4) meaning 3 rows and 4 columns.
Explain what tensor shape and dimensions mean in PyTorch and why they matter.
Think about how data is organized in rows, columns, and layers.
You got /3 concepts.
Describe how to check and change the shape of a tensor in PyTorch with code examples.
Show how to get shape and dimension, then reshape a tensor.
You got /4 concepts.
Practice
(1/5)
1. What does the shape of a PyTorch tensor represent?
easy
A. The number of elements in each dimension of the tensor
B. The data type of the tensor elements
C. The device where the tensor is stored
D. The memory size of the tensor in bytes
Solution
Step 1: Understand tensor shape meaning
The shape of a tensor tells how many elements are in each dimension, like rows and columns in a matrix.
Step 2: Differentiate from other properties
Data type, device, and memory size are different properties and not related to shape.
Final Answer:
The number of elements in each dimension of the tensor -> Option A
Quick Check:
Tensor shape = elements per dimension [OK]
Hint: Shape = counts of elements per dimension [OK]
Common Mistakes:
Confusing shape with data type
Thinking shape shows memory size
Mixing shape with device location
2. Which of the following is the correct way to get the shape of a tensor t in PyTorch?
easy
A. shape(t)
B. t.shape()
C. t.size()
D. t.get_shape()
Solution
Step 1: Recall PyTorch shape methods
In PyTorch, t.size() returns the shape of the tensor as a tuple-like object.
Step 2: Check other options
t.shape is an attribute, not a method, so t.shape() is incorrect. shape(t) and t.get_shape() are not valid PyTorch syntax.
Final Answer:
t.size() -> Option C
Quick Check:
Use t.size() to get shape [OK]
Hint: Use t.size() method to get shape [OK]
Common Mistakes:
Calling t.shape() as a method
Using non-existent functions like shape(t)
Trying t.get_shape() which is invalid
3. What is the shape of the tensor created by torch.randn(3, 4, 5)?
medium
A. (3, 5, 4)
B. (3, 4, 5)
C. (5, 3, 4)
D. (4, 5, 3)
Solution
Step 1: Understand torch.randn arguments
The arguments (3, 4, 5) specify the size of each dimension in order: first dimension 3, second 4, third 5.
Step 2: Confirm shape matches arguments
The tensor shape matches exactly the arguments given, so the shape is (3, 4, 5).
Final Answer:
(3, 4, 5) -> Option B
Quick Check:
Shape matches input sizes (3,4,5) [OK]
Hint: Shape matches torch.randn arguments order [OK]
Common Mistakes:
Mixing up dimension order
Assuming shape is reversed
Confusing shape with total elements
4. Consider this code snippet:
import torch
t = torch.randn(2, 3)
print(t.shape())
What error will this code produce?
medium
A. TypeError: 'torch.Size' object is not callable
B. AttributeError: 'Tensor' object has no attribute 'shape'
C. SyntaxError: invalid syntax
D. No error, prints (2, 3)
Solution
Step 1: Identify usage of t.shape()
In PyTorch, t.shape is an attribute, not a method, so calling t.shape() tries to call a non-callable object.
Step 2: Understand error type
This causes a TypeError saying the 'torch.Size' object is not callable.
Final Answer:
TypeError: 'torch.Size' object is not callable -> Option A
Quick Check:
t.shape is attribute, not method [OK]
Hint: t.shape is attribute, don't add () [OK]
Common Mistakes:
Calling t.shape() instead of t.shape
Expecting no error from t.shape()
Confusing AttributeError with TypeError
5. You have a tensor x with shape (10, 5, 3). You want to reshape it to a 2D tensor with 5 rows and keep the total number of elements the same. Which of the following shapes is valid for the reshaped tensor?
hard
A. (5, 15)
B. (5, 10, 3)
C. (5, 5, 6)
D. (5, 30)
Solution
Step 1: Calculate total elements in original tensor
Original shape is (10, 5, 3), so total elements = 10 * 5 * 3 = 150.
Step 2: Check which new shape keeps total elements and has 5 rows
We want 5 rows, so first dimension = 5. The second dimension must satisfy 5 * ? = 150, so ? = 30. Check options: - A: (5, 15) -> 5*15=75 invalid total elements - B: (5, 10, 3) -> 5*10*3=150 valid but has 3 dims, not 2 - C: (5, 30) -> 5*30=150 valid - D: (5, 5, 6) -> 5*5*6=150 valid but 3 dims
Step 3: Confirm valid shape with 5 rows and total elements
(5, 30) is (5, 30) which matches 5 rows and total elements 150. (5, 15) is incorrect because 5*15=75, less than 150.
Final Answer:
(5, 30) -> Option D
Quick Check:
5 * 30 = 150 elements [OK]
Hint: Total elements must stay same; multiply dims to check [OK]
Common Mistakes:
Ignoring total element count when reshaping
Choosing shape with wrong total elements
Confusing number of dimensions required
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.