Skip to content
AI360XpertBeta

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.

CP rebuilds a 2 by 2 by 2 tensor from vectors 2 1, 3 4, and 1 2 into slices 6 8 3 4 and 12 16 6 8, while Tucker keeps the same idea with a small dense core
CP rebuilds a 2 by 2 by 2 tensor from vectors 2 1, 3 4, and 1 2 into slices 6 8 3 4 and 12 16 6 8, while Tucker keeps the same idea with a small dense core

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 1004=100,000,000100^4 = 100{,}000{,}000 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 O(IN)O(I^N) to O(N⋅I⋅R)O(N \cdot I \cdot R), 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: M=abTM = a b^T, so Mij=aibjM_{ij} = a_i b_j. A rank-1 order-3 tensor is the same idea once more: Xijk=aibjck\mathcal{X}_{ijk} = a_i b_j c_k. Every entry is a triple product, so the whole I×J×KI \times J \times K block is fixed by only I+J+KI + J + K numbers.

Think of It Like This

A sculpture from three ribbon profiles

Imagine a solid sculpture inside a 100×100×100100 \times 100 \times 100 voxel cube. Recording every voxel costs 1,000,0001{,}000{,}000 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 RR such blocks rebuilds the sculpture. With R=10R = 10 you store 3×100×10=3,0003 \times 100 \times 10 = 3{,}000 numbers instead of 1,000,0001{,}000{,}000, about a 333×333\times 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 RR there just blurs the noise into fake structure.

How It Actually Works

Write X∈RI×J×K\mathcal{X} \in \mathbb{R}^{I \times J \times K} for an order-3 array: three indices, one number per triple (i,j,k)(i, j, k). The symbol ∘\circ below is the outer product, and ×n\times_n is multiplication along mode nn only.

CP as a sum of rank-one blocks

CP (CANDECOMP/PARAFAC) approximates the tensor as RR rank-one pieces:

X≈∑r=1Rar∘br∘cr,Xijk≈∑r=1RAirBjrCkr\mathcal{X} \approx \sum_{r=1}^{R} \mathbf{a}_r \circ \mathbf{b}_r \circ \mathbf{c}_r, \qquad \mathcal{X}_{ijk} \approx \sum_{r=1}^{R} A_{ir} B_{jr} C_{kr}

Here ar∈RI\mathbf{a}_r \in \mathbb{R}^I, br∈RJ\mathbf{b}_r \in \mathbb{R}^J, cr∈RK\mathbf{c}_r \in \mathbb{R}^K are the factor vectors, stacked into factor matrices A\mathbf{A}, B\mathbf{B}, C\mathbf{C}. The smallest RR giving equality is the tensor rank. Parameters fall from IJKIJK to R(I+J+K)R(I + J + K).

Tucker as a small core plus three matrices

Tucker (higher-order SVD) lets each mode have its own rank. A small dense core G∈RP×Q×S\mathcal{G} \in \mathbb{R}^{P \times Q \times S} mixes orthogonal factors U∈RI×P\mathbf{U} \in \mathbb{R}^{I \times P}, V∈RJ×Q\mathbf{V} \in \mathbb{R}^{J \times Q}, 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)

CP is the constrained special case where the core is superdiagonal with P=Q=S=RP = Q = S = R. 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 2×2×22 \times 2 \times 2 tensor with rank R=1R = 1 and factors:

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}

Each entry is Xijk=aibjck\mathcal{X}_{ijk} = a_i b_j c_k. Front slice (c0=1c_0 = 1): 2×3×1=62 \times 3 \times 1 = 6, 2×4×1=82 \times 4 \times 1 = 8, 1×3×1=31 \times 3 \times 1 = 3, 1×4×1=41 \times 4 \times 1 = 4. Back slice (c1=2c_1 = 2): 2×3×2=122 \times 3 \times 2 = 12, 2×4×2=162 \times 4 \times 2 = 16, 1×3×2=61 \times 3 \times 2 = 6, 1×4×2=81 \times 4 \times 2 = 8. So the slices are [6834]\begin{bmatrix} 6 & 8 \\ 3 & 4 \end{bmatrix} and [121668]\begin{bmatrix} 12 & 16 \\ 6 & 8 \end{bmatrix}, exactly the numbers drawn above.

Check one Alternating Least Squares recovery. The Khatri-Rao product m=c⊙b\mathbf{m} = \mathbf{c} \odot \mathbf{b} is [1×3,1×4,2×3,2×4]T=[3,4,6,8]T[1 \times 3, 1 \times 4, 2 \times 3, 2 \times 4]^T = [3, 4, 6, 8]^T. Mode-1 unfolding puts row 0 as [6,8,12,16]=2mT[6, 8, 12, 16] = 2\mathbf{m}^T and row 1 as [3,4,6,8]=1mT[3, 4, 6, 8] = 1\mathbf{m}^T, so solving X(1)=amT\mathbf{X}_{(1)} = \mathbf{a}\mathbf{m}^T returns a=[2,1]T\mathbf{a} = [2, 1]^T 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))# -> True

The 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-RR 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 L2L_2 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 2×2×22 \times 2 \times 2 block above into a 2×42 \times 4 matrix and running plain SVD finds a low-rank matrix, not a low-rank tensor. It mixes the JJ and KK axes into one flat index, so a pattern that needs R=1R = 1 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 NN-way tensor as a sum of RR rank-one outer products, cutting parameters from O(IN)O(I^N) to O(NIR)O(NIR).
  • Tucker writes it as a small dense core times one factor matrix per mode, with CP as its superdiagonal special case.
  • The 2×2×22 \times 2 \times 2 fixture from [2,1][2, 1], [3,4][3, 4], [1,2][1, 2] 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.