← Tensor by Tensor
§ Math Fundamentals for AI
Tensor by Tensor · Complete Guide

Matrices

Fourteen lessons that connect high-school mathematics to AI. Start with the plain-language explanation and a small example. Then read the full formulas, derivations, and Python code at your own pace.

How to study a lesson

Read the opening example. Try the question before revealing its answer. Then work through the detailed notes below it. You do not need to understand every proof on the first reading.

Symbols: a subscript such as xi identifies one entry. Σ means add a collection of terms. ∈ means “belongs to”. The symbol ≈ means “approximately equal”, not exactly equal. A parameter is an adjustable number in a model.

01.05 · Matrices

Introduction

Two output neurons can reuse the same input vector while applying different weights. A two-row matrix turns input 2, 3 into outputs 11 and 6 by performing one dot product per row.

Learning goal

Read matrix shapes and calculate matrix-vector products, transposes, and basic matrix operations in runnable code.

Before you start

Vectors, dot products, rows and columns, and basic Python indexing.

Lesson plan

  1. Read a matrix as a rectangular table with named row and column sizes.
  2. Compute each output from one matrix row and check shape compatibility.
  3. Use transpose and NumPy operations while tracking every input and output shape.

Check the shape before multiplying. A matrix with shape (3,2) maps a two-value input to a three-value output. Each output row forms one weighted sum from the same two inputs. A common wrong answer reverses these dimensions.

What is a matrix?

A matrix is a rectangular array of numbers. It can represent a linear transformation, a dataset, or a set of parameters:

$$W = \begin{bmatrix} 4 & 1 \\ 0 & 2 \end{bmatrix}$$

This $2 \times 2$ matrix has 2 rows and 2 columns. We describe its shape as (rows, columns).

Definition · Matrix multiplication

For $A \in \mathbb{R}^{m \times n}$ and $B \in \mathbb{R}^{n \times p}$, the product $C = AB \in \mathbb{R}^{m \times p}$ has entries:

$$C_{ij} = \sum_{k=1}^n A_{ik} B_{kj}$$

Each output entry is the dot product of row $i$ of $A$ with column $j$ of $B$.

Matrix-vector product

A matrix acts on a vector to produce a new vector. With $W$ as above and $\mathbf{x} = [2, 3]^\top$:

$$W\mathbf{x} = \begin{bmatrix} 4 & 1 \\ 0 & 2 \end{bmatrix} \begin{bmatrix} 2 \\ 3 \end{bmatrix} = \begin{bmatrix} 4(2) + 1(3) \\ 0(2) + 2(3) \end{bmatrix} = \begin{bmatrix} 11 \\ 6 \end{bmatrix}$$

Each row of $W$ produces one output via a dot product with $\mathbf{x}$.

Example · Neural network layer

A linear layer in a neural network computes $\mathbf{y} = W\mathbf{x} + \mathbf{b}$, where:

  • $\mathbf{x} \in \mathbb{R}^n$ is the input vector
  • $W \in \mathbb{R}^{m \times n}$ is the weight matrix
  • $\mathbf{b} \in \mathbb{R}^m$ is the bias vector
  • $\mathbf{y} \in \mathbb{R}^m$ is the output vector

Shape compatibility

For $A \in \mathbb{R}^{m \times n}$ and $B \in \mathbb{R}^{p \times q}$, the product $AB$ is defined only if $n = p$ (inner dimensions match). The result has shape $(m, q)$:

$$(m \times n) \cdot (n \times q) \to (m \times q)$$
Common error

A $2 \times 3$ matrix cannot multiply a $2 \times 1$ vector directly — the inner dimensions (3 and 2) don't match. Always check shapes before multiplying.

Transpose

The transpose $W^\top$ swaps rows and columns:

$$W = \begin{bmatrix} 4 & 1 \\ 0 & 2 \end{bmatrix} \implies W^\top = \begin{bmatrix} 4 & 0 \\ 1 & 2 \end{bmatrix}$$

Properties:

  • $(A^\top)^\top = A$
  • $(AB)^\top = B^\top A^\top$
  • $(A + B)^\top = A^\top + B^\top$

Matrix operations in code

import numpy as np

W = np.array([[4, 1], [0, 2]])
x = np.array([2, 3])

# Matrix-vector product
y = W @ x  # or W.dot(x)
print(y)  # [11, 6]

# Transpose
Wt = W.T
print(Wt)  # [[4, 0], [1, 2]]

# Matrix-matrix product
X = np.array([[2, 5], [3, 3]])
Y = W @ X
print(Y)  # [[11, 23], [6, 6]]