0
0
TensorFlowml~15 mins

Broadcasting rules in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Broadcasting rules
Problem:You want to add two tensors of different shapes using TensorFlow, but the shapes are not compatible without broadcasting. The current operation throws an error or produces unexpected results.
Current Metrics:Operation error or incorrect output shape due to incompatible tensor shapes.
Issue:The tensors do not follow TensorFlow's broadcasting rules, causing shape mismatch errors or wrong results.
Your Task
Apply TensorFlow broadcasting rules correctly to add two tensors of different shapes without errors and get the expected output shape.
Do not manually reshape tensors to the same shape using tf.reshape.
Use only TensorFlow operations and broadcasting rules.
Keep the original data values unchanged.
Hint 1
Hint 2
Hint 3
Solution
TensorFlow
import tensorflow as tf

# Tensor A shape (3, 1)
A = tf.constant([[1], [2], [3]], dtype=tf.float32)
# Tensor B shape (1, 4)
B = tf.constant([[10, 20, 30, 40]], dtype=tf.float32)

# Add tensors using broadcasting
C = A + B

print('Tensor A shape:', A.shape)
print('Tensor B shape:', B.shape)
print('Result shape:', C.shape)
print('Result tensor:\n', C.numpy())
Used tensors with shapes (3,1) and (1,4) which follow broadcasting rules.
Added tensors directly without reshaping, relying on TensorFlow's automatic broadcasting.
Ensured dimensions are compatible: 1 can broadcast to 3 and 1 can broadcast to 4.
Results Interpretation

Before: Attempting to add tensors with incompatible shapes caused errors or wrong results.

After: Using broadcasting rules, tensors with shapes (3,1) and (1,4) added successfully to produce a (3,4) tensor.

Broadcasting lets you perform operations on tensors of different shapes by automatically expanding dimensions of size 1 to match the other tensor, simplifying code and avoiding manual reshaping.
Bonus Experiment
Try adding a tensor of shape (3,) to a tensor of shape (3,4) using broadcasting and observe the result.
💡 Hint
Remember that a shape (3,) tensor is treated as (3,) and broadcasting rules apply from the right; try expanding dims to (3,1) or (1,3) to enable broadcasting as needed.