# Sparse Coding: Autoencoder Interpretation

### From Ufldl

## Sparse coding

In the sparse autoencoder, we tried to learn a set of weights *W* (and associated biases *b*) that would give us sparse features σ(*W**x* + *b*) useful in reconstructing the input *x*.

Sparse coding can be seen as a modification of the sparse autoencoder method in which we try to learn the set of features for some data "directly". Together with an associated basis for transforming the learned features from the feature space to the data space, we can then reconstruct the data from the learned features.

Formally, in sparse coding, we have some data *x* we would like to learn features on. In particular, we would like to learn *s*, a set of sparse features useful for representing the data, and *A*, a basis for transforming the features from the feature space to the data space. Our objective function is hence:

(If you are unfamiliar with the notation, refers to the L*k* norm of the *x* which is equal to . The L2 norm is the familiar Euclidean norm, while the L1 norm is the sum of absolute values of the elements of the vector)

The first term is the error in reconstructing the data from the features using the basis, and the second term is a sparsity penalty term to encourage the learned features to be sparse.

However, the objective function as it stands is not properly constrained - it is possible to reduce the sparsity cost (the second term) by scaling *A* by some constant and scaling *s* by the inverse of the same constant, without changing the error. Hence, we include the additional constraint that that for every column *A*_{j} of *A*,
. Our problem is thus:

Unfortunately, the objective function is non-convex, and hence impossible to optimize well using gradient-based methods. However, given *A*, the problem of finding *s* that minimizes *J*(*A*,*s*) is convex. Similarly, given *s*, the problem of finding *A* that minimizes *J*(*A*,*s*) is also convex. This suggests that we might try alternately optimizing for *A* for a fixed *s*, and then optimizing for *s* given a fixed *A*. It turns out that this works quite well in practice.

However, the form of our problem presents another difficulty - the constraint that cannot be enforced using simple gradient-based methods. Hence, in practice, this constraint is weakened to a "weight decay" term designed to keep the entries of *A* small. This gives us a new objective function:

(note that the third term, is the sum of squares of the entries of A, or )

This objective function presents one last problem - the L1 norm is not differentiable at 0, and hence poses a problem for gradient-based methods. While the problem can be solved using other non-gradient descent-based methods, we will "smooth out" the L1 norm using an approximation which will allow us to use gradient descent. To "smooth out" the L1 norm, we use in place of , where ε is a "smoothing parameter" which can also be interpreted as a sort of "sparsity parameter" (to see this, observe that when ε is large compared to *x*, the *x* + ε is dominated by ε, and taking the square root yields approximately ). This "smoothing" will come in handy later when considering topographic sparse coding below.

Our final objective function is hence:

(where is shorthand for )

This objective function can then be optimized iteratively, using the following procedure:

- Initialize
*A*randomly - Repeat until convergence
- Find
*s*that minimizes*J*(*A*,*s*) for the*A*found in the previous step - Find
*A*that minimizes*J*(*A*,*s*) for the*s*found in the previous step

- Find

Observe that with our modified objective function, the objective function *J*(*A*,*s*) given *s*, that is (the L1 term in *s* can be omitted since it is not a function of *A*) is simply a quadratic term in *A*, and hence has an easily derivable analytic solution in *A*. A quick way to derive this solution would be to use matrix calculus - some pages about matrix calculus can be found in the useful links section. Unfortunately, the objective function given *A* does not have a similarly nice analytic solution, so that minimization step will have to be carried out using gradient descent or similar optimization methods.

Optimizing for this objective function using the iterative method as above, will yield features (the basis vectors of *A*) similar to those learned using the sparse autoencoder. For more practical tips on implementing sparse coding, you may wish to refer to the sparse coding exercise. For assistance with deriving the gradients, you may wish to refer to Deriving gradients using the backpropagation idea.

## Topographic sparse coding

With sparse coding, we can learn a set of features useful for representing the data. However, drawing inspiration from the brain, we would like to learn a set of features that are "orderly" in some manner. For instance, consider visual features. As suggested earlier, the V1 cortex of the brain contains neurons which detect edges at particular orientations. However, these neurons are also organized into hypercolumns in which adjacent neurons detect edges at similar orientations. One neuron could detect a horizontal edge, its neighbors edges oriented slightly off the horizontal, and moving further along the hypercolumn, the neurons detect edges oriented further off the horizontal.

Inspired by this example, we would like to learn features which are similarly "topographically ordered". What does this imply for our learned features? Intuitively, if "adjacent" features are "similar", we would expect that if one feature is activated, its neighbors will also be activated to a lesser extent.

Concretely, suppose we (arbitrarily) organized our features into a square matrix. We would then like adjacent features in the matrix to similar. The way this is accomplished is to group these adjacent features together in the smoothed L1 penalty, so that instead of say , we use say instead, if we group in 3x3 regions. The grouping is usually overlapping, so that the 3x3 region starting at the 1st row and 1st column is one group, the 3x3 region starting at the 1st row and 2nd column is another group, and so on. Further, the grouping is also usually done wrapping around, as if the matrix were a torus, so that every feature is counted an equal number of times.

Hence, in place of the smoothed L1 penalty, we use the sum of smoothed L1 penalties over all the groups, so our new objective function is:

In practice, the "grouping" can be accomplished using a "grouping matrix" *V*, such that the *r*th row of *V* indicates which features are grouped in the *r*th group, so *V*_{r,c} = 1 if group *r* contains feature *c*. Thinking of the grouping as being achieved by a grouping matrix makes the computation of the gradients more intuitive. Using this grouping matrix, the objective function can be rewritten as:

(where is

∑ | ∑ | D_{r,c} |

r | c |

if we let )

This objective function can be optimized using the iterated method described in the earlier section. Topographic sparse coding will learn features similar to those learned by sparse coding, except that the features will now be "ordered" in some way.