BackPropagation in detail

深度学习中的反向传播过程详解

综述

反向传播过程就是一个复合函数求导的过程,反向传播算法就是一个帮助我们求导的算法。多层神经网络的本质就是一个复合函数。

1. 为什么叫反向传播?

这个跟前向传播对应。在前向传播中,上一层中的权重是通过输出,来影响下一层的,因此在求隐藏层中w的导数时,是通过隐藏层的输出作为中间的桥梁来进行转换,这一点恰恰是沿着前向传播的路返回的。

2. 为什么不直接进行求导,而要使用复合函数求导(反向传播)的方法?

因为最后的损失函数是在输出层中产生的,求输出层中权重的导数时,可以直接求。但是如果要计算隐藏层中的导数的话,就需要写出复杂的函数表达式,才能够直接进行求导。

3. 反向传播的详细过程

为了讲清楚什么叫反向传播,以及反向传播到底是怎么计算的。首先构建如下神经网络模型:

BackPropagation in detail

这个神经网络包含三层,输入层为i1,i2,隐含层只有一层,为h1,h2,输出层为o1,o2。为一个全连接的网络。其中b1为第一隐含层的bias,b2为输出层的bias.

假设训练样本仅有1个,i1 = 0.05,i2 = 0.1.

初始化权重参数

w1 = 0.15, w2 = 0.20, w3 = 0.25, w4 = 0.30

w5 = 0.40 w6 = 0.45, w7 = 0.50, w8 = 0.55

初始化bias参数b1=0.35,b2=0.60b_1 = 0.35, b_2 = 0.60

目标输出targeto1=0.01,targeto2=0.99target_{o1} = 0.01, target_{o2} = 0.99

  • The forward pass(前向传播过程)

    neth1=w1×i1+w2×i2+1b1=0.15×0.05+0.20.1+1×0.35=0.3775net_{h1} = w1\times i_1 + w_2 \times i_2 + 1 * b1 = 0.15\times 0.05 + 0.2 * 0.1 + 1 \times 0.35 = 0.3775

    outh1=sigmoid(neth1)=11+eneth1=11+e0.3775=0.593269992out_{h1} = sigmoid(net_{h1}) = \frac{1}{1 + e^{-net_{h1}}} =\frac{1}{1 + e^{-0.3775} }= 0.593269992

    neth2=w3×i1+w4×i2+1×b2=0.25×0.05+0.30×0.1+10.35=0.3925net_{h2} = w_3 \times i_1 + w_4 \times i_2 + 1 \times b_2 = 0.25 \times 0.05 + 0.30 \times0.1 + 1 * 0.35 = 0.3925

    outh2=sigmoid(neth2)=11+eneth2=11+e0.3925=0.596884378out_{h2} = sigmoid(net_{h2}) = \frac { 1}{1 + e^{-net_{h2}} } = \frac { 1}{1 + e^{-0.3925} } = 0.596884378

    neto1=w5×outh1+w6×outh2+1b2=0.40×0.593269992+0.450.596884378+1×0.60=1.105905967net_{o1} = w5\times out_{h1} + w_6 \times out_{h2} + 1 * b2 = 0.40\times 0.593269992 + 0.45 * 0.596884378 + 1 \times 0.60 = 1.105905967

    outo1=sigmoid(neto1)=11+eneto1=11+e1.105905967=0.75136507out_{o1} = sigmoid(net_{o1}) = \frac{1}{1 + e^{-net_{o1}}} =\frac{1}{1 + e^{-1.105905967} } = 0.75136507

    neto2=w7×outh1+w8×outh2+1b2=0.50×0.593269992+0.550.596884378+1×0.60=1.2249214net_{o2} = w_7\times out_{h1} + w_8 \times out_{h2} + 1 * b2 = 0.50\times 0.593269992 + 0.55 * 0.596884378 + 1 \times 0.60 = 1.2249214

    outo2=sigmoid(neto2)=11+eneto2=11+e1.2249214=0.772928465out_{o2} = sigmoid(net_{o2}) = \frac{1}{1 + e^{-net_{o2}}} =\frac{1}{1 + e^{-1.2249214} } = 0.772928465

    calculating the total error

    Etotal=12(targetouput)2E_{total} = \sum \frac{1}{2}(target-ouput)^2

    target is desired output.

    output is what we get through the neuron

    (这里的1/2主要是为了求导的时候能够将指数消掉,仅此而已。因为后来修改权重的时候,也是要乘以学习率,所以这里是不影响的,而且还可以简化计算)
    BackPropagation in detail

  • BackPropagation(BP) 反向传播

BackPropagation in detail

可视化图形如下:
BackPropagation in detail
BackPropagation in detail
接下来计算隐藏层参数的导数,可视化图形如下:
BackPropagation in detail
BackPropagation in detail

BackPropagation in detail

BackPropagation in detail

BackPropagation in detail

如有错误,欢迎交流指正