# Deriving gradients using the backpropagation idea

(Difference between revisions)
 Revision as of 06:04, 30 May 2011 (view source)Cyfoo (Talk | contribs)← Older edit Revision as of 06:44, 30 May 2011 (view source)Cyfoo (Talk | contribs) Newer edit → Line 38: Line 38: Let's say we have a function $F$ that takes a matrix $X$ and yields a real number. We would like to use the backpropagation idea to compute the gradient with respect to $X$ of $F$, that is $\nabla_X F$. The general idea is to see the function $F$ as a multi-layer neural network, and to derive the gradients using the backpropagation idea. Let's say we have a function $F$ that takes a matrix $X$ and yields a real number. We would like to use the backpropagation idea to compute the gradient with respect to $X$ of $F$, that is $\nabla_X F$. The general idea is to see the function $F$ as a multi-layer neural network, and to derive the gradients using the backpropagation idea. - To do this, we will set our "objective function" to be the function $J(z)$ that when applied to the outputs of the neurons in the last layer yields the value $F(x)$. For the intermediate layers, we will also choose our activation functions $f^{(l)}$ to this end. + To do this, we will set our "objective function" to be the function $J(z)$ that when applied to the outputs of the neurons in the last layer yields the value $F(X)$. For the intermediate layers, we will also choose our activation functions $f^{(l)}$ to this end. + + Using this method, we can easily compute derivatives with respect to the inputs $X$, as well as derivatives with respect to any of the weights in the network, as we shall see later. == Examples == == Examples == - We will use two functions from the section on [[Sparse Coding: Autoencoder Interpretation | sparse coding]] to illustrate the method of computing gradients of functions on matrices using the backpropagation idea. + To illustrate the use of the backpropagation idea to compute derivatives with respect to the inputs, we will use two functions from the section on [[Sparse Coding: Autoencoder Interpretation | sparse coding]], in examples 1 and 2. In example 3, we use a function from [[Independent Component Analysis | independent component analysis]] to illustrate the use of this idea to compute derivates with respect to weights, and in this specific case, what to do in the case of tied or repeated weights. === Example 1: Objective for weight matrix in sparse coding === === Example 1: Objective for weight matrix in sparse coding === - Recall the objective function for the weight matrix $A$, given the feature matrix $s$: + Recall for [[Sparse Coding: Autoencoder Interpretation | sparse coding]], the objective function for the weight matrix $A$, given the feature matrix $s$: :$F(A; s) = \lVert As - x \rVert_2^2 + \gamma \lVert A \rVert_2^2$ :$F(A; s) = \lVert As - x \rVert_2^2 + \gamma \lVert A \rVert_2^2$ Line 116: Line 118: === Example 2: Smoothed topographic L1 sparsity penalty in sparse coding  === === Example 2: Smoothed topographic L1 sparsity penalty in sparse coding  === - Recall the smoothed topographic L1 sparsity penalty on $s$ in sparse coding: + Recall the smoothed topographic L1 sparsity penalty on $s$ in [[Sparse Coding: Autoencoder Interpretation | sparse coding]]: :$\sum{ \sqrt{Vss^T + \epsilon} }$ :$\sum{ \sqrt{Vss^T + \epsilon} }$ where $V$ is the grouping matrix, $s$ is the feature matrix and $\epsilon$ is a constant. where $V$ is the grouping matrix, $s$ is the feature matrix and $\epsilon$ is a constant. Line 163: Line 165:
3 3 $f'(z_i) = 1$ $f'(z_i) = 1$$\left( I^T \delta^{(3)} \right) \bullet 1$$\left( I^T \delta^{(4)} \right) \bullet 1$ $Vss^T$ $Vss^T$
LayerWeightActivation function $f$
1$W$$f(z_i) = z_i$
2$W^T$$f(z_i) = z_i$
3$I$$f(z_i) = z_i - x_i$
4N/A$f(z_i) = z_i^2$
- + Line 186: Line 188: & = V^T \frac{1}{2}(Vss^T + \epsilon)^{-\frac{1}{2}} \bullet 2s \\ & = V^T \frac{1}{2}(Vss^T + \epsilon)^{-\frac{1}{2}} \bullet 2s \\ & = V^T (Vss^T + \epsilon)^{-\frac{1}{2}} \bullet s & = V^T (Vss^T + \epsilon)^{-\frac{1}{2}} \bullet s + \end{align} + [/itex] + + === Example 3: ICA reconstruction cost === + + Recall the [[Independent Component Analysis | independent component analysis (ICA)]] reconstruction cost term: + $\lVert W^TWx - x \rVert_2^2$ + where $W$ is the weight matrix and $x$ is the input. + + We would like to find $\nabla_W \lVert W^TWx - x \rVert_2^2$ - the derivative of the term with respect to the '''weight matrix''', rather than the '''input''' as in the earlier two examples. We will still proceed similarly though, seeing this term as an instantiation of a neural network: + + [[File:Backpropagation Method Example 3.png | 400px]] + + The weights and activation functions of this network are as follows: + + + + + + + + + + + + + + + + + + + + + + + + To have $J(z^{(4)}) = F(x)$, we can set $J(z^{(4)}) = \sum_k J(z^{(4)}_k)$. + + Now that we can see $F$ as a neural network, we can try to compute the gradient $\nabla_W F$. However, we now face the difficulty that $W$ appears twice in the network. Fortunately, it turns out that if $W$ appears multiple times in the network, the gradient with respect to $W$ is simply the sum of gradients for each $W$ in the network (you may wish to work out a formal proof of this fact to convince yourself). With this in mind, we can proceed to work out the deltas first: + + +
LayerDerivative of activation function $f'$ + DeltaInput $z$ to this layer
4$f'(z_i) = 2z_i$$f'(z_i) = 2z_i$$(W^TWx - x)$
3$f'(z_i) = 1$$\left( I^T \delta^{(4)} \right) \bullet 1$$W^TWx$
2$f'(z_i) = 1$$\left( (W^T)^T \delta^{(3)} \right) \bullet 1$$Wx$
1$f'(z_i) = 1$$\left( W^T \delta^{(2)} \right) \bullet 1$$x$
+ + + + + + + + + + + + + + + + + + + + + + + + + + + First we find the gradients with respect to each $W$. + + With respect to $W^T$: + :+ \begin{align} + \nabla_{W^T} F & = \delta^{(3)} a^{(2)T} \\ + & = 2(W^TWx - x) (Wx)^T + \end{align} + + + With respect to $W$: + :+ \begin{align} + \nabla_{W} F & = \delta^{(2)} a^{(1)T} \\ + & = (W^T)(2(W^TWx -x)) x^T + \end{align} + + + Taking sums, noting that we need to transpose the gradient with respect to $W^T$ to get the gradient with respect to $W$, yields the final gradient with respect to $W$ (pardon the slight abuse of notation here): + + :+ \begin{align} + \nabla_{W} F & = \nabla_{W} F + (\nabla_{W^T} F)^T \\ + & = (W^T)(2(W^TWx -x)) x^T + 2(Wx)(W^TWx - x)^T \end{align} \end{align} [/itex]

## Introduction

In the section on the backpropagation algorithm, you were briefly introduced to backpropagation as a means of deriving gradients for learning in the sparse autoencoder. It turns out that together with matrix calculus, this provides a powerful method and intuition for deriving gradients for more complex matrix functions (functions from matrices to the reals, or symbolically, from $\mathbb{R}^{r \times c} \rightarrow \mathbb{R}$).

First, recall the backpropagation idea, which we present in a modified form appropriate for our purposes below:

1. For each output unit i in layer nl (the final layer), set
$\delta^{(n_l)}_i = \frac{\partial}{\partial z^{(n_l)}_i} \;\; J(z^{(n_l)})$
where J(z) is our "objective function" (explained below).
2. For $l = n_l-1, n_l-2, n_l-3, \ldots, 2$
For each node i in layer l, set
$\delta^{(l)}_i = \left( \sum_{j=1}^{s_{l+1}} W^{(l)}_{ji} \delta^{(l+1)}_j \right) \bullet \frac{\partial}{\partial z^{(l)}_i} f^{(l)} (z^{(l)}_i)$
3. Compute the desired partial derivatives,
\begin{align} \nabla_{W^{(l)}} J(W,b;x,y) &= \delta^{(l+1)} (a^{(l)})^T, \\ \end{align}

Quick notation recap:

• l is the number of layers in the neural network
• nl is the number of neurons in the lth layer
• $W^{(l)}_{ji}$ is the weight from the ith unit in the lth layer to the jth unit in the (l + 1)th layer
• $z^{(l)}_i$ is the input to the ith unit in the lth layer
• $a^{(l)}_i$ is the activation of the ith unit in the lth layer
• $A \bullet B$ is the Hadamard or element-wise product, which for $r \times c$ matrices A and B yields the $r \times c$ matrix $C = A \bullet B$ such that $C_{r, c} = A_{r, c} \cdot B_{r, c}$
• f(l) is the activation function for units in the lth layer

Let's say we have a function F that takes a matrix X and yields a real number. We would like to use the backpropagation idea to compute the gradient with respect to X of F, that is $\nabla_X F$. The general idea is to see the function F as a multi-layer neural network, and to derive the gradients using the backpropagation idea.

To do this, we will set our "objective function" to be the function J(z) that when applied to the outputs of the neurons in the last layer yields the value F(X). For the intermediate layers, we will also choose our activation functions f(l) to this end.

Using this method, we can easily compute derivatives with respect to the inputs X, as well as derivatives with respect to any of the weights in the network, as we shall see later.

## Examples

To illustrate the use of the backpropagation idea to compute derivatives with respect to the inputs, we will use two functions from the section on sparse coding, in examples 1 and 2. In example 3, we use a function from independent component analysis to illustrate the use of this idea to compute derivates with respect to weights, and in this specific case, what to do in the case of tied or repeated weights.

### Example 1: Objective for weight matrix in sparse coding

Recall for sparse coding, the objective function for the weight matrix A, given the feature matrix s:

$F(A; s) = \lVert As - x \rVert_2^2 + \gamma \lVert A \rVert_2^2$

We would like to find the gradient of F with respect to A, or in symbols, $\nabla_A F(A)$. Since the objective function is a sum of two terms in A, the gradient is the sum of gradients of each of the individual terms. The gradient of the second term is trivial, so we will consider the gradient of the first term instead.

The first term, $\lVert As - x \rVert_2^2$, can be seen as an instantiation of neural network taking s as an input, and proceeding in four steps, as described and illustrated in the paragraph and diagram below:

1. Apply A as the weights from the first layer to the second layer.
2. Subtract x from the activation of the second layer, which uses the identity activation function.
3. Pass this unchanged to the third layer, via identity weights. Use the square function as the activation function for the third layer.
4. Sum all the activations of the third layer.

The weights and activation functions of this network are as follows:

LayerWeightActivation function f
1 A f(zi) = zi (identity)
2 I (identity) f(zi) = zixi
3 N/A $f(z_i) = z_i^2$

To have J(z(3)) = F(x), we can set $J(z^{(3)}) = \sum_k J(z^{(3)}_k)$.

Once we see F as a neural network, the gradient $\nabla_X F$ becomes easy to compute - applying backpropagation yields:

LayerDerivative of activation function f'DeltaInput z to this layer
3 f'(zi) = 2zi f'(zi) = 2zi Asx
2 f'(zi) = 1 $\left( I^T \delta^{(3)} \right) \bullet 1$ As
1 f'(zi) = 1 $\left( A^T \delta^{(2)} \right) \bullet 1$ s

Hence,

\begin{align} \nabla_X F & = A^T I^T 2(As - x) \\ & = A^T 2(As - x) \end{align}

### Example 2: Smoothed topographic L1 sparsity penalty in sparse coding

Recall the smoothed topographic L1 sparsity penalty on s in sparse coding:

$\sum{ \sqrt{Vss^T + \epsilon} }$

where V is the grouping matrix, s is the feature matrix and ε is a constant.

We would like to find $\nabla_s \sum{ \sqrt{Vss^T + \epsilon} }$. As above, let's see this term as an instantiation of a neural network:

The weights and activation functions of this network are as follows:

LayerWeightActivation function f
1 I $f(z_i) = z_i^2$
2 V f(zi) = zi
3 I f(zi) = zi + ε
4 N/A $f(z_i) = z_i^{\frac{1}{2}}$

To have J(z(4)) = F(x), we can set $J(z^{(4)}) = \sum_k J(z^{(4)}_k)$.

Once we see F as a neural network, the gradient $\nabla_X F$ becomes easy to compute - applying backpropagation yields:

LayerDerivative of activation function f' DeltaInput z to this layer
4 $f'(z_i) = \frac{1}{2} z_i^{-\frac{1}{2}}$ $f'(z_i) = \frac{1}{2} z_i^{-\frac{1}{2}}$ (VssT + ε)
3 f'(zi) = 1 $\left( I^T \delta^{(4)} \right) \bullet 1$ VssT
2 f'(zi) = 1 $\left( V^T \delta^{(3)} \right) \bullet 1$ ssT
1 f'(zi) = 2zi $\left( I^T \delta^{(2)} \right) \bullet 2s$ s

Hence,

\begin{align} \nabla_X F & = I^T V^T I^T \frac{1}{2}(Vss^T + \epsilon)^{-\frac{1}{2}} \bullet 2s \\ & = V^T \frac{1}{2}(Vss^T + \epsilon)^{-\frac{1}{2}} \bullet 2s \\ & = V^T (Vss^T + \epsilon)^{-\frac{1}{2}} \bullet s \end{align}

### Example 3: ICA reconstruction cost

Recall the independent component analysis (ICA) reconstruction cost term: $\lVert W^TWx - x \rVert_2^2$ where W is the weight matrix and x is the input.

We would like to find $\nabla_W \lVert W^TWx - x \rVert_2^2$ - the derivative of the term with respect to the weight matrix, rather than the input as in the earlier two examples. We will still proceed similarly though, seeing this term as an instantiation of a neural network:

The weights and activation functions of this network are as follows:

LayerWeightActivation function f
1 W f(zi) = zi
2 WT f(zi) = zi
3 I f(zi) = zixi
4 N/A $f(z_i) = z_i^2$

To have J(z(4)) = F(x), we can set $J(z^{(4)}) = \sum_k J(z^{(4)}_k)$.

Now that we can see F as a neural network, we can try to compute the gradient $\nabla_W F$. However, we now face the difficulty that W appears twice in the network. Fortunately, it turns out that if W appears multiple times in the network, the gradient with respect to W is simply the sum of gradients for each W in the network (you may wish to work out a formal proof of this fact to convince yourself). With this in mind, we can proceed to work out the deltas first:

LayerDerivative of activation function f' DeltaInput z to this layer
4 f'(zi) = 2zi f'(zi) = 2zi (WTWxx)
3 f'(zi) = 1 $\left( I^T \delta^{(4)} \right) \bullet 1$ WTWx
2 f'(zi) = 1 $\left( (W^T)^T \delta^{(3)} \right) \bullet 1$ Wx
1 f'(zi) = 1 $\left( W^T \delta^{(2)} \right) \bullet 1$ x

First we find the gradients with respect to each W.

With respect to WT:

\begin{align} \nabla_{W^T} F & = \delta^{(3)} a^{(2)T} \\ & = 2(W^TWx - x) (Wx)^T \end{align}

With respect to W:

\begin{align} \nabla_{W} F & = \delta^{(2)} a^{(1)T} \\ & = (W^T)(2(W^TWx -x)) x^T \end{align}

Taking sums, noting that we need to transpose the gradient with respect to WT to get the gradient with respect to W, yields the final gradient with respect to W (pardon the slight abuse of notation here):

\begin{align} \nabla_{W} F & = \nabla_{W} F + (\nabla_{W^T} F)^T \\ & = (W^T)(2(W^TWx -x)) x^T + 2(Wx)(W^TWx - x)^T \end{align}