x = torch.arange(3)
xDive into Deep Learning · §1.3
Every model in this book compiles down to a short list of operations
vectors · matrices · products · norms · eigenvalues.
Motivation
sum and mean, along chosen axes.Rank = number of axes; shape = size per axis.
01
The objects
scalars, vectors, matrices, tensors (and one flip)
The objects
A scalar is a rank-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:
tensor([0, 1, 2])
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
The objects
The objects
02
Arithmetic & reduction
element-wise ops · sums that drop or keep axes
Arithmetic
Two tensors of one shape combine entry by entry:
(tensor([[0., 1., 2.],
[3., 4., 5.]]),
tensor([[ 0., 2., 4.],
[ 6., 8., 10.]]))
Arithmetic
Adding or multiplying by a scalar touches each entry and leaves the shape alone:
(tensor([[[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]],
[[14, 15, 16, 17],
[18, 19, 20, 21],
[22, 23, 24, 25]]]),
torch.Size([2, 3, 4]))
Reduction
sum() with no arguments collapses everything to a scalar:
(tensor([0., 1., 2.]), tensor(3.))
axis= chooses which dimension disappearsReduction
keepdims: reduce, but stay broadcastableReduction
keepdims=True keeps the folded axis at size 1:
(tensor([[ 3.],
[12.]]),
torch.Size([2, 1]))
03
Products
one idea at three sizes: dot · matrix–vector · matrix–matrix
Products
Products · geometry
\cos\theta = \frac{\mathbf{x}^\top\mathbf{y}}{\|\mathbf{x}\|\,\|\mathbf{y}\|}.
+1 aligned · 0 perpendicular · -1 opposed: the dot product is deep learning’s favorite similarity measure.
Products · geometry
Why can \cos\theta never escape [-1, 1]? That is the Cauchy–Schwarz inequality |\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:
(tensor(1.5027), tensor(True))
An angle in [0, \pi], and the inequality holds on every draw.
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:
(torch.Size([2, 3]), torch.Size([3]), tensor([ 5., 14.]), tensor([ 5., 14.]))
Every fully-connected layer computes exactly this (plus a nonlinearity); much more on that later.
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:
(tensor([0., 1.]), tensor([-1., 0.]))
Products
04
Norms & eigenvalues
how long is a vector, and which directions a matrix keeps
Norms
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:
tensor(2.5362) tensor(2.5362)
tensor(True)
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:
tensor(6.)
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), flip (\lambda<0), but never turn.
Eigenvalues · payoff
Multiply a random vector by \mathbf{S} ten times and measure how much the norm grows per step:
tensor(8.8612)
The growth factor converges to \max_i |\lambda_i| = 8.8612: whatever vector you start from, the largest eigenvalue soon dominates.
Deep networks multiply by dozens of matrices in a row. Whether signals and gradients explode or vanish is this experiment at scale; the analysis returns in the numerical-stability section.
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.