Skip to content

1. 多层全连接神经网络

1.1 身份证问题

身份证的倒数第二个数字代表性别,奇数是男性,偶数是女性,我们希望神经网络能够找到这样的规律。

1.2 单层网络的模型

未免先设置单层网络,输入层有 x(4)x^{(4)} 大小,其中上标加上括号表示变元的数量或称为索引或者切片。

单层网络有四个输入,隐层也只有四个权值 w(4)w^{(4)} ,如果我们想提高网络我的性能,我们需要多层全连接网络,在数学层面表示就是矩阵乘法

[123456]×[123456]=[22284964]\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} \times \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix} = \begin{bmatrix} 22 & 28 \\ 49 & 64 \end{bmatrix}

我们使用 numpy 的时候,很容易进行矩阵乘法

python
import numpy as np
a = [[1, 2, 3], [4, 5, 6]]
b = [[1, 2], [3, 4], [5, 6]]
c = np.matmul(a, b)
print(c)

1.3 使用矩阵乘法实现全连接层

n=WX+b n = WX + b

y=σ(inni) y = \sigma\left(\sum^n_i n_i \right)

以后所有的 σ(x)\sigma(x) 都表示

σ(x)=11+ex \sigma(x) = \frac{1}{1+\mathrm{e}^{-x}}

误差函数使用均方误差

MSE=in(yiyTrain)2 \mathrm{MSE} = \sum^n_i \left(y_i - y_{\mathrm{Train}}\right)^2

激活函数使用 tanhx\tanh x

tanhx= sinhxcoshx= exexex+ex= 121e2x+1 \begin{aligned} \tanh x = &\ \frac{\sinh x}{\cosh x} \\ = &\ \frac{\mathrm{e}^x - \mathrm{e}^{-x}} {\mathrm{e}^x + \mathrm{e}^{-x}} \\ = &\ 1 - 2 \cdot \frac{1}{\mathrm{e}^{2x} + 1} \end{aligned}

所以有

tanhx=2σ(x)1 \tanh x = 2\sigma(x) - 1

激活函数 softmax(zi)\mathrm{softmax}(z_i)

softmax(zi)=ezic=1nezc \mathrm{softmax}(z_i) = \frac{\mathrm{e}^{z_i}} {\displaystyle \sum_{c=1}^n \mathrm{e}^{z_c}}

我们设计的新网络用公式表示如下

n1=tanh(xw1+b1) n_1 = \tanh \left(xw_1 + b_1\right)

n2=n1w2+b2 n_2 = n_1w_2 + b_2

本章下面的问题不再讨论,下面学习 TensorFlow 将主要参考 MOOC 课程。