Bird
Raised Fist0
TensorFlowml~10 mins

Tensor shapes and reshaping in TensorFlow - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a tensor of shape (2, 3) filled with zeros.

TensorFlow
import tensorflow as tf

zeros_tensor = tf.zeros([1])
print(zeros_tensor.shape)
Drag options to blanks, or click blank then click option'
A3, 2
B[2, 3]
C2, 3
D(2, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple for the shape.
Passing dimensions without parentheses.
2fill in blank
medium

Complete the code to reshape a tensor of shape (6,) into shape (2, 3).

TensorFlow
import tensorflow as tf

original = tf.constant([1, 2, 3, 4, 5, 6])
reshaped = tf.reshape(original, [1])
print(reshaped.shape)
Drag options to blanks, or click blank then click option'
A[2, 3]
B(2, 3)
C2, 3
D[3, 2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple for the shape.
Passing dimensions without parentheses.
3fill in blank
hard

Fix the error in the code to get the shape of a tensor correctly.

TensorFlow
import tensorflow as tf

tensor = tf.constant([[1, 2], [3, 4]])
shape = tensor.[1]()
print(shape)
Drag options to blanks, or click blank then click option'
Ashape
Bget_shape()
Cshape()
Dget_shape
Attempts:
3 left
💡 Hint
Common Mistakes
Using shape() which is not a method.
Using get_shape() which returns a TensorShape object but is less common in eager execution.
4fill in blank
hard

Fill both blanks to create a tensor of shape (3, 4) and then reshape it to (4, 3).

TensorFlow
import tensorflow as tf

initial_tensor = tf.constant([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
reshaped_tensor = tf.reshape(initial_tensor, [1])
print(reshaped_tensor.shape == [2])
Drag options to blanks, or click blank then click option'
A(4, 3)
B(3, 4)
C[4, 3]
D[3, 4]
Attempts:
3 left
💡 Hint
Common Mistakes
Using lists instead of tuples for shapes.
Mixing up the order of dimensions.
5fill in blank
hard

Fill all three blanks to create a tensor, reshape it, and check if the reshaped tensor has the expected shape.

TensorFlow
import tensorflow as tf

original = tf.constant([1, 2, 3, 4, 5, 6])
reshaped = tf.reshape(original, [1])
expected_shape = [2]
print(reshaped.shape == expected_shape and reshaped.shape[0] == [3])
Drag options to blanks, or click blank then click option'
A(2, 3)
B(3, 2)
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of dimensions in the shape.
Using lists instead of tuples for shapes.

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

  1. 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.
  2. Step 2: Differentiate shape from other properties

    Data type, memory address, and operations are different properties, not shape.
  3. Final Answer:

    The size of the tensor in each dimension -> Option A
  4. 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

  1. Step 1: Recall TensorFlow reshape syntax

    TensorFlow uses tf.reshape(tensor, new_shape) to reshape tensors.
  2. 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.
  3. Final Answer:

    tf.reshape(t, (2, 3)) -> Option A
  4. 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

  1. Step 1: Check original tensor shape

    The tensor t has shape (2, 3) because it has 2 rows and 3 columns.
  2. Step 2: Understand reshape operation

    Reshape changes the shape to (3, 2) without changing data count. The total elements remain 6.
  3. Final Answer:

    (3, 2) -> Option C
  4. 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

  1. Step 1: Count elements in original tensor

    The tensor t has 4 elements: [1, 2, 3, 4].
  2. Step 2: Check reshape target shape

    The target shape (3, 2) requires 6 elements (3*2=6), which does not match 4 elements available.
  3. Final Answer:

    The reshape shape (3, 2) does not match total elements -> Option D
  4. 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

  1. Step 1: Calculate total elements in original tensor

    Original shape is (4, 3, 2). Total elements = 4 * 3 * 2 = 24.
  2. Step 2: Find second dimension for reshape

    We want first dimension = 6. So second dimension = total elements / 6 = 24 / 6 = 4.
  3. Final Answer:

    4 -> Option B
  4. Quick 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