Matrix-Matrix Products

A matrix-matrix product combines rows from the left matrix with columns from the right matrix.

A @ B

If:

A.shape == (2, 3)
B.shape == (3, 4)

then:

(2, 3) @ (3, 4) -> (2, 4)

The inner dimensions match and disappear. The outer dimensions remain.

Small example

Matrix-matrix product

Runs locally with Python in your browser.

Ready to run.

Here:

(2, 3) @ (3, 2) -> (2, 2)

Each output entry is a row–column dot product. For example, the top-left entry uses the first row of A and the first column of B:

[1, 2, 3] dot [1, 0, 1] = 1*1 + 2*0 + 3*1 = 4

Two rows from A paired with two columns from B explain the (2, 2) result shape.

Why this matters

In deep learning, batches and weights often meet in matrix products:

outputs = X @ W

If X is (examples, input_features) and W is (input_features, output_features), then outputs is (examples, output_features).

This one shape rule carries far.

Exercise: Predict matrix-matrix shape

What is the result shape of (5, 3) @ (3, 2)?

Answer it first, then check.

Hint

Check that the inner dimensions match, then keep the two outer dimensions.

Solution

The result shape is (5, 2):

(5, 3) @ (3, 2) -> (5, 2)

The matching length-three dimensions are combined. Five rows and two columns remain.