§2.1–2.4Scalars to Tensors · Matrix Multiplication · Inverses · Span

Part I DL pp. 31–38 · ~8 min read

  • scalar
  • vector
  • matrix
  • tensor
  • transpose
  • broadcasting
  • matrix product
  • identity matrix
  • matrix inverse
  • span
  • linear independence

Linear algebra is continuous rather than discrete mathematics, so many computer scientists arrive with little experience of it — yet it is essential for nearly every machine learning algorithm, deep learning above all. This chapter teaches exactly the prerequisites the book needs.

Aside — how to read this chapter

Already comfortable? Skip ahead. Need a formula reference later? The book recommends The Matrix Cookbook (Petersen & Pedersen, 2006). Never seen linear algebra? This teaches enough to read the book, but a dedicated text (e.g. Shilov, 1977) is recommended too. Many standard topics are deliberately omitted as inessential for deep learning.

§2.1 The four objects

scalar s3.7italic lowercase · 0 axesvector 𝐱x₁x₂x₃bold lowercase · 1 axismatrix 𝐀bold UPPERCASE · 2 axes (A ∈ ℝ³ˣ²)tensor 𝗔sans-serif bold · any number of axes

The typography IS the type system — see the notation page.

  • A scalar is a single number, italic lowercase, introduced with its kind: “let sRs \in \mathbb{R} be the slope,” “let nNn \in \mathbb{N} be the number of units.”
  • A vector is an ordered array; element ii is xix_i. With nn real elements, xRn\boldsymbol{x} \in \mathbb{R}^n — a point in nn-dimensional space, one coordinate per axis. Written as a column: x=[x1,x2,,xn]\boldsymbol{x} = [x_1, x_2, \dots, x_n]^\top. Index a set of elements with a subscript set: for S={1,3,6}S = \{1, 3, 6\}, xSx_S picks those three; a minus sign takes complements (x1x_{-1} = all but x1x_1).
  • A matrix is a 2-D array, ARm×n\boldsymbol{A} \in \mathbb{R}^{m \times n}; A1,1A_{1,1} is the top-left entry, Ai,:A_{i,:} row ii, A:,iA_{:,i} column ii; for expressions, f(A)i,jf(\boldsymbol{A})_{i,j} indexes the result.
  • A tensor generalizes to any number of axes; element (i,j,k)(i, j, k) of 3-D tensor A\mathsf{\mathbf{A}} is Ai,j,k\mathsf{A}_{i,j,k}.

The transpose mirrors a matrix across its main diagonal: (A)i,j=Aj,i(\boldsymbol{A}^\top)_{i,j} = A_{j,i} (fig 2.1):

A₁,₁A₁,₂A₂,₁A₂,₂A₃,₁A₃,₂A ∈ ℝ³ˣ² — 3 rows, 2 columns⟶ ᵀA₁,₁A₂,₁A₃,₁A₁,₂A₂,₂A₃,₂Aᵀ ∈ ℝ²ˣ³ — rows and columns swap

Fig 2.1 (recreated) — entries on the main diagonal (amber) stay put; everything else reflects. A vector is a one-column matrix, so its transpose is a row: x=[x1,x2,x3]\boldsymbol{x} = [x_1, x_2, x_3]^\top; a scalar is its own transpose.

Element-wise arithmetic works as expected: same-shape addition (Ci,j=Ai,j+Bi,jC_{i,j} = A_{i,j} + B_{i,j}), scalar multiply/add (Di,j=aBi,j+cD_{i,j} = a \cdot B_{i,j} + c). Deep learning adds one unconventional shorthand — broadcasting : C=A+b\boldsymbol{C} = \boldsymbol{A} + \boldsymbol{b} means Ci,j=Ai,j+bjC_{i,j} = A_{i,j} + b_j, i.e. the vector is implicitly copied into every row rather than materializing that copy.

§2.2 Multiplying matrices and vectors

The matrix product C=AB\boldsymbol{C} = \boldsymbol{A}\boldsymbol{B} requires the inner shapes to agree — A\boldsymbol{A} must have as many columns as B\boldsymbol{B} has rows:

Term by term

Ci,j=kAi,kBk,jC_{i,j} = \sum_k A_{i,k} B_{k,j}
A\boldsymbol{A}left factor — its ROW i is consumedm×n
B\boldsymbol{B}right factor — its COLUMN j is consumedn×p
kkthe shared inner dimension being summed away1…n
Ci,jC_{i,j}one entry of the result: the dot product of row i with column jscalar; C is m×p

What this really says

Every entry of the product is a similarity score: how much row ii of A\boldsymbol{A} and column jj of B\boldsymbol{B} point in the same direction. The inner dimension nn is “summed away” — which is why shapes chain like dominoes: (m×n)(n×p)=m×p(m×n)(n×p) = m×p.

Step through it with real numbers:

C = AB, one dot product per cell (worked example)
A (2×3)120-131×B (3×2)21014-2=C (2×2)2320

C1,1 = row 1 · column 1 = 1·2 + 2·0 + 0·4 = 2

cell 1 / 4 — every entry is one dot product

This is not element-wise multiplication — that operation exists too and is called the Hadamard product AB\boldsymbol{A} \odot \boldsymbol{B}. The dot product between same-length vectors is the matrix product xy\boldsymbol{x}^\top\boldsymbol{y} — so the matrix product computes Ci,jC_{i,j} as the dot product of row ii of A\boldsymbol{A} with column jj of B\boldsymbol{B}.

Useful properties (a deliberately partial list):

  • distributive: A(B+C)=AB+AC\boldsymbol{A}(\boldsymbol{B} + \boldsymbol{C}) = \boldsymbol{A}\boldsymbol{B} + \boldsymbol{A}\boldsymbol{C}
  • associative: A(BC)=(AB)C\boldsymbol{A}(\boldsymbol{B}\boldsymbol{C}) = (\boldsymbol{A}\boldsymbol{B})\boldsymbol{C}
  • not commutative in general (AB=BA\boldsymbol{A}\boldsymbol{B} = \boldsymbol{B}\boldsymbol{A} does not always hold) — though the vector dot product is: xy=yx\boldsymbol{x}^\top\boldsymbol{y} = \boldsymbol{y}^\top\boldsymbol{x}
  • transpose of a product reverses the order: (AB)=BA(\boldsymbol{A}\boldsymbol{B})^\top = \boldsymbol{B}^\top\boldsymbol{A}^\top

That last rule proves dot-product commutativity in one line: a scalar is its own transpose, so xy=(xy)=yx\boldsymbol{x}^\top\boldsymbol{y} = (\boldsymbol{x}^\top\boldsymbol{y})^\top = \boldsymbol{y}^\top\boldsymbol{x}.

We can now write a whole system of linear equations in three symbols:

Term by term

Ax=b\boldsymbol{A}\boldsymbol{x} = \boldsymbol{b}
A\boldsymbol{A}KNOWN coefficient matrix — row i holds the coefficients of equation im×n
x\boldsymbol{x}UNKNOWN vector we solve forn×1
b\boldsymbol{b}KNOWN right-hand side — element i is the target of equation im×1

Expanded, row by row, it is exactly the school system: A1,1x1+A1,2x2++A1,nxn=b1A_{1,1}x_1 + A_{1,2}x_2 + \cdots + A_{1,n}x_n = b_1, and so on down to row mm — the matrix-vector notation is simply more compact.

§2.3 Identity and inverse

The identity matrix In\boldsymbol{I}_n changes nothing: xRn,  Inx=x\forall \boldsymbol{x} \in \mathbb{R}^n,\; \boldsymbol{I}_n\boldsymbol{x} = \boldsymbol{x} — ones on the main diagonal, zeros elsewhere (fig 2.2 is just I3\boldsymbol{I}_3). The matrix inverse is the matrix that undoes A\boldsymbol{A}: A1A=In\boldsymbol{A}^{-1}\boldsymbol{A} = \boldsymbol{I}_n. It solves the linear system in four mechanical steps:

Ax=b    A1Ax=A1b    Inx=A1b    x=A1b\boldsymbol{A}\boldsymbol{x} = \boldsymbol{b} \;\Rightarrow\; \boldsymbol{A}^{-1}\boldsymbol{A}\boldsymbol{x} = \boldsymbol{A}^{-1}\boldsymbol{b} \;\Rightarrow\; \boldsymbol{I}_n\boldsymbol{x} = \boldsymbol{A}^{-1}\boldsymbol{b} \;\Rightarrow\; \boldsymbol{x} = \boldsymbol{A}^{-1}\boldsymbol{b}

Worked example — invert and solve

Let A=[2111]\boldsymbol{A} = \begin{bmatrix} 2 & 1 \\ 1 & 1 \end{bmatrix}, b=[5,3]\boldsymbol{b} = [5, 3]^\top. For a 2×2, A1=1det[1112]\boldsymbol{A}^{-1} = \frac{1}{\det}\begin{bmatrix} 1 & -1 \\ -1 & 2 \end{bmatrix} with det=2111=1\det = 2·1 - 1·1 = 1. So x=A1b=[53,5+6]=[2,1]\boldsymbol{x} = \boldsymbol{A}^{-1}\boldsymbol{b} = [5-3,\, -5+6]^\top = [2, 1]^\top. Check: 22+11=52·2 + 1·1 = 5 ✓ and 12+11=31·2 + 1·1 = 3 ✓.

Aside — why nobody actually computes A⁻¹

A1\boldsymbol{A}^{-1} is mainly a theoretical tool. On a digital computer it is stored with limited precision, and algorithms that work directly from b\boldsymbol{b} (chapter 4’s territory) usually give more accurate estimates of x\boldsymbol{x}.

§2.4 When does a solution exist? Span and independence

Think of the columns of A\boldsymbol{A} as directions you can travel from the origin, with xix_i saying how far to go along column ii:

Term by term

Ax=ixiA:,i\boldsymbol{A}\boldsymbol{x} = \sum_i x_i \boldsymbol{A}_{:,i}
A:,i\boldsymbol{A}_{:,i}column i — one available direction of travelm×1
xix_ihow far to travel along that direction (can be negative)scalar
Ax\boldsymbol{A}\boldsymbol{x}the destination reached — a linear combination of the columnsm×1

A linear combination of vectors {v(1),,v(n)}\{\boldsymbol{v}^{(1)}, \dots, \boldsymbol{v}^{(n)}\} is iciv(i)\sum_i c_i \boldsymbol{v}^{(i)}; the span is everything reachable that way. Solving Ax=b\boldsymbol{A}\boldsymbol{x} = \boldsymbol{b} asks: is b\boldsymbol{b} in the span of the columns (the column space, or range, of A\boldsymbol{A})? Try it:

Ax = b as travel along columns — drag a₁ and a₂; make them collinear to see A go singular
a₁a₂bdrag the tips of a₁ and a₂ — shaded region = span of the columns

det(A) = 3.75

solutions of Ax = b: 1 (unique)

x = [1.00, 1.00]ᵀ
b = 1.00·a₁ + 1.00·a₂

Columns are linearly independent: the span is all of ℝ², A is invertible, and the dashed path shows x₁a₁ then x₂a₂ reaching b.

The counting rules the widget demonstrates:

  • Solutions come in 0, 1, or ∞ — never “exactly two”: if x\boldsymbol{x} and y\boldsymbol{y} both work, every blend αx+(1α)y\alpha\boldsymbol{x} + (1-\alpha)\boldsymbol{y} works too.
  • Solvable for every bRm\boldsymbol{b} \in \mathbb{R}^m ⟺ the column space is all of Rm\mathbb{R}^m ⟺ the columns include a set of exactly mm linearly independent vectors (nmn \ge m is necessary but NOT sufficient — two identical columns of a 2×2 span only a line). No mm-dimensional set has more than mm mutually independent vectors.
  • Invertibility additionally needs at most one solution per b\boldsymbol{b}: at most mm columns. Together: A\boldsymbol{A} must be square (m=nm = n) with all columns independent. A square matrix with dependent columns is singular — no inverse (though the system may still be solvable by other means).
  • For square matrices the left and right inverse coincide: AA1=A1A=I\boldsymbol{A}\boldsymbol{A}^{-1} = \boldsymbol{A}^{-1}\boldsymbol{A} = \boldsymbol{I}.

Check yourself — objects, products, inverses, span

1.A is 4×3 and B is 3×5. Which products are defined, and with what shape?

2.In the MatrixGrid widget with A = [[1,2,0],[-1,3,1]] and B = [[2,1],[0,1],[4,-2]], predict C₂,₂ before stepping to it.

3.Which identity is FALSE in general?

4.In the VectorSpanLab, you drag a₂ until it lies exactly along a₁. What happens and why?

5.Why does the book call A⁻¹ 'primarily a theoretical tool'?

5 questions