1. 人体神经网络

上图为人体的神经网络,其工作原理:
- 外部刺激通过神经末梢,转化为电信号,转导到神经细胞(又叫神经元)。
- 无数神经元构成神经中枢。
- 神经中枢综合各种信号,做出判断。
- 人体根据神经中枢的指令,对外部刺激做出反应。
随着神经网络的发展,现在已经不再使用上面的示例解释目前的神经网络了。这是因为现在的神经网络有反向传播的过程,但这个在人体的神经网络是没有这个过程的。
2. 神经网络架构

上图为一个简单的神经网络,每个圆圈代表一个神经元。最左侧为输入层,该层的神经元成为输入单元,最右侧的为输出层,该层神经元为输出单元,中间的层统称为隐藏层,这些层的单元称为隐藏单元。该网络为3层神经网络(输入层不记入网络层数)。
下面根据几个简单的实例来看下神经网络的工作过程。但在此之前,需要先介绍下神经网络中常用的参数:
2.1 神经网络参数
General comments:
- superscript (i) will denote the ith training example while superscript [l] will denote the lth layer
Sizes
-
m : number of examples in the dataset
-
nx : input size
-
ny : output size
-
nh[l] : number of hidden unites of the lth layer
-
L : number of layers in teh network
Objects
-
X∈Rnx×m : is the input matrix
-
x(i)∈Rnx : is the ith example represented as a column vector
-
Y∈Rnx×m : is the label matrix
-
y(i)∈Rnx : is the output label for the ith example
-
W[l]∈Rnh[l]×nh[l−1] : is the weight matrix
-
b[l]∈Rnh[l] : is the bias vector in the lth layer
-
y^∈Rny : is the predicted output vector
2.2 构建逻辑回归模型的神经网路

该网络十分简单,一共2层:输入和输出
- 根据上述的神经网络参数介绍,网络参数维度为:
nx∈ 12288 x m
ny∈ 1 x m
W[1]∈ 1 x 12288
b[1]∈ 1 x 1
然后需要对参数W[1] b[1]进行初始化。神经网络的参数初始化方法很多,后续的文章里面会专门讲。
- 那么有了上述的参数后,我们还需要了解几个神经网络中的函数:
-
Activation function:
对于全连接网络中的一个样本 x(i),它所有的输入单元都会连接到下一层的每一个神经元上面。每个神经元会做两件事:
– 线性函数 z: z(i)=wTx(i)+b
– **函数 σ:a(i)=σ(z(i))
该例中,它的**函数为sigmoid,y^=a(i)=sigmoid(z(i))
-
Loss function:
用于表示每个样本的差异:L(a(i),y(i))=−(y(i)log(a(i))+(1−y(i))log(1−a(i)))
-
Cost function:
指的是计算所有样本的Loss function:J=m1∑i=1mL(a(i),y(i))
- 接下来就是神经网络的计算过程了,主要包括2个过程:
-
前向传播Forward propagration:
它的目的就是将m个样本的输入的特征向量X通过层层的网络计算后得到最后的输出Y^,然后计算出cost J。具体来讲,前向传播的过程是:
Z[1]=W[1]X+b[1]
A[1]=sigmoid(Z[1])
Z[2]=W[2]A[1]+b[2]
A[2]=sigmoid(Z[2])
Y^=A[2]
J=−m1(Ylog(Y^)T+(1−Y)log(1−Y^)T)
-
反向传播Backward propagation:
它的目的就是通过最小化J,更新参数W[1]和b[1]。而最小化的过程就是梯度下降的过程:
dW[1]=∂W[1]∂L
db[1]=∂b[1]∂L
W[1]=W[1]−α dW[1]
b[1]=b[1]−α db[1]
总结:构建一个神经网络的主要步骤如下
- Define the model structure (such as number of input features)
- Initialize the model’s parameters(W[1],b[1])
- Loop:
- Caltulate current loss(forward propagation)
- Calculate current gradient(backward propagation)
- Udpate parameters(gradient descent)
2.3 代码实例
LogisticRegressionNeuralNetwork