0
0
TensorFlowml~10 mins

Broadcasting rules 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 add two tensors using broadcasting.

TensorFlow
import tensorflow as tf

a = tf.constant([1, 2, 3])
b = tf.constant([[10], [20], [30]])
result = a [1] b
print(result.numpy())
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction (-) instead of addition (+).
Trying to multiply (*) when the task is to add.
2fill in blank
medium

Complete the code to reshape a tensor for broadcasting.

TensorFlow
import tensorflow as tf

x = tf.constant([1, 2, 3])
x_reshaped = tf.reshape(x, [1])
print(x_reshaped.shape)
Drag options to blanks, or click blank then click option'
A(1, 3)
B(3, 1)
C(3,)
D(1,)
Attempts:
3 left
💡 Hint
Common Mistakes
Using (1, 3) which changes the shape incorrectly.
Using (3,) which is the original shape and may not broadcast as intended.
3fill in blank
hard

Fix the error in the code to correctly broadcast and multiply tensors.

TensorFlow
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())
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition (+) instead of multiplication (*).
Trying to divide (/) which is not the intended operation.
4fill in blank
hard

Fill in the blank to create a tensor and broadcast it for addition.

TensorFlow
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])
y = tf.constant([1])
result = x + y
print(result.numpy())
Drag options to blanks, or click blank then click option'
A[10, 20, 30]
B[10, 20]
C[[10, 20, 30]]
D[[10], [20]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a 1D list like [10, 20] which has shape (2,) and may not broadcast as intended.
Using a list with shape (1, 3) which does not match x's shape for broadcasting.
5fill in blank
hard

Fill in the blank to create a tensor and broadcast it for multiplication.

TensorFlow
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])
y = tf.constant([1])
result = x * y
print(result.numpy())
Drag options to blanks, or click blank then click option'
A[[10], [20]]
B[10, 20]
C[[1, 2, 3]]
D[[10, 20, 30]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a 1D list like [10, 20] which won't broadcast correctly.
Using a shape that does not match for broadcasting.