We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← JAX Foundations step 24 of 25
Explicit Broadcasting via broadcast_to
Give a vector a batch shape
Suppose a bias vector contains one value for each of three features.
To represent that same vector for four examples, broadcast its shape
from (3,) to (4, 3). Every row of the result has the same values.
jnp.broadcast_to(x, shape) makes the output shape explicit. In many
calculations you can instead let arithmetic broadcast automatically:
adding a (3,) bias to a (4, 3) array already applies it to each row.
Worked example
import jax.numpy as jnp
x = jnp.array([1.0, 2.0, 3.0])
y = jnp.broadcast_to(x, (4, 3))
# [[1., 2., 3.],
# [1., 2., 3.],
# [1., 2., 3.],
# [1., 2., 3.]]
Shapes align from the right. The existing length-three axis matches
the target's last axis. The new leading axis supplies the four rows.
More generally, an aligned input dimension must equal the target
dimension or be 1. For example, (1,) can broadcast to (2, 3),
but (2,) cannot broadcast to (2, 3).
Broadcasting describes values and shapes, not a guaranteed memory layout. Do not assume that a JAX broadcast is a NumPy-style zero-copy view or that it always allocates repeated data. Compilation can change how the result is represented or combine it with later operations.
Problem
Implement explicit_broadcast(x, target_shape) with jnp.broadcast_to.
xis a one-dimensional float32 array with shape(W,).target_shapeis a float32 array[H, W]of whole-number dimensions. Dimensions are nonnegative and the supplied width matchesx.- Return an array of shape
(H, W). Each of its rows equalsx.
For example, [5, 6] with target shape [3, 2] gives
[[5, 6], [5, 6], [5, 6]]. Zero rows or zero columns give an empty
output with the requested two-dimensional shape.
Shape values and JIT
In this exercise's eager call, convert both shape entries with int
and pass the tuple (H, W) to jnp.broadcast_to. This works because
the array values are concrete when the function runs.
Wrapping that function directly in jax.jit makes the shape array
dynamic. int cannot extract a Python integer from a traced value.
For a JIT version, change the interface to accept a Python tuple of
integers and mark it static, or fix the target shape in a compiled
helper. Making the original JAX array static does not work because
it is unhashable.
Broadcasting and repetition
jnp.tile repeats blocks of values. It can turn [1, 2] into
[1, 2, 1, 2]; broadcasting cannot expand a length-two axis to four.
Choose based on the shape operation you need, not a blanket claim
that one function always copies and the other never does.
JAX arrays are immutable. An update such as y.at[0, 0].set(9) returns
a new array value; it does not change x or the existing y.
The diagram shows how the vector's trailing axis aligns with the target.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Give a vector a batch shape
Suppose a bias vector contains one value for each of three features.
To represent that same vector for four examples, broadcast its shape
from (3,) to (4, 3). Every row of the result has the same values.
jnp.broadcast_to(x, shape) makes the output shape explicit. In many
calculations you can instead let arithmetic broadcast automatically:
adding a (3,) bias to a (4, 3) array already applies it to each row.
Worked example
import jax.numpy as jnp
x = jnp.array([1.0, 2.0, 3.0])
y = jnp.broadcast_to(x, (4, 3))
# [[1., 2., 3.],
# [1., 2., 3.],
# [1., 2., 3.],
# [1., 2., 3.]]
Shapes align from the right. The existing length-three axis matches
the target's last axis. The new leading axis supplies the four rows.
More generally, an aligned input dimension must equal the target
dimension or be 1. For example, (1,) can broadcast to (2, 3),
but (2,) cannot broadcast to (2, 3).
Broadcasting describes values and shapes, not a guaranteed memory layout. Do not assume that a JAX broadcast is a NumPy-style zero-copy view or that it always allocates repeated data. Compilation can change how the result is represented or combine it with later operations.
Match shapes from the right
Broadcasting lets an operation apply across compatible shapes without explicitly
constructing repeated inputs. Compare dimensions from the right: each pair must
be equal, or one must be 1. Missing leading dimensions behave like dimensions of 1.
At each position, equal sizes stay the same. If one size is 1, the result uses
the other size. This also covers empty dimensions: (0, 3) and (1, 3)
broadcast to (0, 3), not (1, 3).
For example, (4, 3) plus (3,) applies the same three-feature bias to all four
rows. Adding (4, 1) instead applies a different scalar to each row. A size-one axis lets the same value be reused along that axis.
This explains a subtle centring bug: reducing (B, D) over features gives (B,).
Subtracting it directly aligns B with the feature axis. If B == D, the operation
runs but uses the wrong means. Retaining the reduction axis as (B, 1) expresses
the intended dependence.
Check how much memory the result needs
The input need not be physically copied, but the output can still be huge:
(N, 1, D) minus (1, M, D) has shape (N, M, D). An in-place operation must fit its result in the existing destination shape. Explicit expansion may also create a view
whose positions refer to the same storage, making independent writes unsafe.
Before implementing an operation, write the aligned shapes and the result shape. Then check which inputs are reused and how much space the output needs.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.