Complete the code to create a tensor of shape (2, 3) filled with zeros.
import tensorflow as tf zeros_tensor = tf.zeros([1]) print(zeros_tensor.shape)
The tf.zeros function expects the shape as a tuple. So (2, 3) correctly creates a tensor with 2 rows and 3 columns.
Complete the code to reshape a tensor of shape (6,) into shape (2, 3).
import tensorflow as tf original = tf.constant([1, 2, 3, 4, 5, 6]) reshaped = tf.reshape(original, [1]) print(reshaped.shape)
The tf.reshape function expects the new shape as a tuple. So (2, 3) reshapes the tensor into 2 rows and 3 columns.
Fix the error in the code to get the shape of a tensor correctly.
import tensorflow as tf tensor = tf.constant([[1, 2], [3, 4]]) shape = tensor.[1]() print(shape)
shape() which is not a method.get_shape() which returns a TensorShape object but is less common in eager execution.In TensorFlow, shape is a property that returns the shape of the tensor. Using tensor.shape correctly gets the shape.
Fill both blanks to create a tensor of shape (3, 4) and then reshape it to (4, 3).
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])
The initial tensor has shape (3, 4). We reshape it to (4, 3). The print statement checks if the reshaped tensor's shape equals the original shape (3, 4).
Fill all three blanks to create a tensor, reshape it, and check if the reshaped tensor has the expected shape.
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])
The tensor is reshaped to (2, 3). The expected shape is (3, 2). The print checks if the reshaped tensor's shape matches the expected shape and if the first dimension is 2.
Note: The print will output False because the shapes differ, showing the importance of matching shapes.