Matrix multiplication requires the inner dimensions to match. Naming the shapes of inputs, weights, and outputs can reveal a mistake before any numerical calculation is performed.
Worked example
Let X contain a batch of five observations with three features each, so its shape is five by three. A weight matrix W with shape three by two transforms those observations into two outputs each. The product XW has shape five by two. Reversing the multiplication would require a different dimensional relationship and would describe a different operation.
What to check
Write the axis meanings beside their lengths. A dimension of two could represent classes, time steps, or coordinates, and matching numbers alone do not establish a valid model. Check any bias broadcasting rule as well. A bias with two components is commonly added to every observation’s output, but an incorrectly shaped array may broadcast across an unintended axis. Shape checks and semantic checks support each other.
Continue with the book
Explore the full treatment, worked examples, and exercises in Mathematics for AI and Machine Learning.
Companion article for Mathematics for AI and Machine Learning, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
