Shape Mismatch Debugging

Shape mismatch errors are not random. They report a broken shape assumption.

Start by printing the shapes:

Then write the operation as a shape equation.

Matrix product mismatch

X.shape is (3, 2). w.shape is (3,).

The matrix product expects:

(rows, features) @ (features,)

but the feature counts are 2 and 3, so the operation cannot work.

Read the shape mismatch

Runs locally with Python in your browser.

Ready to run.

Fix the assumption, not the symptom

Do not reshape blindly. Ask what the data means.

If each row has two features, the weight vector should have two weights:

w = np.array([10, 1])

Now:

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

The checklist

When an array operation fails:

  1. print the shapes;
  2. write the intended shape equation;
  3. name what each axis means;
  4. reduce to a tiny example;
  5. fix the data or operation that violated the equation.
Exercise: Broken inner dimension

Why does (3, 2) @ (3,) fail as a matrix-vector product?

Choose one

Select one choice, then check.

Hint

Compare the matrix's number of columns with the vector's length.

Solution

The inner dimensions do not match. The matrix has two columns, but the vector has length three:

(3, 2) @ (3,)
    ^      ^

A compatible vector would have shape (2,).