0
0
TensorFlowml~20 mins

Broadcasting rules in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Broadcasting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
TensorFlow Broadcasting Output Shape
Given the following TensorFlow code, what is the shape of the resulting tensor after the addition?
TensorFlow
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])  # shape (2, 3)
y = tf.constant([10, 20, 30])             # shape (3,)
result = x + y
result_shape = result.shape
A(2,)
B(3, 2)
C(2, 3)
D(3,)
Attempts:
2 left
💡 Hint
Remember how TensorFlow broadcasts smaller tensors to match larger ones by adding dimensions.
🧠 Conceptual
intermediate
2:00remaining
Understanding Broadcasting Compatibility
Which pair of tensor shapes can be broadcast together in TensorFlow without error?
A(5, 4) and (5, 4, 1)
B(4, 1, 3) and (3, 1)
C(2, 3) and (3, 2)
D(1, 2, 3) and (3, 1)
Attempts:
2 left
💡 Hint
Broadcasting compares shapes from the rightmost dimension to the left.
Hyperparameter
advanced
2:00remaining
Broadcasting in Tensor Operations
You have a tensor A of shape (8, 1, 6) and tensor B of shape (1, 5, 6). What will be the shape of A * B after broadcasting in TensorFlow?
A(8, 5, 6)
B(8, 5)
C(1, 5, 6)
D(8, 1, 6)
Attempts:
2 left
💡 Hint
Broadcasting expands dimensions where size is 1 to match the other tensor.
🔧 Debug
advanced
2:00remaining
Identifying Broadcasting Error
What error will this TensorFlow code raise due to broadcasting rules?
TensorFlow
import tensorflow as tf

x = tf.constant([[1, 2], [3, 4]])  # shape (2, 2)
y = tf.constant([1, 2, 3])          # shape (3,)
result = x + y
ANo error, result shape is (2, 3)
BValueError: operands could not be broadcast together with shapes (2,2) (3,)
CTypeError: unsupported operand type(s) for +: 'Tensor' and 'Tensor'
DInvalidArgumentError: Incompatible shapes: [2,2] vs. [3]
Attempts:
2 left
💡 Hint
Check if the dimensions can align from right to left for broadcasting.
Model Choice
expert
2:00remaining
Choosing Tensor Shapes for Efficient Broadcasting
You want to add two tensors efficiently in TensorFlow with minimal memory overhead. Which pair of shapes is best for broadcasting?
A(64, 1, 1) and (1, 32, 128)
B(64, 32, 128) and (64, 32, 128)
C(64, 32, 128) and (32, 128)
D(64, 1, 128) and (1, 32, 128)
Attempts:
2 left
💡 Hint
Smaller dimensions with 1 allow broadcasting without copying large data.