Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Numbers, lists, tables… and stacks of tables
- A single number (temperature now) is a scalar.
- A list of numbers (temperature each hour) is a vector.
- A table (temperature at each spot on a map) is a matrix.
- A stack of tables (that map, hour after hour) is a tensor.
A colour photo is a tensor too: height × width × 3 colours. A video adds time.
Many tensor operations are “multiply matching numbers and add them up”. One neat notation, einsum, describes all of them by naming the axes.
| 3 | 3 | -3 |
| -1 | 3 | -3 |
| -3 | 2 |
| 2 | -3 |
| -2 | -3 |
| 3 | 6 |
| 15 | -2 |
A Mars rover’s cameras produce tensors (images × colour channels × time), and so do its arm’s force sensors (sensors × axes × time). Every neural network that processes this data is a chain of tensor operations, usually written in exactly this einsum language.
- 1. How many numbers describe one pixel of a colour (RGB) image?
- 2. A greyscale video (frames × height × width) is a tensor with how many axes?
- 3. A matrix is a tensor with how many axes?
Level 1: Beginner
★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.Vocabulary
| Term | Meaning | Example |
|---|---|---|
| order (number of modes) | number of indices | an RGB batch has order 4 |
| shape | size of each mode | |
| fiber | fix all indices but one | one pixel’s 3 colour values |
| slice | fix all indices but two | one colour channel of one image |
| element-wise ops | , , functions | ReLU, broadcasting (A2.2-6) |
A tensor with shape has entries. Computers store it as one long list in row-major order: the last index changes fastest. In shape , the entry sits at position .
- 1. How many entries in shape (2, 3, 4)?
- 2. What is the order of a batch of RGB images (B, C, H, W)?
- 3. In row-major storage, which index changes fastest?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.Einstein summation
Write the indices of every input and the output. Every index not in the output is summed over.
| einsum | Formula | Operation |
|---|---|---|
ij,jk->ik |
matrix product | |
ii-> |
trace | |
ij->ji |
transpose | |
i,j->ij |
outer product | |
ij,ij-> |
Frobenius inner product | |
bij,bjk->bik |
per-batch matmul | batched layers |
bhqd,bhkd->bhqk |
multi-head attention scores |
Einstein introduced the convention in 1916 for general relativity. It is now np.einsum, torch.einsum and the einops library.
Unfolding and the mode-n product
The mode-n unfolding lays out all mode- fibers as the columns of a matrix of size . The mode-n product multiplies every mode- fiber by the matrix :
Each unfolding has a rank, and the tuple of these ranks is the multilinear rank. Unfolding turns tensor problems into matrix problems that we already know how to solve.
- 1. einsum('ii->', [[1, 2], [3, 4]]) = ?
- 2. In 'bij,bjk->bik', which index is summed?
- 3. Mode-1 unfolding of a 3 × 4 × 5 tensor has shape…
Level 3: Advanced
★★★★☆ Advanced⏱ 45 minGraduate level, with full derivations and proofs.CP decomposition: tensor rank
A rank-1 tensor is an outer product , with entries . The CP (CANDECOMP/PARAFAC) decomposition writes
The smallest exact is the tensor rank. It behaves very differently from matrix rank:
- Computing it is NP-hard (Håstad, 1990; Hillar and Lim, 2013).
- It can differ over and over , and it can exceed every dimension.
- A best rank-R approximation may not exist: sequences of rank-2 tensors can converge to a rank-3 tensor (de Silva and Lim, 2008).
- But CP is often unique. Kruskal’s theorem: if (the ’s are Kruskal ranks), then the factors are unique up to scaling and permutation. Matrix factorizations never are, since and . This uniqueness is why CP is used to discover interpretable components, as in PARAFAC in chemometrics.
Tucker decomposition and HOSVD
Here is a small core tensor and are factor matrices with orthonormal columns. The higher-order SVD (De Lathauwer et al., 2000) takes as the leading left singular vectors of the unfolding , and sets . Truncated HOSVD is not optimal, but it is within a factor of the best approximation:
Exercise 3.1: Count the parameters of CP vs Tucker for a 10 × 20 × 30 tensor
The full tensor has 6,000 entries. CP with needs numbers. Tucker with ranks needs . Tucker is more flexible, because its core mixes the components. CP is more compact and unique.
- 1. Computing the rank of a general 3-way tensor is…
- 2. Which decomposition is typically unique up to scaling and permutation?
- 3. HOSVD gets its factors from…
Level 4: Expert
★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.Fitting CP: alternating least squares
Fix two factor matrices, and the third solves a linear least-squares problem:
Here is the Khatri–Rao (column-wise Kronecker) product and is the Hadamard product. Cycle through the modes until the fit stops improving (Kolda and Bader, 2009).
Tensors in research
- Compressing neural networks. A convolution kernel of shape out × in × can be CP-decomposed into four thin convolutions, which gives large speed-ups with little accuracy loss (Lebedev et al., 2015). Tucker and tensor-train layers compress fully connected weights (Novikov et al., 2015).
- Tensor networks and tensor trains (Oseledets, 2011) store a -way tensor with numbers instead of . They beat the curse of dimensionality in high-dimensional PDEs, quantum many-body physics and uncertainty quantification.
- Multi-sensor and spatiotemporal data. Sensor × time × trial tensors come from robot force/torque logs, EEG, and spacecraft telemetry. CP finds shared components, and Tucker/HOSVD compresses smooth fields (Practice Code 5 gets 93× on a heating panel). Tensor completion fills missing sensor readings.
- Knowledge graphs and recommenders. Entity × relation × entity tensors are factorized by RESCAL, ComplEx and TuckER to predict missing facts. User × item × context tensors are used for recommendations.
- Deep learning frameworks compile einsum expressions and choose the contraction order. For long chains, finding the cheapest order is itself hard;
opt_einsumsearches for it.
References
- A. Einstein (1916). “Die Grundlage der allgemeinen Relativitätstheorie.” Annalen der Physik, 49.
- J. Kruskal (1977). “Three-way arrays: rank and uniqueness of trilinear decompositions.” Linear Algebra and its Applications, 18.
- L. De Lathauwer, B. De Moor, J. Vandewalle (2000). “A multilinear singular value decomposition.” SIAM J. Matrix Anal. Appl., 21(4).
- V. de Silva, L.-H. Lim (2008). “Tensor rank and the ill-posedness of the best low-rank approximation problem.” SIAM J. Matrix Anal. Appl., 30(3).
- T. Kolda, B. Bader (2009). “Tensor decompositions and applications.” SIAM Review, 51(3).
- I. Oseledets (2011). “Tensor-train decomposition.” SIAM J. Scientific Computing, 33(5).
- C. Hillar, L.-H. Lim (2013). “Most tensor problems are NP-hard.” Journal of the ACM, 60(6).
- V. Lebedev, Y. Ganin, M. Rakhuba, I. Oseledets, V. Lempitsky (2015). “Speeding-up convolutional neural networks using fine-tuned CP-decomposition.” ICLR.
- A. Novikov, D. Podoprikhin, A. Osokin, D. Vetrov (2015). “Tensorizing neural networks.” NeurIPS.