Backpropagation
In machine learning, backpropagation is a gradient computation method commonly used for training a neural network in computing parameter updates.
It is an efficient application of the chain rule to neural networks. Backpropagation computes the gradient of a loss function with respect to the weights of the network for a single input–output example, and does so efficiently, computing the gradient one layer at a time, iterating backward from the last layer to avoid redundant calculations of intermediate terms in the chain rule; this can be derived through dynamic programming.
Strictly speaking, the term backpropagation refers only to an algorithm for efficiently computing the gradient, not how the gradient is used; but the term is often used loosely to refer to the entire learning algorithm. This includes changing model parameters in the negative direction of the gradient, such as by stochastic gradient descent, or as an intermediate step in a more complicated optimizer, such as Adaptive Moment Estimation.
Backpropagation had multiple discoveries and partial discoveries, with a tangled [|history] and terminology. See the history section for details. Some other names for the technique include "reverse mode of automatic differentiation" or "reverse accumulation".
Overview
Backpropagation computes the gradient in weight space of a feedforward neural network, with respect to a loss function. Denote:- : input
- : target output
- :For classification, output will be a vector of class probabilities.
- : loss function or "cost function"
- :For classification, this is usually cross-entropy, while for regression it is usually squared error loss.
- : the number of layers
- : the weights between layer and, where is the weight between the -th node in layer and the -th node in layer
- : activation functions at layer
- :For classification the last layer is usually the logistic function for binary classification, and softmax for multi-class classification, while for the hidden layers this was traditionally a sigmoid function on each node, but today is more varied, with rectifier being common.
- : activation of the -th node in layer.
The overall network is a combination of function composition and matrix multiplication:
For a training set there will be a set of input–output pairs,. For each input–output pair in the training set, the loss of the model on that pair is the cost of the difference between the predicted output and the target output :
Note the distinction: during model evaluation the weights are fixed while the inputs vary, and the network ends with the output layer. During model training the input–output pair is fixed while the weights vary, and the network ends with the loss function.
Backpropagation computes the gradient for a fixed input–output pair, where the weights can vary. Each individual component of the gradient, can be computed by the chain rule; but doing this separately for each weight is inefficient. Backpropagation efficiently computes the gradient by avoiding duplicate calculations and not computing unnecessary intermediate values, by computing the gradient of each layer – specifically the gradient of the weighted input of each layer, denoted by – from back to front.
Informally, the key point is that since the only way a weight in affects the loss is through its effect on the next layer, and it does so linearly, are the only data you need to compute the gradients of the weights at layer, and then the gradients of weights of previous layer can be computed by and repeated recursively. This avoids inefficiency in two ways. First, it avoids duplication because when computing the gradient at layer, it is unnecessary to recompute all derivatives on later layers each time. Second, it avoids unnecessary intermediate calculations, because at each stage it directly computes the gradient of the weights with respect to the ultimate output, rather than unnecessarily computing the derivatives of the values of hidden layers with respect to changes in weights.
Backpropagation can be expressed for simple feedforward networks in terms of [|matrix multiplication], or more generally in terms of the [|adjoint graph].
Matrix multiplication
For the basic case of a feedforward network, where nodes in each layer are connected only to nodes in the immediate next layer, and there is a loss function that computes a scalar loss for the final output, backpropagation can be understood simply by matrix multiplication. Essentially, backpropagation evaluates the expression for the derivative of the cost function as a product of derivatives between each layer from right to left – "backwards" – with the gradient of the weights between each layer being a simple modification of the partial products.Given an input–output pair, the loss is:
To compute this, one starts with the input and works forward; denote the weighted input of each hidden layer as and the output of hidden layer as the activation. For backpropagation, the activation as well as the derivatives must be cached for use during the backwards pass.
The derivative of the loss in terms of the inputs is given by the chain rule; note that each term is a total derivative, evaluated at the value of the network on the input :
where is a diagonal matrix.
These terms are: the derivative of the loss function; the derivatives of the activation functions; and the matrices of weights:
The gradient is the transpose of the derivative of the output in terms of the input, so the matrices are transposed and the order of multiplication is reversed, but the entries are the same:
Backpropagation then consists essentially of evaluating this expression from right to left, computing the gradient at each layer on the way; there is an added step, because the gradient of the weights is not just a subexpression: there's an extra multiplication.
Introducing the auxiliary quantity for the partial products, interpreted as the "error at level " and defined as the gradient of the input values at level :
Note that is a vector, of length equal to the number of nodes in level ; each component is interpreted as the "cost attributable to that node".
The gradient of the weights in layer is then:
The factor of is because the weights between level and affect level proportionally to the inputs : the inputs are fixed, the weights vary.
The can easily be computed recursively, going from right to left, as:
The gradients of the weights can thus be computed using a few matrix multiplications for each level; this is backpropagation.
Compared with naively computing forwards :
There are two key differences with backpropagation:
- Computing in terms of avoids the obvious duplicate multiplication of layers and beyond.
- Multiplying starting from – propagating the error backwards – means that each step simply multiplies a vector by the matrices of weights and derivatives of activations. By contrast, multiplying forwards, starting from the changes at an earlier layer, means that each multiplication multiplies a matrix by a matrix. This is much more expensive, and corresponds to tracking every possible path of a change in one layer forward to changes in the layer , which unnecessarily computes the intermediate quantities of how weight changes affect the values of hidden nodes.
Adjoint graph
Intuition
Motivation
The goal of any supervised learning algorithm is to find a function that best maps a set of inputs to their correct output. The motivation for backpropagation is to train a multi-layered neural network such that it can learn the appropriate internal representations to allow it to learn any arbitrary mapping of input to output.Learning as an optimization problem
To understand the mathematical derivation of the backpropagation algorithm, it helps to first develop some intuition about the relationship between the actual output of a neuron and the correct output for a particular training example. Consider a simple neural network with two input units, one output unit and no hidden units, and in which each neuron uses a linear output that is the weighted sum of its input.Initially, before training, the weights will be set randomly. Then the neuron learns from training examples, which in this case consist of a set of tuples where and are the inputs to the network and is the correct output. The initial network, given and, will compute an output that likely differs from . A loss function is used for measuring the discrepancy between the target output and the computed output. For regression analysis problems the squared error can be used as a loss function, for classification the categorical cross-entropy can be used.
As an example consider a regression problem using the square error as a loss:
where is the discrepancy or error.
Consider the network on a single training case:. Thus, the input and are 1 and 1 respectively and the correct output, is 0. Now if the relation is plotted between the network's output on the horizontal axis and the error on the vertical axis, the result is a parabola. The minimum of the parabola corresponds to the output which minimizes the error. For a single training case, the minimum also touches the horizontal axis, which means the error will be zero and the network can produce an output that exactly matches the target output. Therefore, the problem of mapping inputs to outputs can be reduced to an optimization problem of finding a function that will produce the minimal error.
However, the output of a neuron depends on the weighted sum of all its inputs:
where and are the weights on the connection from the input units to the output unit. Therefore, the error also depends on the incoming weights to the neuron, which is ultimately what needs to be changed in the network to enable learning.
In this example, upon injecting the training data, the loss function becomes
Then, the loss function takes the form of a parabolic cylinder with its base directed along. Since all sets of weights that satisfy minimize the loss function, in this case additional constraints are required to converge to a unique solution. Additional constraints could either be generated by setting specific conditions to the weights, or by injecting additional training data.
One commonly used algorithm to find the set of weights that minimizes the error is gradient descent. By backpropagation, the steepest descent direction is calculated of the loss function versus the present synaptic weights. Then, the weights can be modified along the steepest descent direction, and the error is minimized in an efficient way.