๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #15

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?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Matrix Multiplication โ€“ FAQs

Quick answers about learning Matrix Multiplication in Deep Learning.

This free note from CodingNow 2.0 explains Matrix Multiplication in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Matrix Multiplication, is 100% free with no signup required.
With focused practice, most students grasp Matrix Multiplication in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now