Entrywise Products
The Hadamard product multiplies equal-shaped matrices entry by entry.
On this page 7 sections
01Multiply matching entries#
The Hadamard product multiplies equal-shaped matrices entry by entry. It keeps their shape. In ML, this operation can apply a supplied mask or gate separately to each coordinate.
Suppose has rows and has rows . The four products are , so has rows .
02Keep the operation straight#
Each output entry uses exactly one entry from each input. No row-column sum appears. A row-by-column product is a different operation, with a different shape rule and generally different numbers.
Rows of A: (2,−1),(0,3). Rows of B: (4,2),(5,−2). Rows of A ⊙ B?
Show answer and explanation
Multiply corresponding entries and retain their positions.
A mask containing zeros and ones sets an entry to zero when the corresponding mask is zero and preserves it when the mask is one. For row and mask , the result is .
03Match shapes and positions#
A matrix can have a Hadamard product with another matrix. Equal numbers of entries are insufficient if their shapes differ. Reshaping or broadcasting is a separate convention that must be stated explicitly.
A draft computes an entry of A ⊙ B by summing a left row against a right column. What should change?
Show answer and explanation
The draft used ordinary matrix multiplication.
Changing one entry of affects only its matching entry in . If the matching entry is zero, the output stays zero. Position carries the meaning of this operation.
Compute the Hadamard product of two equal-shaped real matrices.
- Compute the Hadamard product of two equal-shaped real matrices.
Sources & further reading
- [1]Boyd and Vandenberghe, Introduction to Applied Linear Algebra ↗stanford.edu · Article
- [2]MIT 6.390, Appendix A: Matrix Calculus ↗introml.mit.edu · Article