Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a tensor shape in TensorFlow?
A tensor shape is a list of integers that describes the size of the tensor in each dimension. For example, a shape of [3, 4] means the tensor has 3 rows and 4 columns.
Click to reveal answer
beginner
How do you reshape a tensor in TensorFlow?
You use the tf.reshape() function, which changes the shape of a tensor without changing its data. You provide the tensor and the new shape you want.
Click to reveal answer
intermediate
What does the '-1' argument mean in tf.reshape()?
The '-1' tells TensorFlow to automatically calculate the size of that dimension based on the other dimensions and the total number of elements.
Click to reveal answer
beginner
Why is it important to keep the total number of elements the same when reshaping?
Because reshaping only changes the shape, not the data. The total number of elements must stay the same to avoid errors and keep data consistent.
Click to reveal answer
beginner
What is the shape of a tensor created by tf.constant([[1, 2], [3, 4], [5, 6]])?
The shape is [3, 2] because there are 3 rows and 2 columns.
Click to reveal answer
What does tf.reshape(tensor, [-1, 2]) do?
AReshapes tensor to have 2 columns and automatically calculates rows
BReshapes tensor to have 2 rows and automatically calculates columns
CFlattens the tensor into a 1D array
DRaises an error because -1 is invalid
✗ Incorrect
The '-1' lets TensorFlow calculate the number of rows automatically, while 2 sets the number of columns.
If a tensor has shape [4, 5], how many elements does it have?
A9
B20
C10
DNone of the above
✗ Incorrect
Multiply the dimensions: 4 * 5 = 20 elements.
Which function is used to check the shape of a tensor in TensorFlow?
Atf.size()
Btf.rank()
Ctf.reshape()
Dtf.shape()
✗ Incorrect
tf.shape() returns the shape of the tensor as a tensor itself.
What happens if you try to reshape a tensor to a shape with a different total number of elements?
ATensorFlow raises an error
BTensorFlow automatically adjusts the data
CTensorFlow fills missing values with zeros
DTensorFlow ignores extra elements
✗ Incorrect
Reshaping requires the total number of elements to stay the same; otherwise, TensorFlow raises an error.
What is the shape of a scalar tensor in TensorFlow?
A[0]
B[1]
C[] (empty shape)
D[1,1]
✗ Incorrect
A scalar has no dimensions, so its shape is an empty list [].
Explain what tensor shape means and why it is important in TensorFlow.
Think about how shape tells you the size and layout of data.
You got /4 concepts.
Describe how to reshape a tensor and what rules must be followed during reshaping.
Consider how reshaping changes layout but not data.
You got /4 concepts.
Practice
(1/5)
1. What does the shape of a tensor represent in TensorFlow?
easy
A. The size of the tensor in each dimension
B. The data type of the tensor elements
C. The memory address of the tensor
D. The number of operations performed on the tensor
Solution
Step 1: Understand tensor shape meaning
The shape of a tensor tells us how many elements it has along each dimension, like rows and columns in a matrix.
Step 2: Differentiate shape from other properties
Data type, memory address, and operations are different properties, not shape.
Final Answer:
The size of the tensor in each dimension -> Option A
Quick Check:
Tensor shape = size per dimension [OK]
Hint: Shape means size per dimension, not data or memory [OK]
Common Mistakes:
Confusing shape with data type
Thinking shape is memory location
Mixing shape with number of operations
2. Which of the following is the correct way to reshape a tensor t to shape (2, 3) in TensorFlow?
easy
A. tf.reshape(t, (2, 3))
B. t.reshape(2, 3)
C. tf.change_shape(t, (2, 3))
D. reshape(t, (2, 3))
Solution
Step 1: Recall TensorFlow reshape syntax
TensorFlow uses tf.reshape(tensor, new_shape) to reshape tensors.
Step 2: Check each option
tf.reshape(t, (2, 3)) uses correct function and parameters. t.reshape(2, 3) is invalid because tensors don't have a reshape method. tf.change_shape(t, (2, 3)) uses a non-existent function. reshape(t, (2, 3)) misses the module prefix.
Final Answer:
tf.reshape(t, (2, 3)) -> Option A
Quick Check:
Use tf.reshape(t, shape) to reshape [OK]
Hint: Use tf.reshape(tensor, shape) to reshape tensors [OK]
Common Mistakes:
Using tensor.reshape() method which doesn't exist
Using wrong function name like tf.change_shape
Omitting tf module prefix
3. What will be the output shape of the following code?
import tensorflow as tf
t = tf.constant([[1, 2, 3], [4, 5, 6]])
t_reshaped = tf.reshape(t, (3, 2))
print(t_reshaped.shape)
medium
A. (2, 3)
B. (6,)
C. (3, 2)
D. (1, 6)
Solution
Step 1: Check original tensor shape
The tensor t has shape (2, 3) because it has 2 rows and 3 columns.
Step 2: Understand reshape operation
Reshape changes the shape to (3, 2) without changing data count. The total elements remain 6.
Final Answer:
(3, 2) -> Option C
Quick Check:
Reshape to (3, 2) changes shape accordingly [OK]
Hint: Reshape keeps total elements same, just changes shape [OK]
Common Mistakes:
Confusing original shape with reshaped shape
Assuming reshape flattens tensor
Mixing up rows and columns
4. Identify the error in the following TensorFlow code:
import tensorflow as tf
t = tf.constant([1, 2, 3, 4])
t_reshaped = tf.reshape(t, (3, 2))
print(t_reshaped)
medium
A. print statement syntax is incorrect
B. tf.constant cannot create 1D tensors
C. tf.reshape requires a list, not a tuple for shape
D. The reshape shape (3, 2) does not match total elements
Solution
Step 1: Count elements in original tensor
The tensor t has 4 elements: [1, 2, 3, 4].
Step 2: Check reshape target shape
The target shape (3, 2) requires 6 elements (3*2=6), which does not match 4 elements available.
Final Answer:
The reshape shape (3, 2) does not match total elements -> Option D
Quick Check:
Reshape shape must match total elements [OK]
Hint: Total elements before and after reshape must be equal [OK]
Common Mistakes:
Ignoring mismatch in total elements
Thinking tf.constant can't create 1D tensors
Believing shape must be list, not tuple
5. You have a tensor t with shape (4, 3, 2). You want to reshape it to a 2D tensor where the first dimension is 6. What should the second dimension be?
hard
A. 24
B. 4
C. 12
D. 8
Solution
Step 1: Calculate total elements in original tensor
Original shape is (4, 3, 2). Total elements = 4 * 3 * 2 = 24.
Step 2: Find second dimension for reshape
We want first dimension = 6. So second dimension = total elements / 6 = 24 / 6 = 4.
Final Answer:
4 -> Option B
Quick Check:
New shape dims multiply to total elements [OK]
Hint: Divide total elements by known dimension to find missing one [OK]