Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to Deep Learning Notes
Topic #204

Matrix Multiplication

Matrix multiplication is the single most-executed operation in deep learning — every linear layer, every attention score, every convolution (reshaped) reduces to it. Understanding its shape rule is non-negotiable for debugging real models.

Formula and Shape Rule

\[ \mathbf{C} = \mathbf{A}\mathbf{B}, \qquad C_{ij} = \sum_{k=1}^{n} A_{ik} B_{kj} \]

If \(\mathbf{A}\) has shape \((m, n)\) and \(\mathbf{B}\) has shape \((n, p)\), the inner dimensions must match (\(n = n\)), and the result \(\mathbf{C}\) has shape \((m, p)\) — the outer dimensions.

How Each Entry Is Computed

A (2×3) 1 2 3 (row 1) 4 5 6 B (3×2) 7 9 11 8 10 12 C₁₁ = 1·7+2·9+3·11 = 58

Each output entry \(C_{ij}\) is the dot product of row \(i\) of \(\mathbf{A}\) and column \(j\) of \(\mathbf{B}\).

Full Numerical Example

\[ \begin{bmatrix}1 & 2\\3 & 4\end{bmatrix} \begin{bmatrix}5 & 6\\7 & 8\end{bmatrix} = \begin{bmatrix}1\cdot5+2\cdot7 & 1\cdot6+2\cdot8\\3\cdot5+4\cdot7 & 3\cdot6+4\cdot8\end{bmatrix} = \begin{bmatrix}19 & 22\\43 & 50\end{bmatrix} \]

Matrix Multiplication Is Not Commutative

In general \(\mathbf{A}\mathbf{B} \ne \mathbf{B}\mathbf{A}\) — order matters, unlike scalar multiplication. This is also distinct from element-wise (Hadamard) multiplication, written \(\mathbf{A} \odot \mathbf{B}\), which multiplies corresponding entries and requires equal shapes.

Code

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

print(A @ B)          # matrix multiplication -> [[19 22] [43 50]]
print(A * B)           # element-wise (Hadamard) -> [[5 12] [21 32]]  -- different!
import torch
A = torch.tensor([[1., 2.], [3., 4.]])
B = torch.tensor([[5., 6.], [7., 8.]])

print(torch.matmul(A, B))   # or A @ B
print(A * B)                 # element-wise, NOT the same operation

Where This Shows Up in Deep Learning

Every linear (fully-connected) layer computes \(\mathbf{y} = \mathbf{W}\mathbf{x} + \mathbf{b}\) — a matrix multiplication plus a bias. A batch of \(N\) inputs, each of size \(d_{in}\), passed through a layer producing \(d_{out}\) outputs, is one matrix multiplication: \((N, d_{in}) \times (d_{in}, d_{out}) \rightarrow (N, d_{out})\). This same operation, at massive scale, is also the core of self-attention (\(\mathbf{Q}\mathbf{K}^\top\)) covered later in this hub.

Common Mistakes

  • Confusing * (element-wise) with @/matmul (true matrix multiplication) — this is one of the most common silent shape/logic bugs in PyTorch code.
  • Forgetting the inner-dimension rule and getting a shape-mismatch error without knowing why — always write out both shapes and check the inner numbers match.

Interview Relevance

Q: "What shape does a batch of 32 samples, each with 10 features, become after a linear layer with 5 output units?" The input is \((32, 10)\), the weight matrix is \((10, 5)\), so the matrix multiplication produces \((32, 5)\) — 32 samples, each now with 5 output features.

Practice Question

Can you multiply a \((4, 3)\) matrix by a \((4, 3)\) matrix using standard matrix multiplication? If not, what shape would the second matrix need to be, and what would the result's shape be?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →