Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Multiplying box by box
Two price lists for the same shop: the old prices and a “discount factor” for each item. To get the new prices, you multiply each item’s price by its own factor, box by box. That’s the Hadamard product, nothing more.
One number for everyone
“Add ₹10 to every price.” You don’t need a whole table of 10s. You just say “+10” and it’s applied everywhere. Computers do the same thing, and it’s called broadcasting.
Tiling a pattern
Take a small 2 × 2 tile pattern and stamp it in every square of a 3 × 3 grid, scaling it by the number in that square. You get a 6 × 6 picture. Making a big pattern out of “every square times the tile” is the Kronecker product.
- 1. Prices [2, 3] and factors [4, 5], box by box. The first new price?
- 2. 'Add 10 to every price' without writing a table of 10s is…
- 3. A 2 × 2 tile stamped into every square of a 3 × 3 grid makes how many cells?
Level 1: Beginner
★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.Hadamard (element-wise) product
It needs the same shape. In NumPy and PyTorch it’s A * B. Matrix multiplication is A @ B, which is a completely different operation. Mixing them up is a classic bug.
Broadcasting, informally
np.array([1, 2, 3]) + 10 gives [11, 12, 13]. A column of shape plus a row of shape gives a full table, where entry is “row value + column value”, like an addition table.
| dim (from right) | 2 | 1 |
|---|---|---|
| A | 3 | 1 |
| B | 1 | 4 |
| result | 3 | 4 |
- 1. In NumPy, A * B for two matrices computes…
- 2. Shape of (3, 1) + (1, 4)?
- 3. Shape of (5, 3) + (3,)?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.The broadcasting rule, precisely
Line the shapes up from the right. For each position, the sizes must be equal, or one of them must be 1 (or missing). The result takes the larger size. Sizes of 1 are “stretched” without copying memory.
| Shapes | Result |
|---|---|
(3,4) + (4,) |
(3,4): add a vector to every row |
(3,4) + (3,) |
❌ error: aligned from the right, 4 ≠ 3. Use (3,1) instead |
(4,1,3) + (2,1) |
(4,2,3) |
(B,H,T,T) + (1,1,T,T) |
(B,H,T,T): one attention mask for every batch item and head |
The Kronecker product
For and , it’s a matrix built from the blocks .
Where they’re used
| Operation | Use |
|---|---|
| Hadamard | masks (dropout, attention masks, padding), gates in LSTMs, GRUs and GLU/SwiGLU, element-wise activations and their gradients, per-feature scaling |
| Broadcasting | adding biases, standardizing columns , pairwise distances, batched operations everywhere |
| Kronecker | separable 2-D filters, multi-dimensional grids, covariance of matrix-valued data, the K-FAC optimizer, quantum computing |
Broadcasting is powerful but silent. y.shape == (n,) minus pred.shape == (n,1) gives an matrix instead of an error, a famous source of wrong losses. Always check shapes (A1-7).
- 1. Shape of A ⊗ B for 2 × 2 and 3 × 3?
- 2. (4, 1, 3) + (2, 1) → ?
- 3. An LSTM gate multiplies a signal by values in (0, 1) using…
Level 3: Advanced
★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.Kronecker identities
(for and ). The eigenvalues of are all the products .
The vec trick
Stacking the columns of into a vector turns a matrix equation into an ordinary linear system:
That’s how Sylvester and Lyapunov equations from control theory (S2), , become . The other way round, it lets you multiply by a giant Kronecker matrix without forming it.
The Schur product theorem
If and are symmetric positive semidefinite, so is .
Proof sketch. is PSD (its eigenvalues are products of non-negative numbers), and is a principal submatrix of : pick the rows and columns indexed by pairs . Principal submatrices of PSD matrices are PSD. ∎ In ML, this means the product of two valid kernels is a valid kernel, a basic rule for building Gaussian-process and SVM kernels (B3, I2).
Exercise 3.1: A is 2 × 3, X is 3 × 4, B is 4 × 5. What size is the Kronecker matrix in vec(AXB) = (Bᵀ ⊗ A) vec(X)?
is and is , so is . It maps to .
- 1. rank A = 2 and rank B = 3. rank(A ⊗ B)?
- 2. tr A = 3 and tr B = 4. tr(A ⊗ B)?
- 3. The product of two valid kernel matrices, element-wise, is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Kronecker-factored curvature: K-FAC
Natural-gradient and second-order optimizers need the curvature (Fisher) matrix of the weights. For a layer that’s a matrix: impossible. K-FAC (Martens and Grosse, 2015) approximates each layer’s block as a Kronecker product of two small matrices (input-activation and output-gradient covariances). Thanks to and the vec trick, inverting it only needs the two small inverses. Shampoo (Gupta et al., 2018), which has performed strongly in recent optimizer benchmarks, uses a similar Kronecker structure (A6).
Element-wise structure in modern architectures
- Gating. GLU-style feed-forward layers (Shazeer, 2020), as used in many modern LLMs, compute : one path decides how much of the other to let through.
- Masks. Causal attention adds a broadcast mask of before the softmax, and dropout multiplies by a random 0/1 mask.
- Robotics and controls. Multi-dimensional grids for value functions and occupancy maps are built with Kronecker or broadcast structure. The covariance of a stack of independent identical sensors is , which keeps multi-sensor fusion cheap.
References
- L. Kronecker (c. 1866). Lectures; the product was studied earlier by J. G. Zehfuss (1858), “Über eine gewisse Determinante.” Zeitschrift für Mathematik und Physik, 3.
- I. Schur (1911). “Bemerkungen zur Theorie der beschränkten Bilinearformen mit unendlich vielen Veränderlichen.” Journal für die reine und angewandte Mathematik, 140.
- C. Van Loan (2000). “The ubiquitous Kronecker product.” Journal of Computational and Applied Mathematics, 123.
- J. Martens and R. Grosse (2015). “Optimizing neural networks with Kronecker-factored approximate curvature.” ICML.
- V. Gupta, T. Koren, Y. Singer (2018). “Shampoo: Preconditioned stochastic tensor optimization.” ICML.
- N. Shazeer (2020). “GLU variants improve Transformer.” arXiv:2002.05202.
- NumPy documentation, “Broadcasting.” numpy.org.