Tensors are like boxes holding numbers. Knowing their shape helps us organize data. Reshaping changes the box size without changing the numbers inside.
Tensor shapes and reshaping in TensorFlow
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
TensorFlow
tensor.shape reshaped_tensor = tf.reshape(tensor, new_shape)
tensor.shape shows the size of each dimension.
tf.reshape changes the shape but keeps the same data.
Examples
TensorFlow
import tensorflow as tf # Create a tensor of shape (2, 3) t = tf.constant([[1, 2, 3], [4, 5, 6]]) print(t.shape)
TensorFlow
reshaped = tf.reshape(t, (3, 2)) print(reshaped)
-1 means 'figure out this size automatically.'TensorFlow
flattened = tf.reshape(t, [-1]) print(flattened)
Sample Model
This program shows how to check a tensor's shape, reshape it to a new shape, and flatten it to 1D. The numbers inside stay the same, only the shape changes.
TensorFlow
import tensorflow as tf # Original tensor: 2 rows, 3 columns t = tf.constant([[10, 20, 30], [40, 50, 60]]) print(f"Original shape: {t.shape}") print(f"Original tensor:\n{t.numpy()}") # Reshape to 3 rows, 2 columns reshaped = tf.reshape(t, (3, 2)) print(f"\nReshaped shape: {reshaped.shape}") print(f"Reshaped tensor:\n{reshaped.numpy()}") # Flatten to 1D flattened = tf.reshape(t, [-1]) print(f"\nFlattened shape: {flattened.shape}") print(f"Flattened tensor:\n{flattened.numpy()}")
Important Notes
When reshaping, the total number of elements must stay the same.
Use -1 in tf.reshape to let TensorFlow calculate that dimension automatically.
Checking shapes helps avoid errors when feeding data to models.
Summary
Tensors have shapes that describe their size in each dimension.
Reshaping changes the shape but keeps the same data.
Use tensor.shape to see shape and tf.reshape to change it.
Practice
1. What does the shape of a tensor represent in TensorFlow?
easy
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 AQuick 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
Solution
Step 1: Recall TensorFlow reshape syntax
TensorFlow usestf.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 AQuick 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
Solution
Step 1: Check original tensor shape
The tensorthas 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 CQuick 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
Solution
Step 1: Count elements in original tensor
The tensorthas 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 DQuick 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
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 BQuick Check:
New shape dims multiply to total elements [OK]
Hint: Divide total elements by known dimension to find missing one [OK]
Common Mistakes:
- Multiplying instead of dividing total elements
- Forgetting to multiply all original dimensions
- Choosing wrong option without calculation
