神经网络
BP 向后传播算法概念
像这种网络叫做全连接网络,也叫做前向传播网络
w 叫做每一次传播的权重 b则是偏置
假设由一组训练集
(
x
1
→
,
y
1
→
)
(
x
2
→
,
y
2
→
)
⋯
(
x
n
→
,
y
n
→
)
(\overrightarrow{x_1},\overrightarrow{y_1})\quad(\overrightarrow{x_2},\overrightarrow{y_2})\cdots(\overrightarrow{x_n},\overrightarrow{y_n})
(x1,y1)(x2,y2)⋯(xn,yn) x为特征属性 y为标签
假如将训练集中拿一个作为输入进入神经网络,若输出
Y
i
=
y
i
Y_i=y_i
Yi=yi 说明这个神经网络不错
E = 1 2 ∑ i = 1 n ( y i − Y i ) 2 E=\frac{1}{2}\sum\limits_{i=1}^n(y_i-Y_i)^2 E=21i=1∑n(yi−Yi)2 为神经网络的损失函数,评判模型的好坏
怎样去更新权重w和偏置b呢?则是利用 梯度下降算法
-
取样本值 ( x 1 → , y 1 → ) ( x 2 → , y 2 → ) ⋯ ( x n → , y n → ) (\overrightarrow{x_1},\overrightarrow{y_1})\quad(\overrightarrow{x_2},\overrightarrow{y_2})\cdots(\overrightarrow{x_n},\overrightarrow{y_n}) (x1,y1)(x2,y2)⋯(xn,yn) 作为神经网络的输入
-
然后 w i b i 对 E 求偏导为 ∂ E ∂ w ∂ E ∂ b 然后 w_i b_i对E求偏导 为\dfrac{\partial E}{\partial w} \dfrac{\partial E}{\partial b} 然后wibi对E求偏导为∂w∂E∂b∂E
-
迭代 更新w b
w 新 = w 旧 − α ∂ E ∂ w 旧 w_新 = w_旧 - \alpha\dfrac{\partial E}{\partial w_旧} w新=w旧−α∂w旧∂E
b 新 = b 旧 − α ∂ E ∂ b 旧 b_新 = b_旧 - \alpha\dfrac{\partial E}{\partial b_旧} b新=b旧−α∂b旧∂E
-
当所有 ∂ E ∂ w ∂ E ∂ b 都为 0 ,更新完毕 当所有\dfrac{\partial E}{\partial w} \dfrac{\partial E}{\partial b} 都为0,更新完毕 当所有∂w∂E∂b∂E都为0,更新完毕
BP 向后传播算法推导
由图可知
z
1
=
w
1
x
1
+
b
1
z_1 = w_1x_1 + b_1
z1=w1x1+b1
n
1
=
f
(
z
1
)
n_1 = f(z_1)
n1=f(z1)
z
2
=
w
2
n
1
+
b
2
z_2 = w_2 n_1+ b_2
z2=w2n1+b2
n
2
=
f
(
z
2
)
n_2 = f(z_2)
n2=f(z2)
Y
=
n
2
=
f
2
(
z
2
)
=
f
2
(
w
2
n
1
+
b
2
)
=
f
2
(
w
2
f
1
(
z
1
)
+
b
2
)
=
f
2
(
w
2
f
1
(
w
1
x
1
+
b
1
)
+
b
2
)
Y = n_2 = f_2(z_2) = f_2(w_2n_1 + b_2) = f_2(w_2f_1(z_1) + b_2) = f_2(w_2f_1(w_1x_1 + b_1) + b_2)
Y=n2=f2(z2)=f2(w2n1+b2)=f2(w2f1(z1)+b2)=f2(w2f1(w1x1+b1)+b2)
w
1
对
E
求偏导
w_1对E求偏导
w1对E求偏导
∂
E
∂
w
1
=
1
2
(
Y
−
y
)
2
∂
w
1
=
(
Y
−
y
)
∂
Y
∂
w
1
=
(
Y
−
y
)
f
2
‘
(
z
2
)
w
2
f
1
‘
(
z
1
)
x
1
\dfrac{\partial E}{\partial w_1} = \dfrac{\frac{1}{2}(Y - y)^2}{\partial w_1} = (Y - y)\dfrac{\partial Y}{\partial w_1} = (Y - y)f^`_2(z_2)w_2f^`_1(z_1)x_1
∂w1∂E=∂w121(Y−y)2=(Y−y)∂w1∂Y=(Y−y)f2‘(z2)w2f1‘(z1)x1
假设有m层 每一层用l表示
则有
z
l
=
w
i
n
l
−
1
+
b
l
z_l = w_in_{l-1} + b_l
zl=winl−1+bl
w
l
对
E
求偏导,如下
w_l对E求偏导,如下
wl对E求偏导,如下
∂
E
∂
w
l
=
∂
E
∂
z
l
∂
z
l
∂
w
l
\dfrac{\partial E}{\partial w_l} = \dfrac{\partial E}{\partial z_l}\dfrac{\partial z_l}{\partial w_l}
∂wl∂E=∂zl∂E∂wl∂zl 容易知道
∂
z
l
∂
w
l
=
n
l
−
1
\dfrac{\partial z_l}{\partial w_l}=n_{l-1}
∂wl∂zl=nl−1
那
∂
E
∂
z
l
该怎么求呢?
那\dfrac{\partial E}{\partial z_l}该怎么求呢?
那∂zl∂E该怎么求呢?
可以知道
z
l
+
1
=
w
l
+
1
n
l
+
b
l
+
1
=
w
l
+
1
f
l
(
z
l
)
+
b
l
+
1
z_{l+1} = w_{l+1}n_l + b_{l+1} = w_{l+1}f_l(z_l) + b_{l+1}
zl+1=wl+1nl+bl+1=wl+1fl(zl)+bl+1
因为
n
l
=
f
l
(
z
l
)
n_l = f_l(z_l)
nl=fl(zl) ,才有上面的结果
∂
E
∂
z
l
求解过程如下
\dfrac{\partial E}{\partial z_l}求解过程如下
∂zl∂E求解过程如下
∂
E
∂
z
l
=
∂
E
∂
z
l
+
1
∂
z
l
+
1
∂
z
l
=
∂
E
∂
z
l
+
1
∂
z
l
+
1
∂
n
l
∂
n
l
∂
z
l
=
∂
E
∂
z
l
+
1
(
w
l
+
1
)
T
f
l
‘
(
z
l
)
\dfrac{\partial E}{\partial z_l} = \dfrac{\partial E}{\partial z_{l+1}}\dfrac{\partial z_{l+1}}{\partial z_l} = \dfrac{\partial E}{\partial z_{l+1}}\dfrac{\partial z_{l+1}}{\partial n_l}\dfrac{\partial n_l}{\partial z_l} = \dfrac{\partial E}{\partial z_{l+1}}(w_{l+1})^Tf^`_l(z_l)
∂zl∂E=∂zl+1∂E∂zl∂zl+1=∂zl+1∂E∂nl∂zl+1∂zl∂nl=∂zl+1∂E(wl+1)Tfl‘(zl)
令
∂
E
∂
z
l
=
δ
l
\dfrac{\partial E}{\partial z_l} = \delta_l
∂zl∂E=δl 则
δ
l
=
δ
l
+
1
(
w
l
+
1
)
T
f
l
‘
(
z
l
)
\delta_l = \delta_{l+1}(w_{l+1})^Tf^`_l(z_l)
δl=δl+1(wl+1)Tfl‘(zl) 依次向后推,即可求出
∂
E
∂
z
l
\dfrac{\partial E}{\partial z_l}
∂zl∂E
推到最后一层
∂
E
∂
z
m
=
∂
E
∂
y
\dfrac{\partial E}{\partial z_m} = \dfrac{\partial E}{\partial y}
∂zm∂E=∂y∂E
δ
叫做误差项,或者叫做灵敏度
\delta叫做误差项,或者叫做灵敏度
δ叫做误差项,或者叫做灵敏度
BP 向后传播算法推导到此也就可以推导出来了👍