Matrix Operations
Matrices add entry by entry, multiply row by column with inner sizes that must match, and transpose flips rows into columns.
Why Does This Exist?
Every layer of a neural network is a matrix operation. A dense layer multiplies a batch of inputs by a weight matrix, adds a bias, and passes the result on. Data preprocessing centers matrices, attention scores them against their transposes, and gradients flow back through the same three operations. If adding, multiplying, and transposing feel shaky, nothing downstream can feel solid.
Vectors, matrices, and tensors introduces what these objects are. This page is the verbs: the three operations, their shape rules, and the one order trap that bites everyone.
The one idea you need first
A matrix shape like reads rows first: 2 rows, 3 columns. Entry sits in row , column . Nearly every error in this subject is a shape error, so read shapes before computing anything.
Think of It Like This
Recipes scaled across kitchens
Think of a matrix as a recipe card: rows are dishes, columns are ingredients. Adding two cards blends two menus dish by dish. Multiplying is fulfilling orders: each row of the first card (a dish's needs) meets each column of the second (a supplier's offering) to produce a table of total costs. Transposing rewrites the card sideways, ingredients as rows, so tomorrow's computation reads naturally.
The analogy's limit is deliberate: real matrices multiply numbers, not intentions, and the row-meets-column rule has no flexibility. A dish with 3 ingredient entries can only meet a supplier listing 3 dishes, which is the inner-size rule with an apron on.
How It Actually Works
Addition: entry by entry, same shape required. Add matching positions:
A plus a is undefined, full stop. (Broadcasting in libraries bends this rule; the math does not.)
Multiplication: row by column, inner sizes must match. For , entry is row of dotted with column of . An times an gives : the inner s must agree and the outer sizes survive.
Worked example
Both are , so the product is :
So . Reversing the order gives , a different matrix, which proves the headline warning: in general.
Transpose: flip across the diagonal. Rows become columns, so an becomes :
Entry moves to . Transposing twice returns the original: .
Watch Out For
Multiplying matrices whose inner sizes disagree
The symptom is a shape error at 1am: a weight meeting a input and refusing to multiply. The fix is the outer-inner chant: write both shapes down, check the middle numbers match, and read the answer shape off the outer numbers. Nine times out of ten the fix is a missing transpose on one side, which is exactly why transpose exists.
Assuming AB equals BA
Scalar habits die hard, and matrix multiplication is not commutative. The symptom is a silently wrong result, because often has a valid shape and computes without complaint, as the worked example shows with two fully valid but different answers. Gradients, rotations, and projections all depend on the order. When porting a formula, preserve the order exactly; when debugging, swap suspects deliberately and compare.
The Quick Version
- Addition is entry by entry and needs identical shapes.
- Multiplication is row by column: times gives .
- The worked product of the two example matrices is .
- Transpose flips rows and columns: entry moves to .
- in general, so operand order is part of the formula.
- When shapes refuse to multiply, check the inner sizes, then suspect a missing transpose.