We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Batched Matrix Multiply
Multiply corresponding pairs of matrices in a batch. Pair A[0] with B[0], A[1] with B[1], and so on, without mixing batch entries.
Your task
Implement batched_matmul(A, B) for floating-point tensors with the same dtype
and device:
- A has shape
(batch, m, k). - B has shape
(batch, k, n)with the same batch size. - Return shape
(batch, m, n), preserving dtype and device. Leave inputs unchanged.
output[s, i, j] = sum(A[s, i, t] * B[s, t, j] for t from 0 to k-1)
Only the inner matrix dimension k is summed over. The leading batch dimension is retained, including when batch=1. No broadcasting between different batch sizes is required in this exercise.
For A=[[[1, 2]], [[3, 4]]] and B=[[[5], [6]], [[7], [8]]], return
[[[17]], [[53]]]: the first pair gives 1*5 + 2*6 = 17, and the second
gives 3*7 + 4*8 = 53. The output shape is (2, 1, 1), not (2,).
Use torch.bmm(A, B) or A @ B in PyTorch, and jnp.matmul(A, B) or
A @ B in JAX. Do not flatten the batch into the matrix dimensions, multiply
all possible pairs, or use elementwise A * B.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Multiply rows by columns
For A with shape (M, K) and B with shape (K, N), the matrix product has
shape (M, N). Each output entry is a dot product of one row of A and one
column of B:
$$C_{ij}=\sum_{k=1}^{K}A_{ik}B_{kj}.$$
For example, multiplying row [1, 2] by column [3, 4] gives
1 * 3 + 2 * 4 = 11. Elementwise multiplication would instead give [3, 8];
it does not include the sum.
If examples are rows of X, with shape (B, D), and weights have shape (D, H),
then XW produces H features for each of the B examples.
Keep batch axes separate from matrix axes
For batched matrix multiplication, the last two axes hold each matrix. Leading axes identify batches of matrices. A general matrix-multiply operation may broadcast compatible batch axes. A stricter batched operation may require them to match exactly. Use the operation specified by the exercise.
Einstein notation, often used with einsum, names the axes explicitly:
bij,bjk->bikmultiplies corresponding matrices in batchband sums overj.bi,bj->bijforms an outer product for each example: output(b, i, j)is the product of input(b, i)and input(b, j).
An axis is summed out when its label is absent from the explicit output.
A label that appears in both inputs is not necessarily summed: b stays in both
outputs above.
Choose a multiplication order with small intermediate results
In exact arithmetic, (AB)C = A(BC), but the two orders can have very different
costs. If the shapes are (1000, 10), (10, 10), and (10, 1), forming BC
first needs a (10, 1) intermediate. Forming AB first needs a (1000, 10)
intermediate and many more operations.
Floating-point rounding can also differ between orders, so do not expect every bit of the two results to match.
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.