Tensor Decomposition
Tensor decomposition breaks down high-dimensional arrays into products of simpler matrices and core tensors, exposing latent patterns while dramatically compressing parameters.
Why Does This Exist?
Machine learning models routinely process multidimensional data: video streams (frames height width channels), recommendation logs (user item context time), multi-relational knowledge graphs, and 4D convolutional weight filters. Storing and learning over raw multidimensional grids quickly triggers the curse of dimensionality — a fourth-order tensor with 100 entries per dimension requires floating-point parameters.
The naive workaround is flattening the tensor into a 2D matrix to apply standard linear algebra. Flattening, however, destroys the spatial and temporal correlations that span multiple modes simultaneously. It breaks multi-way interactions and forces matrix factorization algorithms to learn redundant parameters across unrolled indices.
Tensor decomposition provides the multilinear generalization of singular value decomposition and low-rank matrix approximation. By expressing high-order arrays as products of low-dimensional latent factor matrices and small core interaction tensors, tensor decomposition slashes parameter footprints from exponential to linear , separates multi-modal noise, and enables parameter-efficient neural network compression.
Think of It Like This
A 3D Lego sculpture vs. orthogonal profile ribbons
Imagine building a solid 3D sculpture inside a block grid. Storing the exact color and density of every individual voxel requires keeping track of distinct numbers.
Suppose, however, that the sculpture has coherent structural themes. Instead of cataloging every individual voxel in the cube, you capture three separate 1D ribbon profiles: a vertical profile across height, a horizontal profile across width, and a depth profile along thickness. Multiplying these three ribbons together paints an entire rank-one 3D block. By summing a modest collection of different ribbon sets, you rebuild the entire sculpture.
Instead of storing voxel entries, you store only ribbon values. If , that is just numbers — a reduction in storage with virtually no loss in structural clarity.
The analogy stops when data consists of unstructured white noise or highly irregular local spikes: true random high-dimensional noise cannot be separated into low-rank 1D ribbons without significant reconstruction error.
How It Actually Works
CP and Tucker Formulations
A tensor is an order-3 multi-way array. The two foundational tensor decomposition families are CANDECOMP/PARAFAC (CP) and Tucker decomposition.
In CP decomposition, a tensor is approximated as a finite sum of rank-one tensors, where each rank-one component is an outer product of vectors:
where , , and . Stacking these vectors column-wise produces factor matrices , , and . For any individual coordinate :
The smallest integer for which this equality holds exactly is the tensor rank. The parameter footprint collapses from down to .
In Tucker decomposition (higher-order SVD), the factors are not forced to share a single rank . Instead, a small dense core tensor governs cross-mode interactions between orthogonal factor matrices , , and :
where denotes the mode- tensor-matrix product. CP decomposition is a constrained special case of Tucker where the core tensor is strictly superdiagonal ( with non-zero entries only when ).
To fit factor matrices from an observed tensor , standard practice uses Alternating Least Squares (ALS). ALS fixes two factor matrices (e.g., and ) and solves a linear least squares regression for the third factor using the Khatri-Rao product , cycling through all modes until convergence:
where is the mode-1 matrix unfolding of , and denotes the Moore-Penrose pseudoinverse.
Worked Example
Consider a third-order tensor . We fit a rank-1 () CP decomposition defined by three factor vectors:
Let us compute the exact numerical reconstruction for all 8 elements using :
-
Front slice (, where ):
-
Back slice (, where ):
Now consider an ALS step to solve for given the observed slices and fixed factors and . The Khatri-Rao product has elements: , , , . So .
Mode-1 unfolding arranges row 0 as . Notice that . Row 1 is . Solving recovers exactly.
Code
import numpy as np
def cp_reconstruct_3d( factor_a: np.ndarray, factor_b: np.ndarray, factor_c: np.ndarray) -> np.ndarray: """Reconstruct an order-3 tensor from CP factor matrices via outer products.
Shapes: factor_a: (I, R) factor_b: (J, R) factor_c: (K, R) Returns: X: (I, J, K) """ # einsum contracts mode factors along rank index r 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.""" r = a.shape[1] cols = [np.kron(a[:, col], b[:, col]) for col in range(r)] return np.column_stack(cols)
# Factor vectors for rank R=1a = np.array([[2.0], [1.0]]) # shape (2, 1)b = np.array([[3.0], [4.0]]) # shape (2, 1)c = np.array([[1.0], [2.0]]) # shape (2, 1)
tensor_x = cp_reconstruct_3d(a, b, c)print(tensor_x.shape)# -> (2, 2, 2)
print(tensor_x[:, :, 0])# -> [[6. 8.]# -> [3. 4.]]
print(tensor_x[:, :, 1])# -> [[12. 16.]# -> [ 6. 8.]]
# Unfold mode-1 and verify Khatri-Rao ALS recoveryx_mode1 = tensor_x.reshape(tensor_x.shape[0], -1) # (2, 4)kr_cb = khatri_rao(c, b) # (4, 1)a_recovered = x_mode1 @ kr_cb @ np.linalg.inv(kr_cb.T @ kr_cb)
print(np.allclose(a, a_recovered))# -> TrueWatch Out For
Tensor rank computation is NP-hard and rank approximations can be ill-posed
In matrix linear algebra, computing rank and finding the optimal low-rank matrix approximation is solved in polynomial time via SVD under the Eckart-Young-Mirsky theorem. For order-3 and higher tensors, computing tensor rank is proven to be NP-hard.
Even worse, the set of rank- tensors is not topologically closed for . In iterative CP algorithms, this leads to the infamous "degeneracy problem": two or more rank-one components grow to massive magnitudes with opposing signs, attempting to cancel each other out while chasing an infimum that does not exist as a rank- tensor.
Fix: When fitting CP models, always impose regularisation on factor weights during Alternating Least Squares updates. If stable orthogonal decompositions are essential, choose Tucker decomposition or the Tensor Train (TT) format, whose truncation steps rely strictly on stable sequential SVD operations.
The Quick Version
- CP decomposition factorizes an -way tensor into a sum of rank-1 vector outer products, reducing parameter count from to .
- Tucker decomposition provides a multilinear SVD with orthogonal factor matrices along each mode governed by a dense core interaction tensor.
- Unfolding multi-way tensors into matrices for standard SVD discards multilinear geometry and fails to capture higher-order dependencies.
- Unlike matrix SVD, computing general tensor rank is NP-hard, and CP-ALS fitting requires norm regularization to avoid divergent degenerate components.
- Modern deep networks use tensor decomposition (including Tensor Trains and Tucker) to compress multi-million parameter embedding layers and convolution weights.