Read the shape of a batch multiplication

Preserve the order of coordinates and the signs of values. · 168 words

A batch is a group of examples processed together. Suppose X contains feature vectors as rows. If X has shape (b, d), it has b examples and d features per example. Let W have shape (d, k), with one column of weights per output. Then XW has shape (b, k): each row is one example multiplied by W. For example, X = [[1, 2], [3, 4]] and W = [[1, 0], [0, 2]]. XW = [[1, 4], [3, 8]], also shape (2, 2).

The middle dimensions must agree because each dot product pairs every input feature with a weight column. The number of examples can change without changing the feature count. For three books, let X = [[1, 0], [0, 1], [1, 1]] with shape (3, 2); one column marks history books and one marks science books. Let W = [[2, 1], [1, 3]], shape (2, 2). Then XW = [[2, 1], [1, 3], [3, 4]], shape (3, 2). There are two scores for each of three books.

Check your recall

A batch X has shape (5, 3) and W has shape (3, 2). What is the shape of XW, and what does each entry represent?

Check your answer

XW has shape (5, 2). Each entry is the dot product of one example's three features with one column of W, producing that example's score for one output.

Sources and credits

Original KeyTouch lesson. Background reading · Zhang et al. · Linear Algebra.