x = torch.tensor(3.0)
x, x.shapeDive into Deep Learning · §1.3
Linear Algebra: The Computational Engine of Deep Learning
tensors & symmetry · axis reductions · projections & matrix products · norms · spectral dynamics
Motivation
Building Blocks
() (e.g., loss \mathcal{L} or learning rate \eta).(d,), treated mathematically as column vectors \mathbf{x} = [x_1, \ldots, x_d]^\top.len(x)), whereas a tensor’s order (rank) is its number of axes (ndim). A 784-D vector is a 1^\textrm{st}-order tensor.(tensor(3.), torch.Size([]))
2D Representations
tensor([[0., 3.],
[1., 4.],
[2., 5.]])
Tensors & Hadamard Product
(B, T, d) and 4D images (256, 1, 28, 28) unroll to 2D matrices via reshape(256, -1).A * B): Multiplies matching entries [\mathbf{A} \odot \mathbf{B}]_{ij} = a_{ij} b_{ij} for gating (LSTMs, SwiGLU), distinct from matrix product A @ B.tensor([[ 0., 1., 4.],
[ 9., 16., 25.]])
Reductions
.sum(), .mean()) aggregate entries along a chosen axis and remove that axis from the shape:
A.sum()): collapses (2, 3) -> (), a scalar.axis=0): c_j = \sum_{i=1}^m a_{ij} leaves column totals of shape (3,).axis=1): r_i = \sum_{j=1}^n a_{ij} leaves row totals of shape (2,).(tensor(15.), tensor([3., 5., 7.]), tensor([ 3., 12.]))
Reduction + Broadcasting
A / A.sum(axis=1) fails: Dividing (2, 3) by (2,) right-aligns as (2, 3) vs. (1, 2)—trailing dimensions 3 \neq 2 clash.keepdims=True retains length 1 ((2, 1)): Now A / sum_A broadcasts across columns so each row sums to 1 (softmax, attention).axis=0, (784,)) broadcast directly; per-image means (axis=1, (256, 1)) need keepdims=True (LayerNorm).Inner Products
(tensor([0., 1., 2.]), tensor([1., 1., 1.]), tensor(3.))
Angles in High Dimensions
2 * x) leaves \cos\theta unchanged—making it the workhorse similarity score for token, image, and retrieval embeddings.Matrix–Vector Products
(torch.Size([2, 3]), torch.Size([3]), tensor([ 5., 14.]))
Linear Geometry
Matrix–Matrix Products
A @ B) computes mn dot products of length k: c_{ij} = \mathbf{a}_i^\top \mathbf{b}_{:,j} = \sum_{l=1}^{k} a_{il}\, b_{lj}tensor([[ 3., 3., 3., 3.],
[12., 12., 12., 12.]])
(256, 784) @ (784, 10) + (10,) -> (256, 10) evaluates all 2{,}560 logits at once; argmax along axis 1 predicts each image’s class:(torch.Size([256, 10]), torch.Size([256]))
Systems & FLOPs
Check Your Understanding
Given flattened minibatch X of shape (256, 784) and weight matrix W_T stored row-wise with shape (10, 784):
X @ W_T fail, and what expression computes the (256, 10) logit matrix?X.sum(axis=0) and X.sum(axis=1, keepdims=True)?X is square (784, 784). What happens if you evaluate X - X.mean(axis=1) without keepdims=True?Answers:
X @ W_T.T aligns (256, 784) @ (784, 10) -> (256, 10).X.sum(axis=0) has shape (784,) (per-pixel totals); X.sum(axis=1, keepdims=True) has shape (256, 1) (per-image totals).X.mean(axis=1) has shape (784,), which right-aligns to (1, 784) and subtracts image j’s mean from pixel j of every image without raising an error.Norms
(tensor(5.), tensor(7.))
Norms in Machine Learning
Eigendecomposition
linalg.eigh).(tensor([1., 3.]),
tensor([[-0.7071, 0.7071],
[ 0.7071, 0.7071]]))
Repeated Matrix Multiplication
Why Initialization & Normalization Matter
tensor([1.1739e+02, 5.1538e-03])
Key Takeaways
| Operation | Form | Shape Rule | FLOPs | Deep Learning Role |
|---|---|---|---|---|
| Hadamard Product | \mathbf{A} \odot \mathbf{B} (A * B) |
(m, n) \to (m, n) | mn | Gating in LSTMs, SwiGLU, masks |
| Axis Reduction | sum(axis=1, keepdims=True) |
(m, n) \to (m, 1) | mn | Softmax normalization, LayerNorm |
| Dot Product & Cosine | \mathbf{x}^\top\mathbf{y} = \|\mathbf{x}\|_2\|\mathbf{y}\|_2\cos\theta | (d,), (d,) \to () | 2d | Neuron logit, query–key similarity |
| Matrix–Vector | \mathbf{A}\mathbf{x} (A @ x) |
(m, n) \times (n,) \to (m,) | 2mn | Single-example linear map |
| Matrix–Matrix | \mathbf{C} = \mathbf{X}\mathbf{W} (X @ W) |
(n, k) \times (k, m) \to (n, m) | 2nkm | Batched dense & attention layers |
| Norms (\ell_2, \ell_1, \textrm{F}) | \|\mathbf{x}\|_2, \|\mathbf{x}\|_1, \|\mathbf{W}\|_\textrm{F} | Any tensor \to () | 2N | MSE/MAE loss, weight decay, clipping |
| Eigendecomposition | \mathbf{A}\mathbf{v} = \lambda\mathbf{v} \Rightarrow \mathbf{A}^k\mathbf{v} = \lambda^k\mathbf{v} | (n, n) \to (\lambda_i, \mathbf{v}_i) | O(n^3) | Exploding/vanishing signals, curvature |