Backpropagation
In machine learning, backpropagation (backprop, BP) is a widely used algorithm for training feedforward neural networks. Generalizations of backpropagation exist for other artificial neural networks (ANNs), and for functions generally. These classes of algorithms are all referred to generically as "backpropagation". In fitting a neural network, backpropagation computes the gradient of the loss function with respect to the weights of the network for a single input–output example, and does so efficiently, unlike a naive direct computation of the gradient with respect to each weight individually. This efficiency makes it feasible to use gradient methods for training multilayer networks , updating weights to minimize loss; gradient descent, or variants such as stochastic gradient descent, are commonly used. The backpropagation algorithm works by computing the gradient of the loss function with respect to each weight by the chain rule, 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 is an example of dynamic programming. The term "backpropagation" strictly refers only to the algorithm for computing the gradient, not how the gradient is used; however, the term is often used loosely to refer to the  entire learning algorithm, including how the gradient is used, such as by stochastic gradient descent. Backpropagation generalizes the gradient computation in the delta rule, which is the single-layer version of backpropagation, and is in turn generalized by automatic differentiation, where backpropagation is a special case of reverse accumulation (or "reverse mode"). The term "backpropagation" and its general use in neural networks was announced in , then elaborated and popularized in , but the technique was independently rediscovered many times, and had many predecessors dating to the 1960s; see . A modern overview is given in the deep learning textbook by . Overview. Backpropagation  computes the gradient in weight space of a feedforward neural network, with respect to a loss function. Denote: In the derivation of backpropagation, other intermediate quantities are used; they are introduced as needed below. Bias terms are not treated specially, as they correspond to a weight with a fixed input of 1. For the purpose of backpropagation, the specific loss function and activation functions do not matter, as long as they and their derivatives can be evaluated efficiently. Traditional activation functions include but are not limited to sigmoid, tanh, and ReLU. Since, swish, mish, and other activation functions were proposed  as well. 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, formula_18. For each input–output pair formula_19 in the training set, the loss of the model on that pair is the cost of the difference between the predicted output formula_20 and the target output formula_21: Note the distinction: during model evaluation, the weights are fixed, while the inputs vary (and the target output may be unknown), and the network ends with the output layer (it does not include the loss function). 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 formula_19, where the weights formula_10 can vary. Each individual component of the gradient, formula_25 can be computed by the chain rule; however, 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 formula_26 – from back to front. Informally, the  key point is that since the only way a weight in formula_27 affects the loss is through its effect on the "next" layer, and it does so "linearly", formula_26 are the only data you need to compute the gradients of the weights at layer formula_9, and then you can compute the previous layer formula_30 and repeat recursively. This avoids inefficiency in two ways. Firstly, it avoids duplication because when computing the gradient at layer formula_9, you do not need to recompute all the derivatives on later layers formula_32 each time. Secondly, it avoids unnecessary intermediate calculations because at each stage  it directly computes the gradient of the weights with respect to the ultimate output (the loss), rather than unnecessarily computing the derivatives of the values of hidden layers with respect to changes in weights formula_33. 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 (without skipping any layers), 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 left to right" – "backwards" – with the gradient of the weights between each layer being a simple modification of the partial products (the "backwards propagated error"). Given an input–output pair formula_34, the loss is: To compute this, one starts with the input formula_1 and works forward; denote the weighted input of each layer as formula_37 and the output of layer formula_9 as the activation formula_39. For backpropagation, the activation formula_39  as well as the derivatives formula_41 (evaluated at formula_37) 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 (at each node) on the input formula_1: where formula_45 is a Hadamard product, that is an element-wise product, and the products are taken from left to right. These terms are: the derivative of the loss function; the derivatives of the activation functions; and the matrices of weights: The gradient formula_47  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 (equivalently, multiplying the previous expression for the derivative from left to right), computing the gradient at each layer on the way; there is an added step, because the gradient of the weights isn't just a subexpression: there's an extra multiplication. Introducing the auxiliary quantity formula_26 for the partial products (multiplying from right to left ), interpreted as the "error at level formula_9" and defined as the gradient of the input values at level formula_9: Note that formula_26 is a vector, of length equal to the number of nodes in level formula_9; each component is interpreted as the "cost attributable to (the value of) that node". The gradient of the weights in layer formula_9 is then: The factor of formula_57 is because the weights formula_27 between level formula_8 and formula_9 affect level formula_9 proportionally to the inputs (activations): the inputs are fixed, the weights vary. The formula_26 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 (using the formula_26 for illustration): there are two key differences with backpropagation: Adjoint graph. For more general graphs, and other advanced variations, backpropagation can be understood in terms of automatic differentiation, where backpropagation is a special case of reverse accumulation (or "reverse mode"). 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 (unlike most work on neural networks, in  which mapping from inputs to outputs is non-linear) 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 formula_77 where formula_78 and formula_79 are the inputs to the network and is the correct output (the output the network should produce given those inputs, when it has been trained). The initial network, given formula_78 and formula_79, will compute an output that likely differs from (given random weights). A loss function formula_82 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 crossentropy 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: formula_84. Thus, the input formula_78 and formula_79 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 formula_88 and formula_89 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 formula_90 Then, the loss function formula_91 takes the form of a parabolic cylinder with its base directed along formula_92. Since all sets of weights that satisfy formula_92 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. Derivation. The gradient descent method involves calculating the derivative of the loss function with respect to the weights of the network. This is normally done using backpropagation. Assuming one output neuron, the squared error function is where For  each neuron formula_13, its output formula_101 is defined as where the activation function formula_103 is non-linear and differentiable (even if the ReLU is not in one point). A historically used activation function is the logistic function: which has a convenient derivative of: The input formula_106 to a neuron is the weighted sum of outputs formula_107 of previous neurons. If the neuron is in the first layer after the input layer, the formula_107 of the input layer are simply the inputs formula_109 to the network. The number of input units to the neuron is formula_110. The variable formula_111 denotes the  weight between neuron formula_11 of the previous layer and neuron formula_13 of the current layer. Finding the derivative of the error. Calculating the partial derivative of the error with respect to a weight formula_114 is done using the chain rule twice: In the last factor of the right-hand side of the above, only one term in the sum formula_106 depends on formula_114, so that If the neuron is in the first layer after the input layer, formula_117 is just formula_118. The derivative of the output of neuron formula_13 with respect to its input is simply the partial derivative of  the activation function: which for the logistic activation function case is: This is the reason why backpropagation requires the activation function to be differentiable. (Nevertheless, the ReLU activation function, which is non-differentiable at 0, has become quite popular, e.g. in AlexNet) The first factor is straightforward to evaluate if the neuron is in the output layer, because then formula_121 and If half of the square error is used as loss function we can rewrite it as However, if formula_13 is in an arbitrary inner layer of the network, finding the derivative formula_91 with respect to formula_101 is less obvious . Considering formula_91 as a function with the inputs being all neurons formula_127 receiving input from neuron formula_13, and taking the total derivative with respect to formula_101, a recursive expression for the derivative is obtained: Therefore, the derivative with respect to formula_101 can be calculated if all the derivatives with respect to the outputs formula_132 of the next layer – the ones closer to the output neuron – are known. [Note, if any of the neurons in set formula_6 were not connected to neuron formula_13, they would be independent of formula_114 and the corresponding partial derivative under the summation would vanish to 0 .] Substituting , and in we obtain: with if formula_103 is the logistic function, and the error is the square error: To update the weight formula_114 using gradient descent, one must choose a learning rate, formula_142. The change in weight needs to reflect the impact on formula_91 of an increase or decrease in formula_114. If formula_145, an increase in formula_114 increases formula_91; conversely, if formula_148, an increase in formula_114 decreases formula_91. The new formula_151 is added to the old weight, and the product of the learning rate and the gradient, multiplied by formula_152 guarantees that formula_114 changes in a way that always  decreases formula_91. In other words, in the equation immediately below, formula_155 always changes formula_114 in such a way that formula_91 is decreased: Second-order gradient descent. Using a Hessian matrix of second-order derivatives of the error function, the Levenberg-Marquardt algorithm often converges faster than first-order gradient descent, especially when the topology of the error function is complicated. It may also find solutions in smaller node counts for which other methods might not converge. The Hessian can be approximated by the Fisher information matrix. Loss function. The loss function is a function that maps values of one or  more variables onto a real number intuitively representing some "cost" associated with those values. For backpropagation, the loss function calculates the difference between the network output and its expected output, after a training example has propagated through the network. Assumptions. The mathematical expression of the loss function must fulfill two conditions in order for it to be possibly used in backpropagation. The first is that it can be written as an average formula_159 over error functions formula_160, for formula_161 individual training examples, formula_162. The reason for this assumption is that the backpropagation algorithm calculates the gradient of the error function  for a single training example, which needs to be generalized to the overall error function. The second assumption is that it can be written as a function of the outputs from the neural network. Example loss function. Let formula_163 be vectors in formula_164. Select an error function formula_165 measuring the difference between two outputs. The standard choice is the square of the Euclidean distance between the vectors formula_2 and formula_167:formula_168The error function over formula_161 training examples can then be written as an average of losses over individual examples:formula_170 History. The term "backpropagation" and its general use in neural  networks was announced in , then elaborated and popularized in , but the technique was independently rediscovered many times, and had many predecessors dating to the 1960s. The basics of continuous backpropagation were derived in the context of control theory by Henry J. Kelley in 1960, and by Arthur E. Bryson in 1961. They used principles of dynamic programming. In 1962, Stuart Dreyfus published a simpler derivation based only on the chain rule. Bryson and Ho described it as a multi-stage dynamic system optimization method in 1969. Backpropagation was derived by multiple researchers in the early 60's and implemented to  run on computers as early as 1970 by Seppo Linnainmaa. Paul Werbos was first in the US to propose that it could be used for neural nets after analyzing it in depth in his 1974 dissertation. While not applied to neural networks, in 1970 Linnainmaa published the general method for automatic differentiation (AD). Although very controversial, some scientists believe this was actually the first step toward developing a back-propagation algorithm. In 1973 Dreyfus adapts parameters of controllers in proportion to error gradients. In 1974 Werbos mentioned the possibility of applying this principle to artificial neural networks, and in 1982  he applied Linnainmaa's AD method to non-linear functions. Later the Werbos method was rediscovered and described in 1985 by Parker, and in 1986 by Rumelhart, Hinton and Williams. Rumelhart, Hinton and Williams showed experimentally that this method can generate useful internal representations of incoming data in hidden layers of neural networks. Yann LeCun proposed the modern form of the back-propagation learning algorithm for neural networks in his PhD thesis in 1987. In 1993, Eric Wan won an international pattern recognition contest through backpropagation. During the 2000s it fell out of favour, but returned in the 2010s, benefitting  from cheap, powerful GPU-based computing systems. This has been especially so in speech recognition, machine vision, natural language processing, and language structure learning research (in which it has been used to explain a variety of phenomena related to first and second language learning.). Error backpropagation has been suggested to explain human brain ERP components like the N400 and P600.