- About - Contract - Library - - Books - - - AJGP - - Code - - Courses - - Links - - Media - - Tutorials - People - - David - - - Résumé - - Shannon - Portfolio - Update

2005-02-18

## Introduction

This is a derivation of a popular neural network training algorithm known as backpropagation. It is commonly used with feed-forward multilayer perceptrons.

To see backpropagation in action, check out the applet BackpropXOR in the CroftSoft Collection.

## Definitions and Identities

1. Sigmoid Function

$\sigma \left(x\right)=\frac{1}{\left(1+{e}^{\mathrm{-x}}\right)}$

2. Derivative of the Sigmoid Function

$\sigma \text{'}\left(x\right)=\sigma \left(x\right)*\left[1-\sigma \left(x\right)\right]$

## Simplest Case

This is a derivation of the backpropagation neural network training algorithm for the simplest case: a two-layer network with one neuron in each layer. In this derivation, all of the variables are scalars. Deriving the learning rule for the simplest case makes backpropagation easier to understand when vectors and matrices are introduced later.

s → v → b → σ → h → w → a → σ → r

A signal (s) is the input to the first layer hidden neuron. It is multiplied by the first layer weight (v). The weighted input (b) is then squashed by the nonlinear neuron to fall between zero and one using the sigmoidal function (σ). The output of the hidden neuron (h) is then multiplied by the weight (w). The weighted input from the hidden neuron (a) is fed to the squashing function of the second layer output neuron. The difference between the desired response (d) and the actual response (r) of the output neuron is the error (e). The objective function (l) is the scaled square of the error (e).

1. Objective Function

$l=½*{e}^{2}$

2. Error Function

$e=d-r$

3. Response from Output Neuron

$r=\sigma \left(a\right)$

4. Weighted Input from Hidden Neuron to Output Neuron

$a=w*h$

5. Hidden Neuron Output

$h=\sigma \left(b\right)$

6. Weighted Input to Hidden Neuron

$b=v*s$

## Output Layer

We seek to minimize the objective function (l) by modifying the weight (w) to the output layer. This is done by taking the partial derivative of the objective function with respect to the weights. This derivative is then used to modify the weight using a gradient descent algorithm.

1. Gradient for Output Layer Weight, Chain Rule

$\frac{\partial l\left(w\right)}{\partial w}=\frac{\partial l\left(e\right)}{\partial e}*\frac{\partial e\left(r\right)}{\partial r}*\frac{\partial r\left(a\right)}{\partial a}*\frac{\partial a\left(w\right)}{\partial w}$

2. Output Layer Weight Gradient, First Term

$\frac{\partial l\left(e\right)}{\partial e}=\frac{\partial \left(½*{e}^{2}\right)}{\partial e}=e=d-r$

3. Output Layer Weight Gradient, Second Term

$\frac{\partial e\left(r\right)}{\partial r}=\frac{\partial \left(d-r\right)}{\partial r}=-1$

4. Output Layer Weight Gradient, Third Term

$\frac{\partial r\left(a\right)}{\partial a}=\frac{\partial \sigma \left(a\right)}{\partial a}=\sigma \left(a\right)*\left(1-\sigma \left(a\right)\right)=r*\left(1-r\right)$

5. Output Layer Weight Gradient, Fourth Term

$\frac{\partial a\left(w\right)}{\partial w}=\frac{\partial \left(w*h\right)}{\partial w}=h$

6. Gradient for Output Layer Weight, Combined Terms

$\frac{\partial l\left(w\right)}{\partial w}=\left[d-r\right]*\left[-1\right]*\left[r*\left(1-r\right)\right]*\left[h\right]$

## Hidden Layer

We also seek to minimize the objective function (l) by modifying the weight (v) to the hidden layer. The derivation of the first three terms in the gradient for the hidden layer weight are skipped as they are the same as the first three terms in the gradient for the output layer derived previously.

1. Gradient for Hidden Layer Weight, Chain Rule

$\frac{\partial l\left(v\right)}{\partial v}=\frac{\partial l\left(e\right)}{\partial e}*\frac{\partial e\left(r\right)}{\partial r}*\frac{\partial r\left(a\right)}{\partial a}*\frac{\partial a\left(h\right)}{\partial h}*\frac{\partial h\left(b\right)}{\partial b}*\frac{\partial b\left(v\right)}{\partial v}$

2. Hidden Layer Weight Gradient, Fourth Term

$\frac{\partial a\left(h\right)}{\partial h}=\frac{\partial \left(w*h\right)}{\partial h}=w$

3. Hidden Layer Weight Gradient, Fifth Term

$\frac{\partial h\left(b\right)}{\partial b}=\frac{\partial \sigma \left(b\right)}{\partial b}=\sigma \left(b\right)*\left(1-\sigma \left(b\right)\right)=h*\left(1-h\right)$

4. Hidden Layer Weight Gradient, Sixth Term

$\frac{\partial b\left(v\right)}{\partial v}=\frac{\partial \left(v*s\right)}{\partial v}=s$

5. Gradient for Hidden Layer Weight, Combined Terms

$\frac{\partial l\left(v\right)}{\partial v}=\left[d-r\right]*\left[-1\right]*\left[r*\left(1-r\right)\right]*\left[w\right]*\left[h*\left(1-h\right)\right]*\left[s\right]$

We noted that the first three terms of the gradients for the output layer and the hidden layer are the same. The negative product of these three terms is the local gradient. The local gradient can be computed for the output layer and then reused for the hidden layer.

1. Local Gradient for Output Neuron

${\delta }_{o}=-\frac{\partial l\left(a\right)}{\partial a}=-\frac{\partial l\left(e\right)}{\partial e}*\frac{\partial e\left(r\right)}{\partial r}*\frac{\partial r\left(a\right)}{\partial a}=e*r*\left(1-r\right)$

$\frac{\partial l\left(w\right)}{\partial w}={-\delta }_{o}*h$

$\frac{\partial l\left(v\right)}{\partial v}={-\delta }_{o}*w*h*\left(1-h\right)*s$

## Backpropagation

The local gradient shows how the objective function decreases as the weighted input to the neuron increases. The local gradient for an output neuron depends on the error (e). The local gradients for hidden layer neurons, however, depend on the local gradients of the following layer. Thus the computation of the local gradients must be propagated from the back layer of the network to the front. The weight gradients can then be expressed as functions of the local gradients and the unweighted inputs.

1. Local Gradient for Hidden Neuron

${\delta }_{h}=-\frac{\partial l\left(b\right)}{\partial b}=-\frac{\partial l\left(e\right)}{\partial e}*\frac{\partial e\left(r\right)}{\partial r}*\frac{\partial r\left(a\right)}{\partial a}*\frac{\partial a\left(h\right)}{\partial h}*\frac{\partial h\left(b\right)}{\partial b}$

${\delta }_{h}={\delta }_{o}*\frac{\partial a\left(h\right)}{\partial h}*\frac{\partial h\left(b\right)}{\partial b}$

3. The Pattern for Hidden Layers

${\delta }_{h}={\delta }_{o}*w*h*\left(1-h\right)$

4. Hidden Layer Gradient Abbreviated Further

$\frac{\partial l\left(v\right)}{\partial v}={-\delta }_{h}*s$

Now that we have the gradient of the objective function with respect to the weights in terms of the of local gradients, we can incrementally decrease the objective function, and therefore the overall error, by shifting the weights.

1. Weight Update Rule

$w\left(t+1\right)=w\left(t\right)+\Delta w$

$\Delta w=-\eta *\frac{\partial l\left(w\right)}{\partial w}$

3. A Negative Becomes a Positive

$\Delta w=\eta *{\delta }_{o}*h$

4. The Weight Update Rule Revised

$w\left(t+1\right)=w\left(t\right)+\eta *{\delta }_{o}*h$

5. Similar Pattern for Hidden Weights

$v\left(t+1\right)=v\left(t\right)+\eta *{\delta }_{h}*s$