# Deriving gradients using the backpropagation idea

### From Ufldl

## Contents |

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

- For
- For each node
*i*in layer*l*, set

- For each node
- Compute the desired partial derivatives,

Quick notation recap:

*l*is the number of layers in the neural network*n*_{l}is the number of neurons in the*l*th layer- is the weight from the
*i*th unit in the*l*th layer to the*j*th unit in the (*l*+ 1)th layer - is the input to the
*i*th unit in the*l*th layer - is the activation of the
*i*th unit in the*l*th 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*l*th layer

Notice that we don't consider an objective function in this case, and we allow each layer to have a different activation function *f*^{(l)}. This will be useful in allowing us to compute the gradients of functions of matrices.

## The method

To compute the gradient with respect to some matrix *X* of a complicated function of matrices, it may be helpful to consider the function as a complicated multi-layer neural network, if possible. We will use two functions from the section on sparse coding to illustrate this.

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

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

We would like to find the gradient of *J* 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:

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

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

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

We would like to find . As above, let's see this term as an instantiation of a neural network: