Backpropagation, or backprop, is an [algorithm](algorithm.md), based on the chain rule of derivation, used in training [neural networks](neural_network.md); it computes the partial derivative (or [gradient](gradient.md)) of the function of the network's error so that we can perform a [gradient descent](gradient_descent.md), i.e. update the weights towards lowering the network's error. It computes the analytical derivative (theoretically you could estimate a derivative numerically, but that's not so accurate and can be too computationally expensive). It is called backpropagation because it works backwards and propagates the error from the output towards the input, due to how the chain rule works, and it's efficient by reusing already computed values.
It has an input layer (neurons *x0*, *x1*), a hidden layer (neurons *y0*, *y1*) and an output layer (neurons *z0*, *z1*). For simplicity there are no biases (biases can easily be added as input neurons that are always on). At the end there is a total error *E* computed from the networks's output against the desired output (training data).
Let's say the total error is computed as the squared error: *E = squared_error(z0) + squared_error(z1) = 1/2 * (z0 - z0_desired)^2 + 1/2 * (z1 - z1_desired)^2*.
We can see each non-input neuron as a function. E.g. the neuron *z0* is a function *z0(x) = z0(a(z0s(x)))* where:
- *z0s* is the sum of inputs to the neuron, in this case *z0s(x) = w100 * y0(x) + +110 * y1(x)*
- *a* is the activation function, let's suppose the normally used [logistic function](logistic_function.md) *a(x) = 1/(1 + e^x)*.
If you don't know what the fuck is going on see [neural networks](neural_network.md) first.
What is our goal now? To find the **[partial derivative](partial_derivative.md) of the whole network's total error function** (at the current point defined by the weights), or in other words the **gradient** at the current point. I.e. from the point of view of the total error (which is just a number output by this system), the network is a function of 8 variables (weights *w000*, *w001*, ...) and we want to find a derivative of this function in respect to each of these variables (that's what a partial derivative is) at the current point (i.e. with current values of the weights). This will, for each of these variables, tell us how much (at what rate and in which direction) the total error changes if we change that variable by certain amount. Why do we need to know this? So that we can do a [gradient descent](gradient_descent.md), i.e. this information is kind of a direction in which we want to move (change the weights and biases) towards lowering the total error (making the network compute results which are closer to the training data).
Backpropagation is based on the **chain rule**, a rule of derivation that equates the derivative of a function composition (functions inside other functions) to a product of derivatives. This is important because by converting the derivatives to a product we will be able to **reuse** the individual factors and so compute very efficiently and quickly.
Let's write derivative of *f(x)* with respect to *x* as *D{f(x),x}*. The chain rule says that:
*D{f(g(x)),x} = D{f(g(x)),g(x)} * D{g(x),x}*
Notice that this can be applied to any number of composed functions, the product chain just becomes longer.
Let's get to the computation. Backpropagation work by going "backwards" from the output towards the input. So, let's start by computing the derivative against the weight *w100*. It will be a specific number; let's call it *'w100*. Derivative of a sum is equal to the sum of derivatives:
Now let's compute the derivative in respect to *w000*, i.e. the number *'w000*. We will proceed similarly but the computation will be different because the weight *w000* affects both output neurons ('z0' and 'z1'). Again, we'll use the chain rule.
Once we have them, we multiply them all by some value (**learning rate**, a distance by which we move in the computed direction) and subtract them from the current weights by which we perform the gradient descent and lower the total error.
Note that here we've only used one training sample, i.e. the error *E* was computed from the network against a single desired output. If more example are used in a single update step, they are usually somehow averaged.