Complete the code to create a 2D tensor with TensorFlow.
import tensorflow as tf matrix = tf.constant([1]) print(matrix)
The tf.constant function creates a tensor. To make a 2D tensor (matrix), you pass a nested list like [[1, 2], [3, 4]].
Complete the code to get the shape of a tensor.
import tensorflow as tf tensor = tf.constant([[1, 2], [3, 4], [5, 6]]) shape = tensor.[1] print(shape)
size which returns total elements, not shape.rank which returns the number of dimensions.The shape property returns the dimensions of the tensor, like rows and columns.
Fix the error in the code to multiply two tensors element-wise.
import tensorflow as tf a = tf.constant([1, 2, 3]) b = tf.constant([4, 5, 6]) result = a [1] b print(result)
@ performs matrix multiplication (dot product) instead of element-wise.+ or - changes the operation to addition or subtraction.Using * multiplies tensors element-wise. The @ operator is for matrix multiplication (dot product for 1D tensors), not element-wise.
Fill both blanks to create a tensor of zeros with shape (3, 4) and type float32.
import tensorflow as tf zeros_tensor = tf.[1](shape=[2], dtype=tf.float32) print(zeros_tensor)
tf.ones instead of tf.zeros.tf.zeros creates a tensor filled with zeros. The shape must be a tuple like (3, 4) to specify rows and columns.
Fill all three blanks to create a tensor from a list, reshape it to (2, 3), and print its new shape.
import tensorflow as tf list_data = [1, 2, 3, 4, 5, 6] tensor = tf.constant([1]) reshaped = tf.reshape(tensor, [2]) print(reshaped.[3])
reshaped.shape() (it's a property, not a method).First, create a tensor from list_data. Then reshape it to a tuple (2, 3). Finally, use shape to print the new dimensions.