Backpropagation derivation using Leibniz notation: Difference between revisions

From Machinelearning
No edit summary
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
This page presents a '''derivation/proof of backpropagation derivation using Leibniz notation'''. Leibniz notation is the most common notation for presenting backpropagation, but it is somewhat complicated due to its blurring of the function/value distinction and its reliance on functional relationships being implicit. Those who prefer function notation may wish to refer to [[backpropagation derivation using function notation]] instead of (or in addition to) this page.
Most of the notation on this page is borrowed from Michael Nielsen's book.<ref>[http://neuralnetworksanddeeplearning.com/chap2.html "Chapter 2: How the backpropagation algorithm works"] in ''Neural Networks and Deep Learning''. Michael A. Nielsen. ''Determination Press''. 2015. Retrieved November 8, 2018.</ref>
==Theorem statement==
Let <math>N</math> be a neural network with <math>L</math> layers and <math>n(l)</math> be the number of neurons in layer <math>l</math> for <math>l \in \{1, \ldots, L\}</math>. For <math>l \in \{2, \ldots, L\}</math>, <math>k\in\{1, \ldots, n(l-1)\}</math>, and <math>j \in \{1, \ldots, n(l)\}</math> let <math>w^l_{jk} \in \mathbf R</math> be the weight from the <math>k</math>th neuron in the <math>(l-1)</math>th layer to the <math>j</math>th neuron in the <math>l</math>th layer. Let <math>z^l_j = \sum_{k=1}^{n(l-1)} w^l_{jk}a^{l-1}_k + b^l_j</math> and let <math>a^l_j = \sigma(z^l_j)</math>, where <math>\sigma : \mathbf R \to \mathbf R</math> is the [[sigmoid function]]. Let <math>C = \frac12 \sum_{j=1}^{n(L)} (y_j - a^L_j)^2</math> be a cost function. Then we can calculate the partial derivatives <math>\frac{\partial C}{\partial w^l_{jk}}</math> and <math>\frac{\partial C}{\partial b^l_j}</math> starting from the later layers. Specifically, we have
<math display="block">\frac{\partial C}{\partial w^l_{jk}} = \left(\sum_{i=1}^{n(l+1)} \frac{\partial C}{\partial a^{l+1}_i} \sigma'(z^{l+1}_i)w^{l+1}_{ij}\right) \sigma'(z^l_j)a^{l-1}_k</math>
and
<math display="block">\frac{\partial C}{\partial b^l_j} = ???</math>
==Proof==
We induct on the layer number <math>l</math>, starting at <math>l=L</math>. For the base case, we have
<math display="block">\frac{\partial C}{\partial w^L_{jk}} = \frac{\partial C}{\partial a^L_j} \frac{\partial a^L_j}{\partial w^L_{jk}} = \frac{\partial C}{\partial a^L_j} \sigma'(z^L_j)a^{L-1}_k </math>
We also have
<math display="block">\frac{\partial C}{\partial a^L_j} = a^L_j - y_j</math>
The cost function <math>C</math> depends on <math>w^l_{jk}</math> only through the activation of the <math>j</math>th neuron in the <math>l</math>th layer, i.e. on the value of <math>a^l_j</math>. Thus we can use the chain rule to expand:
The cost function <math>C</math> depends on <math>w^l_{jk}</math> only through the activation of the <math>j</math>th neuron in the <math>l</math>th layer, i.e. on the value of <math>a^l_j</math>. Thus we can use the chain rule to expand:


<math display="block">\frac{\partial C}{\partial w^l_{jk}} = \frac{\partial C}{\partial a^l_j} \frac{\partial a^l_j}{\partial w^l_{jk}}</math>
<math display="block">\frac{\partial C}{\partial w^l_{jk}} = \frac{\partial C}{\partial a^l_j} \frac{\partial a^l_j}{\partial w^l_{jk}}</math>


We know that <math>\frac{\partial a^l_j}{\partial w^l_{jk}} = \sigma'(z^l_j)a^{l-1}_k</math> because <math>a^l_j = \sigma(z^l_j) = \sigma\left(\sum_k w^l_{jk}a^{l-1}_k + b^l_j\right)</math>. We have used the chain rule again here.
We know that <math>\frac{\partial a^l_j}{\partial w^l_{jk}} = \sigma'(z^l_j)a^{l-1}_k</math> because <math>a^l_j = \sigma(z^l_j) = \sigma\left(\sum_{k=1}^{n(l-1)} w^l_{jk}a^{l-1}_k + b^l_j\right)</math>. We have used the chain rule again here.


In turn, <math>C</math> depends on <math>a^l_j</math> only through the activations of the <math>(l+1)</math>th layer. Thus we can write (using the chain rule once again):
In turn, <math>C</math> depends on <math>a^l_j</math> only through the activations of the <math>(l+1)</math>th layer. Thus we can write (using the chain rule once again):


<math display="block">\frac{\partial C}{\partial a^l_j} = \sum_{i=1}^n(l+1) \frac{\partial C}{\partial a^{l+1}_i} \frac{\partial a^{l+1}_i}{\partial a^l_j}</math>
<math display="block">\frac{\partial C}{\partial a^l_j} = \sum_{i=1}^{n(l+1)} \frac{\partial C}{\partial a^{l+1}_i} \frac{\partial a^{l+1}_i}{\partial a^l_j}</math>
 
where <math>n(l+1)</math> is the number of neurons in the <math>(l+1)</math>th layer.


Backpropagation works recursively starting at the later layers. Since we are trying to compute <math>\frac{\partial C}{\partial a^l_j}</math> for the <math>l</math>th layer, we can assume inductively that we have already computed <math>\frac{\partial C}{\partial a^{l+1}_i}</math>.
Backpropagation works recursively starting at the later layers. Since we are trying to compute <math>\frac{\partial C}{\partial a^l_j}</math> for the <math>l</math>th layer, we can assume inductively that we have already computed <math>\frac{\partial C}{\partial a^{l+1}_i}</math>.


It remains to find <math>\frac{\partial a^{l+1}_i}{\partial a^l_j}</math>. But <math>a^{l+1}_i = \sigma(z^{l+1}_i) = \sigma\left(\sum_j w^{l+1}_{ij}a^l_j + b^{l+1}_i\right)</math> so we have
It remains to find <math>\frac{\partial a^{l+1}_i}{\partial a^l_j}</math>. But <math>a^{l+1}_i = \sigma(z^{l+1}_i) = \sigma\left(\sum_{j=1}^{n(l)} w^{l+1}_{ij}a^l_j + b^{l+1}_i\right)</math> so we have


<math display="block">\frac{\partial a^{l+1}_i}{\partial a^l_j} = \sigma'(z^{l+1}_i)w^{l+1}_{ij}</math>
<math display="block">\frac{\partial a^{l+1}_i}{\partial a^l_j} = \sigma'(z^{l+1}_i)w^{l+1}_{ij}</math>
Line 19: Line 41:
Putting all this together, we obtain
Putting all this together, we obtain


<math display="block">\begin{align}\frac{\partial C}{\partial w^l_{jk}} &= \frac{\partial C}{\partial a^l_j} \frac{\partial a^l_j}{\partial w^l_{jk}} \\ &= \left(\sum_{i \in \{1,\ldots,n(l+1)\}} \frac{\partial C}{\partial a^{l+1}_i} \frac{\partial a^{l+1}_i}{\partial a^l_j}\right) \sigma'(z^l_j)a^{l-1}_k \\ &= \left(\sum_{i \in \{1,\ldots,n(l+1)\}} \frac{\partial C}{\partial a^{l+1}_i} \sigma'(z^{l+1}_i)w^{l+1}_{ij}\right) \sigma'(z^l_j)a^{l-1}_k\end{align}</math>
<math display="block">\begin{align}\frac{\partial C}{\partial w^l_{jk}} &= \frac{\partial C}{\partial a^l_j} \frac{\partial a^l_j}{\partial w^l_{jk}} \\ &= \left(\sum_{i=1}^{n(l+1)} \frac{\partial C}{\partial a^{l+1}_i} \frac{\partial a^{l+1}_i}{\partial a^l_j}\right) \sigma'(z^l_j)a^{l-1}_k \\ &= \left(\sum_{i=1}^{n(l+1)} \frac{\partial C}{\partial a^{l+1}_i} \sigma'(z^{l+1}_i)w^{l+1}_{ij}\right) \sigma'(z^l_j)a^{l-1}_k\end{align}</math>
 
Let us verify that we can calculate the right-hand side. By induction hypothesis, we can calculate <math>\frac{\partial C}{\partial a^{l+1}_i}</math>. We calculate <math>z^{l+1}_i</math>, <math>z^l_j</math>, and <math>a^{l-1}_k</math> during the forward pass through the network. Finally, <math>w^{l+1}_{ij}</math> is just a weight in the network, so we already know its value.
 
==References==
 
<references/>

Latest revision as of 00:26, 9 November 2018

This page presents a derivation/proof of backpropagation derivation using Leibniz notation. Leibniz notation is the most common notation for presenting backpropagation, but it is somewhat complicated due to its blurring of the function/value distinction and its reliance on functional relationships being implicit. Those who prefer function notation may wish to refer to backpropagation derivation using function notation instead of (or in addition to) this page.

Most of the notation on this page is borrowed from Michael Nielsen's book.[1]

Theorem statement

Let N be a neural network with L layers and n(l) be the number of neurons in layer l for l{1,,L}. For l{2,,L}, k{1,,n(l1)}, and j{1,,n(l)} let wjklR be the weight from the kth neuron in the (l1)th layer to the jth neuron in the lth layer. Let zjl=k=1n(l1)wjklakl1+bjl and let ajl=σ(zjl), where σ:RR is the sigmoid function. Let C=12j=1n(L)(yjajL)2 be a cost function. Then we can calculate the partial derivatives Cwjkl and Cbjl starting from the later layers. Specifically, we have

Cwjkl=(i=1n(l+1)Cail+1σ(zil+1)wijl+1)σ(zjl)akl1

and

Cbjl=???

Proof

We induct on the layer number l, starting at l=L. For the base case, we have

CwjkL=CajLajLwjkL=CajLσ(zjL)akL1

We also have

CajL=ajLyj

The cost function C depends on wjkl only through the activation of the jth neuron in the lth layer, i.e. on the value of ajl. Thus we can use the chain rule to expand:

Cwjkl=Cajlajlwjkl

We know that ajlwjkl=σ(zjl)akl1 because ajl=σ(zjl)=σ(k=1n(l1)wjklakl1+bjl). We have used the chain rule again here.

In turn, C depends on ajl only through the activations of the (l+1)th layer. Thus we can write (using the chain rule once again):

Cajl=i=1n(l+1)Cail+1ail+1ajl

Backpropagation works recursively starting at the later layers. Since we are trying to compute Cajl for the lth layer, we can assume inductively that we have already computed Cail+1.

It remains to find ail+1ajl. But ail+1=σ(zil+1)=σ(j=1n(l)wijl+1ajl+bil+1) so we have

ail+1ajl=σ(zil+1)wijl+1

Putting all this together, we obtain

Cwjkl=Cajlajlwjkl=(i=1n(l+1)Cail+1ail+1ajl)σ'(zjl)akl1=(i=1n(l+1)Cail+1σ(zil+1)wijl+1)σ'(zjl)akl1

Let us verify that we can calculate the right-hand side. By induction hypothesis, we can calculate Cail+1. We calculate zil+1, zjl, and akl1 during the forward pass through the network. Finally, wijl+1 is just a weight in the network, so we already know its value.

References

  1. "Chapter 2: How the backpropagation algorithm works" in Neural Networks and Deep Learning. Michael A. Nielsen. Determination Press. 2015. Retrieved November 8, 2018.