Skip to content
AI360Xpert
Beta

Tensor Decomposition

Tensor decomposition breaks down high-dimensional arrays into products of simpler matrices and core tensors, exposing latent patterns while dramatically compressing parameters.

CP decomposition factors a 3D tensor into a sum of rank-one outer products, while Tucker decomposition compresses it into a small core tensor multiplied by factor matrices along each mode.
CP decomposition factors a 3D tensor into a sum of rank-one outer products, while Tucker decomposition compresses it into a small core tensor multiplied by factor matrices along each mode.

Why Does This Exist?

Machine learning models routinely process multidimensional data: video streams (frames ×\times height ×\times width ×\times channels), recommendation logs (user ×\times item ×\times context ×\times 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 1004=100,000,000100^4 = 100,000,000 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 O(IN)O(I^N) to linear O(N⋅I⋅R)O(N \cdot I \cdot R), 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 100×100×100100 \times 100 \times 100 block grid. Storing the exact color and density of every individual voxel requires keeping track of 1,000,0001,000,000 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 RR different ribbon sets, you rebuild the entire sculpture.

Instead of storing 1,000,0001,000,000 voxel entries, you store only 3×100×R3 \times 100 \times R ribbon values. If R=10R = 10, that is just 3,0003,000 numbers — a 333×333\times 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 X∈RI×J×K\mathcal{X} \in \mathbb{R}^{I \times J \times K} 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 RR rank-one tensors, where each rank-one component is an outer product ∘\circ of vectors:

X≈∑r=1Rar∘br∘cr\mathcal{X} \approx \sum_{r=1}^R \mathbf{a}_r \circ \mathbf{b}_r \circ \mathbf{c}_r

where ar∈RI\mathbf{a}_r \in \mathbb{R}^I, br∈RJ\mathbf{b}_r \in \mathbb{R}^J, and cr∈RK\mathbf{c}_r \in \mathbb{R}^K. Stacking these vectors column-wise produces factor matrices A∈RI×R\mathbf{A} \in \mathbb{R}^{I \times R}, B∈RJ×R\mathbf{B} \in \mathbb{R}^{J \times R}, and C∈RK×R\mathbf{C} \in \mathbb{R}^{K \times R}. For any individual coordinate (i,j,k)(i, j, k):

Xijk≈∑r=1RAirBjrCkr\mathcal{X}_{ijk} \approx \sum_{r=1}^R A_{ir} B_{jr} C_{kr}

The smallest integer RR for which this equality holds exactly is the tensor rank. The parameter footprint collapses from I⋅J⋅KI \cdot J \cdot K down to R(I+J+K)R(I + J + K).

In Tucker decomposition (higher-order SVD), the factors are not forced to share a single rank RR. Instead, a small dense core tensor G∈RP×Q×S\mathcal{G} \in \mathbb{R}^{P \times Q \times S} governs cross-mode interactions between orthogonal factor matrices U∈RI×P\mathbf{U} \in \mathbb{R}^{I \times P}, V∈RJ×Q\mathbf{V} \in \mathbb{R}^{J \times Q}, and W∈RK×S\mathbf{W} \in \mathbb{R}^{K \times S}:

X≈G×1U×2V×3W=∑p=1P∑q=1Q∑s=1SGpqs(up∘vq∘ws)\mathcal{X} \approx \mathcal{G} \times_1 \mathbf{U} \times_2 \mathbf{V} \times_3 \mathbf{W} = \sum_{p=1}^P \sum_{q=1}^Q \sum_{s=1}^S \mathcal{G}_{pqs} (\mathbf{u}_p \circ \mathbf{v}_q \circ \mathbf{w}_s)

where ×n\times_n denotes the mode-nn tensor-matrix product. CP decomposition is a constrained special case of Tucker where the core tensor G\mathcal{G} is strictly superdiagonal (P=Q=S=RP = Q = S = R with non-zero entries only when p=q=sp = q = s).

To fit factor matrices from an observed tensor X\mathcal{X}, standard practice uses Alternating Least Squares (ALS). ALS fixes two factor matrices (e.g., B\mathbf{B} and C\mathbf{C}) and solves a linear least squares regression for the third factor A\mathbf{A} using the Khatri-Rao product C⊙B\mathbf{C} \odot \mathbf{B}, cycling through all modes until convergence:

X(1)≈A(C⊙B)T  ⟹  A=X(1)[(C⊙B)T]†\mathbf{X}_{(1)} \approx \mathbf{A} (\mathbf{C} \odot \mathbf{B})^T \implies \mathbf{A} = \mathbf{X}_{(1)} \left[(\mathbf{C} \odot \mathbf{B})^T\right]^\dagger

where X(1)\mathbf{X}_{(1)} is the mode-1 matrix unfolding of X\mathcal{X}, and †\dagger denotes the Moore-Penrose pseudoinverse.

Worked Example

Consider a 2×2×22 \times 2 \times 2 third-order tensor X\mathcal{X}. We fit a rank-1 (R=1R = 1) CP decomposition defined by three factor vectors:

a=[21],b=[34],c=[12]\mathbf{a} = \begin{bmatrix} 2 \\ 1 \end{bmatrix}, \quad \mathbf{b} = \begin{bmatrix} 3 \\ 4 \end{bmatrix}, \quad \mathbf{c} = \begin{bmatrix} 1 \\ 2 \end{bmatrix}

Let us compute the exact numerical reconstruction for all 8 elements using Xijk=ai⋅bj⋅ck\mathcal{X}_{ijk} = a_i \cdot b_j \cdot c_k:

  1. Front slice (k=0k = 0, where c0=1c_0 = 1):

    • X0,0,0=a0⋅b0⋅c0=2×3×1=6\mathcal{X}_{0,0,0} = a_0 \cdot b_0 \cdot c_0 = 2 \times 3 \times 1 = 6
    • X0,1,0=a0⋅b1⋅c0=2×4×1=8\mathcal{X}_{0,1,0} = a_0 \cdot b_1 \cdot c_0 = 2 \times 4 \times 1 = 8
    • X1,0,0=a1⋅b0⋅c0=1×3×1=3\mathcal{X}_{1,0,0} = a_1 \cdot b_0 \cdot c_0 = 1 \times 3 \times 1 = 3
    • X1,1,0=a1⋅b1⋅c0=1×4×1=4\mathcal{X}_{1,1,0} = a_1 \cdot b_1 \cdot c_0 = 1 \times 4 \times 1 = 4
  2. Back slice (k=1k = 1, where c1=2c_1 = 2):

    • X0,0,1=a0⋅b0⋅c1=2×3×2=12\mathcal{X}_{0,0,1} = a_0 \cdot b_0 \cdot c_1 = 2 \times 3 \times 2 = 12
    • X0,1,1=a0⋅b1⋅c1=2×4×2=16\mathcal{X}_{0,1,1} = a_0 \cdot b_1 \cdot c_1 = 2 \times 4 \times 2 = 16
    • X1,0,1=a1⋅b0⋅c1=1×3×2=6\mathcal{X}_{1,0,1} = a_1 \cdot b_0 \cdot c_1 = 1 \times 3 \times 2 = 6
    • X1,1,1=a1⋅b1⋅c1=1×4×2=8\mathcal{X}_{1,1,1} = a_1 \cdot b_1 \cdot c_1 = 1 \times 4 \times 2 = 8

Now consider an ALS step to solve for a\mathbf{a} given the observed slices and fixed factors b\mathbf{b} and c\mathbf{c}. The Khatri-Rao product m=c⊙b\mathbf{m} = \mathbf{c} \odot \mathbf{b} has elements: m0=c0⋅b0=1×3=3m_0 = c_0 \cdot b_0 = 1 \times 3 = 3, m1=c0⋅b1=1×4=4m_1 = c_0 \cdot b_1 = 1 \times 4 = 4, m2=c1⋅b0=2×3=6m_2 = c_1 \cdot b_0 = 2 \times 3 = 6, m3=c1⋅b1=2×4=8m_3 = c_1 \cdot b_1 = 2 \times 4 = 8. So m=[3,4,6,8]T\mathbf{m} = [3, 4, 6, 8]^T.

Mode-1 unfolding X(1)\mathbf{X}_{(1)} arranges row 0 as [X0,0,0,X0,1,0,X0,0,1,X0,1,1]=[6,8,12,16][\mathcal{X}_{0,0,0}, \mathcal{X}_{0,1,0}, \mathcal{X}_{0,0,1}, \mathcal{X}_{0,1,1}] = [6, 8, 12, 16]. Notice that [6,8,12,16]=2×[3,4,6,8]=2mT[6, 8, 12, 16] = 2 \times [3, 4, 6, 8] = 2 \mathbf{m}^T. Row 1 is [3,4,6,8]=1×[3,4,6,8]=1mT[3, 4, 6, 8] = 1 \times [3, 4, 6, 8] = 1 \mathbf{m}^T. Solving X(1)=amT\mathbf{X}_{(1)} = \mathbf{a} \mathbf{m}^T recovers a=[2,1]T\mathbf{a} = [2, 1]^T 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))# -> True

Watch 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-RR tensors is not topologically closed for R≥2R \ge 2. 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-RR tensor.

Fix: When fitting CP models, always impose L2L_2 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 NN-way tensor into a sum of RR rank-1 vector outer products, reducing parameter count from O(IN)O(I^N) to O(N⋅I⋅R)O(N \cdot I \cdot R).
  • 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.