# 稀疏编码自编码表达

 Revision as of 05:39, 8 March 2013 (view source)Kandeng (Talk | contribs)← Older edit Revision as of 07:01, 8 March 2013 (view source)Kandeng (Talk | contribs) Newer edit → Line 1: Line 1: - [原文] + == Sparse coding == - == 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 σ(Wx + b) useful in reconstructing the input x. + In the sparse autoencoder, we tried to learn a set of weights $W$ (and associated biases $b$) that would give us sparse features $\sigma(Wx + b)$ useful in reconstructing the input $x$. - [初译] + [[File:STL_SparseAE.png | 240px]] - 稀疏编码 + 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. - 在稀疏自编码中，为了用稀疏特征σ(Wx + b)重新表示输入数据X需要学习权重系数W（以及对应的偏移量b）。 + 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: - [一审] + :$+ J(A, s) = \lVert As - x \rVert_2^2 + \lambda \lVert s \rVert_1 +$ - 稀疏编码 + (If you are unfamiliar with the notation, $\lVert x \rVert_k$ refers to the L$k$ norm of the $x$ which is equal to $\left( \sum{ \left| x_i^k \right| } \right) ^{\frac{1}{k}}$. The L2 norm is the familiar Euclidean norm, while the L1 norm is the sum of absolute values of the elements of the vector) - 在稀疏自编码中，为了用稀疏特征σ(Wx + b)重新表示输入数据X需要学习权重系数W（以及对应的偏移量b）。 + 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. - [[File:Sparse coding.png | 300px | link=]] + 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$, + $A_j^TA_j \le 1$. Our problem is thus: - [原文] + :$+ \begin{array}{rcl} + {\rm minimize} & \lVert As - x \rVert_2^2 + \lambda \lVert s \rVert_1 \\ + {\rm s.t.} & A_j^TA_j \le 1 \; \forall j \\ + \end{array} +$ - 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. + 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 $A_j^TA_j \le 1 \; \forall j$ 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: - 稀疏编码可以看作是稀疏自编码方法的一个变形，该方法试图直接学习数据的特征集。利用偏移量将待学习特征集从特征空间转化到数据空间，实现了待学习特征集数据的重新表示。 + :$+ J(A, s) = \lVert As - x \rVert_2^2 + \lambda \lVert s \rVert_1 + \gamma \lVert A \rVert_2^2 +$ - [一审] + (note that the third term, $\lVert A \rVert_2^2$ is simply the sum of squares of the entries of A, or $\sum_r{\sum_c{A_{rc}^2}}$) - 稀疏编码可以看作是稀疏自编码方法的一个变形，该方法试图直接学习数据的特征集。利用连带基将学习特征集从特征空间转化到数据空间，就可以从学到的特征中重建数据。 + 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 $\sqrt{x + \epsilon}$ in place of $\left| x \right|$, where $\epsilon$ is a "smoothing parameter" which can also be interpreted as a sort of "sparsity parameter" (to see this, observe that when $\epsilon$ is large compared to $x$, the $x + \epsilon$ is dominated by $\epsilon$, and taking the square root yields approximately $\sqrt{\epsilon}$). This "smoothing" will come in handy later when considering topographic sparse coding below. - [原文] + Our final objective function is hence: - 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: + :$+ J(A, s) = \lVert As - x \rVert_2^2 + \lambda \sqrt{s^2 + \epsilon} + \gamma \lVert A \rVert_2^2 +$ - [初译] + (where $\sqrt{s^2 + \epsilon}$ is shorthand for $\sum_k{\sqrt{s_k^2 + \epsilon}}$) - 在稀疏编码中，通常有很多数据x供我们进行特征学习。例如：s是一个用于表示数据的稀疏特征集，A是特征集从特征空间转换到数据空间的基。因此，为了计算s和A构建如下目标函数： + This objective function can then be optimized iteratively, using the following procedure: +
+
1. Initialize $A$ randomly +
2. Repeat until convergence +
+
1. Find the $s$ that minimizes $J(A, s)$ for the $A$ found in the previous step +
2. Solve for the $A$ that minimizes $J(A, s)$ for the $s$ found in the previous step +
+
- [一审] + Observe that with our modified objective function, the objective function $J(A, s)$ given $s$, that is $J(A; s) = \lVert As - x \rVert_2^2 + \gamma \lVert A \rVert_2^2$ (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 | 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. - 在稀疏编码中，对于从数据x中进行特征学习的情况。例如学习一个用于表示数据的稀疏特征集s，和一个将特征从特征空间转换到数据空间的基A,我们可以构建如下目标函数： + In theory, optimizing for this objective function using the iterative method as above should (eventually) yield features (the basis vectors of $A$) similar to those learned using the sparse autoencoder. However, in practice, there are quite a few tricks required for better convergence of the algorithm, and these tricks are described in greater detail in the later section on [[ Sparse Coding: Autoencoder Interpretation#Sparse coding in practice | sparse coding in practice]]. Deriving the gradients for the objective function may be slightly tricky as well, and using matrix calculus or [[Deriving gradients using the backpropagation idea | using the backpropagation intuition]] can be helpful. - [原文] + == Topographic sparse coding == - :$J(A, s) = \lVert As - x \rVert_2^2 + \lambda \lVert s \rVert_1$ + 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. - (If you are unfamiliar with the notation,$\lVert x \rVert_k$ refers to the Lk norm of the x which is equal to $\sum \mid x^k_i\mid ^\frac{1}{k}$. The L2 norm is the familiar Euclidean norm, while the L1 norm is the sum of absolute values of the elements of the vector) + - [初译] + 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. - 在稀疏编码中，通常有很多数据x供我们进行特征学习。例如：s是一个用于表示数据的稀疏特征集，A是特征集从特征空间转换到数据空间的基。因此，为了计算s和A构建如下目标函数： + Concretely, suppose we (arbitrarily) organized our features into a square matrix. We would then like adjacent features in the matrix to be similar. The way this is accomplished is to group these adjacent features together in the smoothed L1 penalty, so that instead of say $\sqrt{s_{1,1}^2 + \epsilon}$, we use say $\sqrt{s_{1,1}^2 + s_{1,2}^2 + s_{1,3}^2 + s_{2,1}^2 + s_{2,2}^2 + s_{3,2}^2 + s_{3,1}^2 + s_{3,2}^2 + s_{3,3}^2 + \epsilon}$ 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: - 在稀疏编码中，对于从数据x中进行特征学习的情况。例如学习一个用于表示数据的稀疏特征集s，和一个将特征从特征空间转换到数据空间的基A,我们可以构建如下目标函数： + :$+ J(A, s) = \lVert As - x \rVert_2^2 + \lambda \sum_{\text{all groups } g}{\sqrt{ \left( \sum_{\text{all } s \in g}{s^2} \right) + \epsilon} } + \gamma \lVert A \rVert_2^2 +$ + 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: - :$J(A, s) = \lVert As - x \rVert_2^2 + \lambda \lVert s \rVert_1$ + :$+ J(A, s) = \lVert As - x \rVert_2^2 + \lambda \sum{ \sqrt{Vss^T + \epsilon} } + \gamma \lVert A \rVert_2^2 +$ + + (where $\sum{ \sqrt{Vss^T + \epsilon} }$ is $\sum_r{ \sum_c { D_{r, c} } }$ if we let $D = \sqrt{Vss^T + \epsilon}$) + + 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. + + == Sparse coding in practice == + + As suggested in the earlier sections, while the theory behind sparse coding is quite simple, writing a good implementation that actually works and converges reasonably quickly to good optima requires a bit of finesse. + + Recall the simple iterative algorithm proposed earlier: +
+
1. Initialize $A$ randomly +
2. Repeat until convergence +
+
1. Find the $s$ that minimizes $J(A, s)$ for the $A$ found in the previous step +
2. Solve for the $A$ that minimizes $J(A, s)$ for the $s$ found in the previous step +
+
+ + It turns out that running this algorithm out of the box will not produce very good results, if any results are produced at all. There are two main tricks to achieve faster and better convergence: +
+
1. Batching examples into "mini-batches" +
2. Good initialization of $s$ +
+ + === Batching examples into mini-batches === + + If you try running the simple iterative algorithm on a large dataset of say 10 000 patches at one go, you will find that each iteration takes a long time, and the algorithm may hence take a long time to converge. To increase the rate of convergence, you can instead run the algorithm on mini-batches instead. To do this, instead of running the algorithm on all 10 000 patches, in each iteration, select a mini-batch - a (different) random subset of say 2000 patches from the 10 000 patches - and run the algorithm on that mini-batch for the iteration instead. This accomplishes two things - firstly, it speeds up each iteration, since now each iteration is operating on 2000 rather than 10 000 patches; secondly, and more importantly, it increases the rate of convergence [[(TODO]]: explain why). + + === Good initialization of $s$ === + + Another important trick in obtaining faster and better convergence is good initialization of the feature matrix $s$ before using gradient descent (or other methods) to optimize for the objective function for $s$ given $A$. In practice, initializing $s$ randomly at each iteration can result in poor convergence unless a good optima is found for $s$ before moving on to optimize for $A$. A better way to initialize $s$ is the following: +
+
1. Set $s \leftarrow W^Tx$ (where $x$ is the matrix of patches in the mini-batch) +
2. For each feature in $s$ (i.e. each column of $s$), divide the feature by the norm of the corresponding basis vector in $A$. That is, if $s_{r, c}$ is the $r$th feature for the $c$th example, and $A_c$ is the $c$th basis vector in $A$, then set $s_{r, c} \leftarrow \frac{ s_{r, c} } { \lVert A_c \rVert }.$ +
+ + Very roughly and informally speaking, this initialization helps because the first step is an attempt to find a good $s$ such that $Ws \approx x$, and the second step "normalizes" $s$ in an attempt to keep the sparsity penalty small. It turns out that initializing $s$ using only one but not both steps results in poor performance in practice. ([[TODO]]: a better explanation for why this initialization helps?) + + === The practical algorithm === + + With the above two tricks, the algorithm for sparse coding then becomes: +
+
1. Initialize $A$ randomly +
2. Repeat until convergence +
+
1. Select a random mini-batch of 2000 patches +
2. Initialize $s$ as described above +
3. Find the $s$ that minimizes $J(A, s)$ for the $A$ found in the previous step +
4. Solve for the $A$ that minimizes $J(A, s)$ for the $s$ found in the previous step +
+
+ + With this method, you should be able to reach a good local optima relatively quickly.