Matrices
Follow each row of a matrix to one output, check the (m × n)(n × p) shape rule, multiply several input columns at once, and see what a transpose does.
A matrix is a rectangular arrangement of numbers. Here is one with two rows and two columns:
$$W=\begin{bmatrix}4&1\\0&2\end{bmatrix}$$
A row runs across: the first row contains 4 and 1. A column runs down: the first column contains 4 and 0. We describe the matrix's shape as 2 × 2, always rows first, columns second. W is just its name.
We can use those numbers as a rule that turns two inputs into two outputs. Choose the inputs 2 and 3, written in a column:
$$x=\begin{bmatrix}2\\3\end{bmatrix}$$
This input column has shape 2 × 1: two rows and one column. Let's see how W acts on it.
Follow one row to one output
Take the first row of W. Multiply its 4 by the first input, 2, and its 1 by the second input, 3. Add the results: 8 + 3 = 11.
Repeat with the second row: 0 × 2 + 2 × 3 = 6. Written together, these two calculations give the matrix product Wx:
$$Wx=\begin{bmatrix}4\times2+1\times3\\0\times2+2\times3\end{bmatrix} =\begin{bmatrix}11\\6\end{bmatrix}$$
Each row produces one output. The second output ignores the first input because its multiplier is zero. Multiplying corresponding entries and adding them is called a dot product; matrix multiplication repeats that operation for each row.
Predict: Change only the first input from 2 to 5. Which output changes, and what are the two new outputs?
Shapes tell you which calculation is possible
Suppose the next matrix has three rows and two columns. Each row still needs two inputs, and the three rows produce three outputs.
$$(3\times2)\;(2\times1)\;\longrightarrow\;(3\times1)$$
The inner dimensions must agree because every row needs as many entries as the input supplies. More generally, (m × n) multiplied by (n × p) produces (m × p). Here m, n and p count dimensions; they are not values stored inside the matrices.
Try multiplying W by two input columns at once:
$$X=\begin{bmatrix}2&5\\3&3\end{bmatrix},\qquad WX=\begin{bmatrix}11&23\\6&6\end{bmatrix}$$
Each column is a separate pair of inputs. The first gives [11, 6], as above; the second gives [23, 6], matching your prediction. A matrix product can process several input columns this way.
Rows can become columns
The transpose swaps row and column positions, without changing individual values:
$$W^T=\begin{bmatrix}4&0\\1&2\end{bmatrix}$$
Transposing a 3 × 2 matrix produces a 2 × 3 matrix. This changes which entries participate together in a row's dot product; it does not generally undo the original transformation.
For a neural network's weighted sums, you can now follow which inputs contribute to each output and check whether the shapes fit.
Optional exercise: Read the matrix product in Deep Learning, §2.2. Pick one output entry in its notation and identify the row and column that produce it. Then explain why a 2 × 3 matrix cannot multiply a 2 × 1 column directly.
- A matrix's shape is rows × columns. A row runs across; a column runs down.
- Matrix multiplication forms each output entry by multiplying a row with a column and adding the products.
- Shape rule: (m × n) @ (n × p) → (m × p). Inner dimensions must match.
- With W = [[4, 1], [0, 2]], input [2, 3] produces [11, 6].
- Transpose swaps row and column positions. It does not generally undo a transformation.