自学内容网 自学内容网

pytorch基础-前向传播与反向传播

1 简单神经网络的前向传播与反向传播详解 [2,3,2,1]

1.1 网络结构

首先,让我们看看这个神经网络的结构:

输出层
隐藏层2
隐藏层1
输入层
w₁₁⁽¹⁾
w₁₂⁽¹⁾
w₁₃⁽¹⁾
w₂₁⁽¹⁾
w₂₂⁽¹⁾
w₂₃⁽¹⁾
w₁₁⁽²⁾
w₂₁⁽²⁾
w₃₁⁽²⁾
w₁₂⁽²⁾
w₂₂⁽²⁾
w₃₂⁽²⁾
w₁⁽³⁾
w₂⁽³⁾
y
h₁²
h₂²
h₁¹
h₂¹
h₃¹
x₁
x₂

1.2 前向传播推导

1.2.1 第一层隐藏层计算

h 1 ( 1 ) = w 11 ( 1 ) x 1 + w 21 ( 1 ) x 2 h_1^{(1)} = w_{11}^{(1)}x_1 + w_{21}^{(1)}x_2 h1(1)=w11(1)x1+w21(1)x2

h 2 ( 1 ) = w 12 ( 1 ) x 1 + w 22 ( 1 ) x 2 h_2^{(1)} = w_{12}^{(1)}x_1 + w_{22}^{(1)}x_2 h2(1)=w12(1)x1+w22(1)x2

h 3 ( 1 ) = w 13 ( 1 ) x 1 + w 23 ( 1 ) x 2 h_3^{(1)} = w_{13}^{(1)}x_1 + w_{23}^{(1)}x_2 h3(1)=w13(1)x1+w23(1)x2

矩阵形式:
[ h 1 ( 1 ) h 2 ( 1 ) h 3 ( 1 ) ] = [ w 11 ( 1 ) w 21 ( 1 ) w 12 ( 1 ) w 22 ( 1 ) w 13 ( 1 ) w 23 ( 1 ) ] [ x 1 x 2 ] \begin{bmatrix} h_1^{(1)} \\ h_2^{(1)} \\ h_3^{(1)} \end{bmatrix} = \begin{bmatrix} w_{11}^{(1)} & w_{21}^{(1)} \\ w_{12}^{(1)} & w_{22}^{(1)} \\ w_{13}^{(1)} & w_{23}^{(1)} \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} h1(1)h2(1)h3(1) = w11(1)w12(1)w13(1)w21(1)w22(1)w23(1) [x1x2]

1.2.2 第二层隐藏层计算

h 1 ( 2 ) = w 11 ( 2 ) h 1 ( 1 ) + w 21 ( 2 ) h 2 ( 1 ) + w 31 ( 2 ) h 3 ( 1 ) h_1^{(2)} = w_{11}^{(2)}h_1^{(1)} + w_{21}^{(2)}h_2^{(1)} + w_{31}^{(2)}h_3^{(1)} h1(2)=w11(2)h1(1)+w21(2)h2(1)+w31(2)h3(1)

h 2 ( 2 ) = w 12 ( 2 ) h 1 ( 1 ) + w 22 ( 2 ) h 2 ( 1 ) + w 32 ( 2 ) h 3 ( 1 ) h_2^{(2)} = w_{12}^{(2)}h_1^{(1)} + w_{22}^{(2)}h_2^{(1)} + w_{32}^{(2)}h_3^{(1)} h2(2)=w12(2)h1(1)+w22(2)h2(1)+w32(2)h3(1)

矩阵形式:
[ h 1 ( 2 ) h 2 ( 2 ) ] = [ w 11 ( 2 ) w 21 ( 2 ) w 31 ( 2 ) w 12 ( 2 ) w 22 ( 2 ) w 32 ( 2 ) ] [ h 1 ( 1 ) h 2 ( 1 ) h 3 ( 1 ) ] \begin{bmatrix} h_1^{(2)} \\ h_2^{(2)} \end{bmatrix} = \begin{bmatrix} w_{11}^{(2)} & w_{21}^{(2)} & w_{31}^{(2)} \\ w_{12}^{(2)} & w_{22}^{(2)} & w_{32}^{(2)} \end{bmatrix} \begin{bmatrix} h_1^{(1)} \\ h_2^{(1)} \\ h_3^{(1)} \end{bmatrix} [h1(2)h2(2)]=[w11(2)w12(2)w21(2)w22(2)w31(2)w32(2)] h1(1)h2(1)h3(1)

1.2.3 输出层计算

y = w 1 ( 3 ) h 1 ( 2 ) + w 2 ( 3 ) h 2 ( 2 ) y = w_1^{(3)}h_1^{(2)} + w_2^{(3)}h_2^{(2)} y=w1(3)h1(2)+


原文地址:https://blog.csdn.net/gudao07/article/details/143832597

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!