Skip to main content

How to Use Broadcasting in TensorFlow

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Doubling every price in a table, adding a fee per product, or discounting one shop's prices: each combines a big tensor with a smaller one. TensorFlow does that without copying the small one by hand. It stretches it to fit, which is called broadcasting. This guide uses it three ways in TensorFlow 2.22.0, and shows the rule that decides when it works.

Syntax​

Syntax
<tensor> <operator> <smaller tensor>
PartWhat it means
<tensor>The larger tensor, such as a table with the shape (2, 3)
<operator>Any element-wise operation: +, -, *, /
<smaller tensor>A number, a list or a tensor whose shape fits the rule below

The tensor guide explains the idea: "under certain conditions, smaller tensors are "stretched" automatically to fit larger tensors when running combined operations on them." The tf.math.add API page gives the rule: "The two input array shapes are compared element-wise. Starting with the trailing dimensions, the two dimensions either have to be equal or one of them needs to be 1." Trailing means starting from the last axis.

The Example​

The prices of three products in two shops: one row per shop, one column per product. The script doubles every price, adds a different fee to each product, and gives the second shop a 50% discount.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

broadcast.py
import tensorflow as tf

# Prices of three products (columns) in two shops (rows)
prices = tf.constant([[10.0, 20.0, 30.0], [40.0, 50.0, 60.0]])

print(prices * 2)
print(prices + [1.0, 2.0, 3.0])
print(prices * [[1.0], [0.5]])

The broadcast.py script in a macOS-style code window: a prices table of two shops by three products, then prices * 2, prices + [1.0, 2.0, 3.0], and prices * [[1.0], [0.5]].

OperationSmall shapeStretched toWhat it does
prices * 2()(2, 3)Every price times 2
prices + [1.0, 2.0, 3.0](3,)(2, 3)The row is repeated for each shop: one fee per product
prices * [[1.0], [0.5]](2, 1)(2, 3)The column is repeated for each product: one factor per shop

Step 2: Run It​

Terminal
uv run python broadcast.py
Output
tf.Tensor(
[[ 20. 40. 60.]
[ 80. 100. 120.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[11. 22. 33.]
[41. 52. 63.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[10. 20. 30.]
[20. 25. 30.]], shape=(2, 3), dtype=float32)

The terminal after uv run python broadcast.py: every price doubled to 20 40 60 and 80 100 120, the fees added to give 11 22 33 and 41 52 63, and the second shop halved to 20 25 30.

All three results have the table's shape, (2, 3). The 2 went to every price. The fees 1, 2, 3 went to both shops' rows. The factors went down the rows: shop 1's prices stayed the same, and shop 2's were halved to 20 25 30. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
A list of per-shop factors as a row: prices * [1.0, 0.5]InvalidArgumentError: … Incompatible shapes: [2,3] vs. [2] [Op:Mul]. The shapes are compared from the end: 3 against 2, neither equal nor 1Make it a column: [[1.0], [0.5]] has the shape (2, 1)

FAQ​

Does broadcasting copy the small tensor in memory?​

The tensor guide says it doesn't have to: "Most of the time, broadcasting is both time and space efficient, as the broadcast operation never materializes the expanded tensors in memory."

How do I see the stretched tensor?​

tf.broadcast_to(<tensor>, <shape>) returns it, and the guide uses it to show what broadcasting looks like. Unlike + or *, it really builds the bigger tensor: the guide notes that "broadcast_to does nothing special to save memory."

Conclusion​

Broadcasting let a number, a row and a column combine with a (2, 3) table, each stretched to fit. Shapes are compared from the last axis, and each pair of lengths must be equal or one of them 1. A per-row list has to be a column, (2, 1), to fit.

Additional Resources​