Complete the code to add two tensors using broadcasting.
import tensorflow as tf a = tf.constant([1, 2, 3]) b = tf.constant([[10], [20], [30]]) result = a [1] b print(result.numpy())
Using the plus sign (+) adds the two tensors element-wise with broadcasting.
Complete the code to reshape a tensor for broadcasting.
import tensorflow as tf x = tf.constant([1, 2, 3]) x_reshaped = tf.reshape(x, [1]) print(x_reshaped.shape)
Reshaping to (3, 1) prepares the tensor for broadcasting with a (3, 4) tensor along the second dimension.
Fix the error in the code to correctly broadcast and multiply tensors.
import tensorflow as tf x = tf.constant([[1, 2, 3], [4, 5, 6]]) y = tf.constant([10, 20]) result = x [1] y print(result.numpy())
The multiplication operator (*) correctly broadcasts y to match x's shape for element-wise multiplication.
Fill in the blank to create a tensor and broadcast it for addition.
import tensorflow as tf x = tf.constant([[1, 2, 3], [4, 5, 6]]) y = tf.constant([1]) result = x + y print(result.numpy())
Using [[10], [20]] allows broadcasting along the columns to add to x.
Fill in the blank to create a tensor and broadcast it for multiplication.
import tensorflow as tf x = tf.constant([[1, 2, 3], [4, 5, 6]]) y = tf.constant([1]) result = x * y print(result.numpy())
y is [[10], [20]] to broadcast along columns; x shape is (2,3); multiplication works element-wise.