0
0
TensorFlowml~10 mins

Tensor shapes and reshaping in TensorFlow - Interactive Code Practice

Choose your learning style9 modes available
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.