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
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:
- print the shapes;
- write the intended shape equation;
- name what each axis means;
- reduce to a tiny example;
- fix the data or operation that violated the equation.
Exercise: Broken inner dimension
Why does (3, 2) @ (3,) fail as a matrix-vector product?
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,).