# K-SVD

In applied mathematics, K-SVD is a dictionary learning algorithm for creating a dictionary for sparse representations, via a singular value decomposition approach. K-SVD is a generalization of the k-means clustering method, and it works by iteratively alternating between sparse coding the input data based on the current dictionary, and updating the atoms in the dictionary to better fit the data.[1][2] K-SVD can be found widely in use in applications such as image processing, audio processing, biology, and document analysis.

## Problem description

The goal of dictionary learning is to learn an overcomplete dictionary matrix ${\displaystyle D\in \mathbb {R} ^{n\times K}}$ that contains ${\displaystyle K}$ signal-atoms (in this notation, columns of ${\displaystyle D}$). A signal vector ${\displaystyle y\in \mathbb {R} ^{n}}$ can be represented, sparsely, as a linear combination of these atoms; to represent ${\displaystyle y}$, the representation vector ${\displaystyle x}$ should satisfy the exact condition ${\displaystyle y=Dx}$, or the approximate condition ${\displaystyle y\approx Dx}$, made precise by requiring that ${\displaystyle \|y-Dx\|_{p}\leq \epsilon }$ for some small value ε and some Lp norm. The vector ${\displaystyle x\in \mathbb {R} ^{K}}$ contains the representation coefficients of the signal ${\displaystyle y}$. Typically, the norm ${\displaystyle p}$ is selected as L1, L2, or L.

If ${\displaystyle n and D is a full-rank matrix, an infinite number of solutions are available for the representation problem. Hence, constraints should be set on the solution. Also, to ensure sparsity, the solution with the fewest nonzero coefficients is preferred. Thus, the sparsity representation is the solution of either

${\displaystyle (P_{0})\quad \min \limits _{x}\|x\|_{0}\qquad {\text{subject to }}y=Dx}$

or

${\displaystyle (P_{0,\epsilon })\quad \min \limits _{x}\|x\|_{0}\qquad {\text{subject to }}\|y-Dx\|_{2}\leq \epsilon }$

where the ${\displaystyle \|x\|_{0}}$ counts the nonzero entries in the vector ${\displaystyle x}$. (See the zero "norm".)

## K-SVD algorithm

K-SVD is a kind of generalization of K-means, as follows. The k-means clustering can be also regarded as a method of sparse representation. That is, finding the best possible codebook to represent the data samples ${\displaystyle \{y_{i}\}_{i=1}^{M}}$ by nearest neighbor, by solving

${\displaystyle \quad \min \limits _{D,X}\{\|Y-DX\|_{F}^{2}\}\qquad {\text{subject to }}\forall i,x_{i}=e_{k}{\text{ for some }}k.}$

which is equivalent to

${\displaystyle \quad \min \limits _{D,X}\{\|Y-DX\|_{F}^{2}\}\qquad {\text{subject to }}\quad \forall i,\|x_{i}\|_{0}=1}$.

The letter F denotes the Frobenius norm. The sparse representation term ${\displaystyle x_{i}=e_{k}}$ enforces K-means algorithm to use only one atom (column) in dictionary ${\displaystyle D}$. To relax this constraint, the target of the K-SVD algorithm is to represent signal as a linear combination of atoms in ${\displaystyle D}$.

The K-SVD algorithm follows the construction flow of the K-means algorithm. However, in contrary to K-means, in order to achieve a linear combination of atoms in ${\displaystyle D}$, the sparsity term of the constraint is relaxed so that the number of nonzero entries of each column ${\displaystyle x_{i}}$ can be more than 1, but less than a number ${\displaystyle T_{0}}$.

So, the objective function becomes

${\displaystyle \quad \min \limits _{D,X}\{\|Y-DX\|_{F}^{2}\}\qquad {\text{subject to }}\quad \forall i\;,\|x_{i}\|_{0}\leq T_{0}.}$

or in another objective form

${\displaystyle \quad \min \limits _{D,X}\sum _{i}\|x_{i}\|_{0}\qquad {\text{subject to }}\quad \forall i\;,\|Y-DX\|_{F}^{2}\leq \epsilon .}$

In the K-SVD algorithm, the ${\displaystyle D}$ is first fixed and the best coefficient matrix ${\displaystyle X}$ is found. As finding the truly optimal ${\displaystyle X}$ is impossible, we use an approximation pursuit method. Any algorithm such as OMP, the orthogonal matching pursuit can be used for the calculation of the coefficients, as long as it can supply a solution with a fixed and predetermined number of nonzero entries ${\displaystyle T_{0}}$.

After the sparse coding task, the next is to search for a better dictionary ${\displaystyle D}$. However, finding the whole dictionary all at a time is impossible, so the process is to update only one column of the dictionary ${\displaystyle D}$ each time, while fixing ${\displaystyle X}$. The update of the ${\displaystyle k}$-th column is done by rewriting the penalty term as

${\displaystyle \|Y-DX\|_{F}^{2}=\left\|Y-\sum _{j=1}^{K}d_{j}x_{T}^{j}\right\|_{F}^{2}=\left\|\left(Y-\sum _{j\neq k}d_{j}x_{T}^{j}\right)-d_{k}x_{T}^{k}\right\|_{F}^{2}=\|E_{k}-d_{k}x_{T}^{k}\|_{F}^{2}}$

where ${\displaystyle x_{T}^{k}}$ denotes the k-th row of X.

By decomposing the multiplication ${\displaystyle DX}$ into sum of ${\displaystyle K}$ rank 1 matrices, we can assume the other ${\displaystyle K-1}$ terms are assumed fixed, and the ${\displaystyle k}$-th remains unknown. After this step, we can solve the minimization problem by approximate the ${\displaystyle E_{k}}$ term with a ${\displaystyle rank-1}$ matrix using singular value decomposition, then update ${\displaystyle d_{k}}$ with it. However, the new solution of vector ${\displaystyle x_{T}^{k}}$ is very likely to be filled, because the sparsity constraint is not enforced.

To cure this problem, Define ${\displaystyle \omega _{k}}$ as

${\displaystyle \omega _{k}=\{i\mid 1\leq i\leq N,x_{T}^{k}(i)\neq 0\},}$

which points to examples ${\displaystyle \{y_{i}\}_{i=1}^{N}}$ that use atom ${\displaystyle d_{k}}$ (also the entries of ${\displaystyle x_{i}}$ that is nonzero). Then, define ${\displaystyle \Omega _{k}}$ as a matrix of size ${\displaystyle N\times |\omega _{k}|}$, with ones on the ${\displaystyle (i,\omega _{k}(i)){\text{-th}},}$ entries and zeros otherwise. When multiplying ${\displaystyle x_{R}^{k}=x_{T}^{k}\Omega _{k}}$, this shrinks the row vector ${\displaystyle x_{T}^{k}}$ by discarding the zero entries. Similarly, the multiplication ${\displaystyle Y_{k}^{R}=Y\Omega _{k}}$ is the subset of the examples that are current using the ${\displaystyle d_{k}}$ atom. The same effect can be seen on ${\displaystyle E_{k}^{R}=E_{k}\Omega _{k}}$.

So the minimization problem as mentioned before becomes

${\displaystyle \|E_{k}\Omega _{k}-d_{k}x_{T}^{k}\Omega _{k}\|_{F}^{2}=\|E_{k}^{R}-d_{k}x_{R}^{k}\|_{F}^{2}}$

and can be done by directly using SVD. SVD decomposes ${\displaystyle E_{k}^{R}}$ into ${\displaystyle U\Delta V^{T}}$. The solution for ${\displaystyle d_{k}}$ is the first column of U, the coefficient vector ${\displaystyle x_{R}^{k}}$ as the first column of ${\displaystyle V\times \Delta (1,1)}$. After updating the whole dictionary, the process then turns to iteratively solve X, then iteratively solve D.

## Limitations

Choosing an appropriate "dictionary" for a dataset is a non-convex problem, and K-SVD operates by an iterative update which does not guarantee to find the global optimum.[2] However, this is common to other algorithms for this purpose, and K-SVD works fairly well in practice.[2][better source needed]