Tensors are like containers for numbers. Knowing their shapes and dimensions helps us organize and use data correctly in machine learning.
Tensor shapes and dimensions in PyTorch
tensor.shape
# or
tensor.size()tensor.shape and tensor.size() both give the size of each dimension.
Dimensions are counted from 0, where 0 means a single number (scalar), 1 means a list (vector), 2 means a table (matrix), and so on.
import torch x = torch.tensor(5) print(x.shape)
x = torch.tensor([1, 2, 3]) print(x.shape)
x = torch.tensor([[1, 2], [3, 4]]) print(x.shape)
x = torch.randn(4, 3, 2) print(x.shape)
This program creates tensors with different dimensions and prints their shapes to show how tensor sizes look in PyTorch.
import torch # Create different tensors scalar = torch.tensor(7) vector = torch.tensor([1, 2, 3, 4]) matrix = torch.tensor([[1, 2], [3, 4], [5, 6]]) three_d = torch.randn(2, 3, 4) # Print their shapes print(f"Scalar shape: {scalar.shape}") print(f"Vector shape: {vector.shape}") print(f"Matrix shape: {matrix.shape}") print(f"3D tensor shape: {three_d.shape}")
Remember that torch.Size is like a tuple showing the size of each dimension.
Changing tensor shapes without changing data is called reshaping, which is useful but must keep the total number of elements the same.
Tensors have shapes that tell how many elements are in each dimension.
Use tensor.shape or tensor.size() to check tensor dimensions.
Understanding shapes helps avoid errors and correctly prepare data for models.
Practice
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 AQuick Check:
Tensor shape = elements per dimension [OK]
- Confusing shape with data type
- Thinking shape shows memory size
- Mixing shape with device location
t in PyTorch?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.shapeis an attribute, not a method, sot.shape()is incorrect.shape(t)andt.get_shape()are not valid PyTorch syntax.Final Answer:
t.size() -> Option CQuick Check:
Use t.size() to get shape [OK]
- Calling t.shape() as a method
- Using non-existent functions like shape(t)
- Trying t.get_shape() which is invalid
torch.randn(3, 4, 5)?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 BQuick Check:
Shape matches input sizes (3,4,5) [OK]
- Mixing up dimension order
- Assuming shape is reversed
- Confusing shape with total elements
import torch t = torch.randn(2, 3) print(t.shape())
What error will this code produce?
Solution
Step 1: Identify usage of t.shape()
In PyTorch,t.shapeis an attribute, not a method, so callingt.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 AQuick Check:
t.shape is attribute, not method [OK]
- Calling t.shape() instead of t.shape
- Expecting no error from t.shape()
- Confusing AttributeError with TypeError
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?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 dimsStep 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 DQuick Check:
5 * 30 = 150 elements [OK]
- Ignoring total element count when reshaping
- Choosing shape with wrong total elements
- Confusing number of dimensions required
Ready to prove it?
Pass the test and this pattern moves unknown
