x = tf.range(3)
xDive into Deep Learning · §1.3
Linear-algebra operations used throughout this book
vectors · matrices · products · norms · eigenvalues.
Motivation
sum and mean, along chosen axes.Order = number of axes; shape = size per axis. Matrix rank is a different concept.
01
The objects
scalars, vectors, matrices, tensors (and one flip)
The objects
A scalar is an order-0 tensor: one number. Stack n of them and you get a vector: a data record and an arrow in \mathbb{R}^n, with a length and a direction:
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([0, 1, 2], dtype=int32)>
Both readings matter: a row of a dataset is a vector, and so is the direction a training step moves the weights.
.shape answers the first question about any tensorThe objects
len counts a vector’s elements:
3
The objects
A matrix is an order-2 tensor, m rows \times n columns:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[0, 1],
[2, 3],
[4, 5]], dtype=int32)>
\mathbf{A}^\top swaps the roles of rows and columns:
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[0, 2, 4],
[1, 3, 5]], dtype=int32)>
The objects
\mathbf{A} = \mathbf{A}^\top: the flip changes nothing, and code can check it in one line:
<tf.Tensor: shape=(), dtype=bool, numpy=True>
Covariance and Gram matrices are symmetric, a structure that many methods (and this deck’s finale) exploit.
The objects
02
Arithmetic & reduction
element-wise ops · sums that drop or keep axes
Arithmetic
Two tensors of one shape combine entry by entry:
(<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[0., 1., 2.],
[3., 4., 5.]], dtype=float32)>,
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[ 0., 2., 4.],
[ 6., 8., 10.]], dtype=float32)>)
Arithmetic
Adding or multiplying by a scalar touches each entry and leaves the shape alone:
(<tf.Tensor: shape=(2, 3, 4), dtype=int32, numpy=
array([[[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]],
[[14, 15, 16, 17],
[18, 19, 20, 21],
[22, 23, 24, 25]]], dtype=int32)>,
TensorShape([2, 3, 4]))
Reduction
sum() with no arguments collapses everything to a scalar:
(<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0., 1., 2.], dtype=float32)>,
<tf.Tensor: shape=(), dtype=float32, numpy=3.0>)
axis= chooses which dimension disappearsReduction
keepdims: reduce, but stay broadcastableReduction
keepdims=True keeps the folded axis at size 1:
(<tf.Tensor: shape=(2, 1), dtype=float32, numpy=
array([[ 3.],
[12.]], dtype=float32)>,
TensorShape([2, 1]))
03
Products
one idea at three sizes: dot · matrix–vector · matrix–matrix
Products
\mathbf{x}^\top\mathbf{y} = \sum_i x_i y_i: multiply matching entries, add them up:
(<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0., 1., 2.], dtype=float32)>,
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>,
<tf.Tensor: shape=(), dtype=float32, numpy=3.0>)
With nonnegative weights summing to 1, the dot product is a weighted average.
Products · geometry
\cos\theta = \frac{\mathbf{x}^\top\mathbf{y}}{\|\mathbf{x}\|\,\|\mathbf{y}\|}.
+1 aligned · 0 perpendicular · -1 opposed: the normalized dot product is a common similarity measure.
Products · geometry
The Cauchy–Schwarz inequality explains why \cos\theta remains in [-1, 1]: |\mathbf{x}^\top\mathbf{y}| \le \|\mathbf{x}\|\,\|\mathbf{y}\| (proved in the geometry-and-linear-algebraic-operations section). One random pair checks both facts at once:
(<tf.Tensor: shape=(), dtype=float32, numpy=1.960163950920105>,
<tf.Tensor: shape=(), dtype=bool, numpy=True>)
The computed angle lies in [0, \pi], and the result verifies the inequality for one random pair.
Products
(\mathbf{A}\mathbf{x})_i = \mathbf{a}^\top_i \mathbf{x}, so a 2\times3 matrix maps a length-3 vector to a length-2 vector:
(TensorShape([2, 3]),
TensorShape([3]),
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([ 5., 14.], dtype=float32)>)
A fully connected layer applies this operation, adds a bias, and may then apply a nonlinearity.
Products
Multiplication by \mathbf{A} \in \mathbb{R}^{m\times n} is a linear map \mathbb{R}^n \to \mathbb{R}^m. The rotation matrix \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} turns the plane by \theta; at \theta = 90° it sends \mathbf{e}_1 \mapsto \mathbf{e}_2 and \mathbf{e}_2 \mapsto -\mathbf{e}_1:
(<tf.Tensor: shape=(2,), dtype=float32, numpy=array([0., 1.], dtype=float32)>,
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([-1., 0.], dtype=float32)>)
Products
04
Norms & eigenvalues
how long is a vector, and which directions a matrix keeps
Norms
For \mathbf{u} = [3, -4] the Euclidean ruler reads 5; the taxicab ruler reads 7:
<tf.Tensor: shape=(), dtype=float32, numpy=5.0>
<tf.Tensor: shape=(), dtype=float32, numpy=7.0>
\|\mathbf{x}\|_2 = \sqrt{\textstyle\sum_i x_i^2}, \qquad \|\mathbf{x}\|_1 = \textstyle\sum_i |x_i|.
Norms
Homogeneity \|\alpha\mathbf{x}\| = |\alpha|\,\|\mathbf{x}\| and the triangle inequality \|\mathbf{x}+\mathbf{y}\| \le \|\mathbf{x}\|+\|\mathbf{y}\|, holding on random vectors:
tf.Tensor(4.0812373, shape=(), dtype=float32) tf.Tensor(4.081238, shape=(), dtype=float32)
tf.Tensor(True, shape=(), dtype=bool)
For \ell_2, the triangle inequality is Cauchy–Schwarz in disguise: expand \|\mathbf{u}+\mathbf{v}\|^2 and bound the cross term (the geometry-and-linear-algebraic-operations section).
Norms
\|\mathbf{X}\|_\textrm{F} = \sqrt{\sum_{i,j} x_{ij}^2} is the \ell_2 norm of the flattened matrix. For the all-ones 4\times9: \sqrt{36} = 6:
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>
The spectral norm (how much \mathbf{X} can stretch a vector) needs the singular value decomposition; it arrives in the SVD-and-low-rank- approximation section.
Eigenvalues
\mathbf{A}\mathbf{v} = \lambda\mathbf{v}.
Along an eigenvector, the matrix acts like a scalar: stretch (|\lambda|>1), shrink (|\lambda|<1), or reverse orientation (\lambda<0), while preserving its span.
Eigenvalues
Multiply a random vector by \mathbf{S} ten times and measure how much the norm grows per step:
<tf.Tensor: shape=(), dtype=float32, numpy=8.861247062683105>
For a generic starting vector, the growth factor converges to \max_i |\lambda_i| = 8.8612 as the dominant eigenvalue takes over.
Products of many matrices can cause signals and gradients to explode or vanish. The spectra of those matrices contribute to this behavior; the numerical-stability section develops the analysis.
Wrap-up
sum/mean with axis=; keepdims= stays broadcastable.Next, calculus (the calculus section): every gradient there is built from these products. The full linear-algebra story continues in the linear algebra part of the math appendix.