Tensor Decomposition
Tensor decomposition breaks a big multi-way array into products of small vectors, matrices, and a tiny core, so hidden structure shows and parameters shrink from millions to thousands.
Why Does This Exist?
Video (frames by height by width by channels), recommendation logs (user by item by context by time), and 4D convolution filters all arrive as multi-way arrays. A fourth-order tensor with 100 entries per axis holds numbers. That exponential growth is the curse of dimensionality, and it makes raw storage and full learning hopeless.
Flattening the array into a matrix so ordinary linear algebra applies destroys the structure that spans several axes at once. A time pattern that only exists when user, item, and context move together gets shredded into unrelated rows.
Tensor decomposition is the multilinear generalization of singular value decomposition. It writes the big array as products of small factor vectors and matrices plus a tiny core, cutting parameters from to , separating multi-way noise, and compressing embedding and convolution weights. This page covers the two classic forms, CP and Tucker. Containers and shapes live in vectors, matrices and tensors, and index notation for the contractions lives in einsum and tensor shapes.
The one idea you need first
A matrix of rank 1 is an outer product of two vectors: , so . A rank-1 order-3 tensor is the same idea once more: . Every entry is a triple product, so the whole block is fixed by only numbers.
Think of It Like This
A sculpture from three ribbon profiles
Imagine a solid sculpture inside a voxel cube. Recording every voxel costs numbers.
Suppose the sculpture has coherent themes. Instead of cataloguing voxels, you record three 1D ribbons: a height profile, a width profile, and a depth profile. Multiplying the three ribbons paints one full rank-one block. Adding such blocks rebuilds the sculpture. With you store numbers instead of , about a saving.
Where it stops: white noise has no themes. Random high-dimensional static cannot be split into a few ribbons without large error, and forcing a small there just blurs the noise into fake structure.
How It Actually Works
Write for an order-3 array: three indices, one number per triple . The symbol below is the outer product, and is multiplication along mode only.
CP as a sum of rank-one blocks
CP (CANDECOMP/PARAFAC) approximates the tensor as rank-one pieces:
Here , , are the factor vectors, stacked into factor matrices , , . The smallest giving equality is the tensor rank. Parameters fall from to .
Tucker as a small core plus three matrices
Tucker (higher-order SVD) lets each mode have its own rank. A small dense core mixes orthogonal factors , , :
CP is the constrained special case where the core is superdiagonal with . Fitting usually runs Alternating Least Squares: fix two factors, solve least squares for the third with the Khatri-Rao product, and cycle.
Worked example
Take a tensor with rank and factors:
Each entry is . Front slice (): , , , . Back slice (): , , , . So the slices are and , exactly the numbers drawn above.
Check one Alternating Least Squares recovery. The Khatri-Rao product is . Mode-1 unfolding puts row 0 as and row 1 as , so solving returns exactly.
Code
Rebuild the worked tensor from its factors and verify the least-squares recovery:
import numpy as np
def cp_reconstruct_3d( factor_a: np.ndarray, factor_b: np.ndarray, factor_c: np.ndarray) -> np.ndarray: """Rebuild an order-3 tensor from rank-R factor matrices.""" return np.einsum("ir,jr,kr->ijk", factor_a, factor_b, factor_c)
def khatri_rao(a: np.ndarray, b: np.ndarray) -> np.ndarray: """Column-wise Khatri-Rao product of two matrices.""" return np.column_stack([np.kron(a[:, col], b[:, col]) for col in range(a.shape[1])])
a = np.array([[2.0], [1.0]])b = np.array([[3.0], [4.0]])c = np.array([[1.0], [2.0]])
tensor_x = cp_reconstruct_3d(a, b, c)print(tensor_x[:, :, 0])# -> [[6. 8.]# -> [3. 4.]]print(tensor_x[:, :, 1])# -> [[12. 16.]# -> [ 6. 8.]]
x_mode1 = tensor_x.reshape(2, -1)kr_cb = khatri_rao(c, b)a_back = x_mode1 @ kr_cb @ np.linalg.inv(kr_cb.T @ kr_cb)print(np.allclose(a, a_back))# -> TrueThe printed slices match the hand computation, and the recovery returns True.
Watch Out For
Tensor rank is hard and CP fits can degenerate
Matrix rank comes from one SVD. Tensor rank for order 3 and above is NP-hard, and the set of rank- tensors is not closed: two rank-one terms can blow up with opposite signs while chasing an infimum that is never attained. The symptom is factor entries growing to huge magnitudes while the fit barely improves.
Fix it with regularization on the factors during Alternating Least Squares. When stability matters more than a diagonal core, use Tucker or Tensor Train, whose truncations rest on ordinary SVDs.
Unfolding first and calling it a tensor method
Unfolding the block above into a matrix and running plain SVD finds a low-rank matrix, not a low-rank tensor. It mixes the and axes into one flat index, so a pattern that needs multilinearly can demand higher matrix rank after flattening, and the recovered factors no longer separate per mode.
Keep each mode intact until the decomposition assigns it a factor. If a library call takes only one matrix, it is matrix factorization wearing a tensor label.
The Quick Version
- CP writes an -way tensor as a sum of rank-one outer products, cutting parameters from to .
- Tucker writes it as a small dense core times one factor matrix per mode, with CP as its superdiagonal special case.
- The fixture from , , rebuilds slices with 6, 8, 3, 4 and 12, 16, 6, 8.
- Flattening first destroys multi-way structure that the decomposition is meant to keep.
- Tensor rank is NP-hard and CP fits can degenerate, so regularize factors or switch to Tucker or Tensor Train.