Module 0 · optional · Lesson 0.3
Dimensions and transpose
Check shapes before calculating entries. By the end, you should infer a transposed matrix product, reject an impossible product, and preserve axes through scaling and row normalization.
Prerequisite: read matrix declarations and indices; 0.1 and 0.2 offer optional refreshers.
0.3.1 Match the inner axes
For matrix multiplication, a left matrix with r rows and c columns can multiply a right matrix with c rows and s columns. The matching c axis is reduced. The result retains r rows and s columns:
(r by c) times (c by s) produces r by s
r, c and s are sizes, not matrix entries. An equal total element count is insufficient; the inner dimensions must match. Transpose swaps row and column axes without adding or deleting entries.
0.3.2 Worked example: scores compare query and key rows
Q: 4 by 8; K: 6 by 8; transposed K: 8 by 6
Q times transposed K: 4 by 6
Q contains four query rows, K six key rows, and both use eight features per row. Every query-key pair reduces eight component products into one score. Four queries compared against six keys need 24 scores. K and its transpose each contain 48 entries. The untransposed product QK is invalid here because 8 does not match 6.
Common mistake: producing 8 by 8 by retaining the inner axes. Those axes describe the products being summed, not the surviving query-key pairs.
0.3.3 Elementwise operations preserve the score grid
Dividing each score by a nonzero scalar changes entries but keeps the four-by-six shape. Row-wise softmax turns each six-entry row into six weights; it also preserves shape. Broadcasting a scalar means reusing the same scalar for each entry, rather than performing matrix multiplication.
With independent sequences and heads, a score tensor may have shape N by H by T_q by T_k. N is batch size, H is query-head count, T_q query positions and T_k key positions. The last two axes describe each score matrix. Reducing or normalizing over the key axis must be stated; softmax over the whole tensor would answer a different question.
0.3.4 Predict valid and impossible products
Make a prediction before opening hints. Try the task again without help after checking the reasoning.
0.3.5 Transfer to different dimensions
Without the shape display, let P is 3 by 5 and R is 7 by 5. Give the shapes of transposed R, P times transposed R, and its row-wise softmax. Can P multiply R directly?
Check the reference explanation
Transposed R is 5 by 7. The product is 3 by 7, and row-wise softmax keeps 3 by 7. The direct product is invalid because 5 differs from 7. If your output was 3 by 5, you kept the feature axis that was reduced instead of the seven comparison targets.