# Deriving gradients using the backpropagation idea

### From Ufldl

## 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 ).

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
   where J(z) is our "objective function" (explained below).
2. For
     For each node i in layer l, set
3. Compute the desired partial derivatives,

Quick notation recap:

- l is the number of layers in the neural network
- nl is the number of neurons in the lth layer
- is the weight from the ith unit in the lth layer to the jth unit in the (l + 1)th layer
- is the input to the ith unit in the lth layer
- is the activation of the ith unit in the lth layer
- is the Hadamard or element-wise product, which for matrices A and B yields the matrix such that
- 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 . 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:

We would like to find the gradient of F with respect to A, or in symbols, . 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, , 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:

Layer | Weight | Activation function f
--- | --- | ---
1 | A | f(zi) = zi (identity)
2 | I (identity) | f(zi) = zi − xi
3 | N/A |

To have J(z(3)) = F(x), we can set .

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

Layer | Derivative of activation function f' | Delta | Input z to this layer
--- | --- | --- | ---
3 | f'(zi) = 2zi | f'(zi) = 2zi | A s − x
2 | f'(zi) = 1 |  | A s
1 | f'(zi) = 1 |  | s

Hence,

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

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

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

We would like to find . 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:

Layer | Weight | Activation function f
--- | --- | ---
1 | I |
2 | V | f(zi) = zi
3 | I | f(zi) = zi + ε
4 | N/A |

To have J(z(4)) = F(x), we can set .

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

Layer | Derivative of activation function f' | Delta | Input z to this layer
--- | --- | --- | ---
4 |  |  | (V s sT + ε)
3 | f'(zi) = 1 |  | V s sT
2 | f'(zi) = 1 |  | s sT
1 | f'(zi) = 2zi |  | s

Hence,

### Example 3: ICA reconstruction cost

Recall the independent component analysis (ICA) reconstruction cost term: where W is the weight matrix and x is the input.

We would like to find - 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:

Layer | Weight | Activation function f
--- | --- | ---
1 | W | f(zi) = zi
2 | WT | f(zi) = zi
3 | I | f(zi) = zi − xi
4 | N/A |

To have J(z(4)) = F(x), we can set .

Now that we can see F as a neural network, we can try to compute the gradient . 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 instance of W in the network (you may wish to work out a formal proof of this fact to convince yourself). With this in mind, we will proceed to work out the deltas first:

Layer | Derivative of activation function f' | Delta | Input z to this layer
--- | --- | --- | ---
4 | f'(zi) = 2zi | f'(zi) = 2zi | (WTW x − x)
3 | f'(zi) = 1 |  | WTW x
2 | f'(zi) = 1 |  | W x
1 | f'(zi) = 1 |  | x

To find the gradients with respect to W, first we find the gradients with respect to each instance of W in the network.

With respect to WT:

With respect to W:

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):

Language : 中文

Retrieved from "http://deeplearning.stanford.edu/wiki/index.php/Deriving_gradients_using_the_backpropagation_idea"

##### Views

- Page
- Discussion
- View source
- History

##### Personal tools

- Log in

##### Search

##### Toolbox

- What links here
- Related changes
- Special pages
- Printable version
- Permanent link