1. 人体神经网络

深度学习 -- 神经网络 1

上图为人体的神经网络,其工作原理:

  • 外部刺激通过神经末梢,转化为电信号,转导到神经细胞(又叫神经元)。
  • 无数神经元构成神经中枢。
  • 神经中枢综合各种信号,做出判断。
  • 人体根据神经中枢的指令,对外部刺激做出反应。

随着神经网络的发展,现在已经不再使用上面的示例解释目前的神经网络了。这是因为现在的神经网络有反向传播的过程,但这个在人体的神经网络是没有这个过程的。

2. 神经网络架构

深度学习 -- 神经网络 1

上图为一个简单的神经网络,每个圆圈代表一个神经元。最左侧为输入层,该层的神经元成为输入单元,最右侧的为输出层,该层神经元为输出单元,中间的层统称为隐藏层,这些层的单元称为隐藏单元。该网络为3层神经网络(输入层不记入网络层数)。

下面根据几个简单的实例来看下神经网络的工作过程。但在此之前,需要先介绍下神经网络中常用的参数:

2.1 神经网络参数

General comments:

  • superscript (i)(i) will denote the ithi^{th} training example while superscript [l][l] will denote the lthl^{th} layer

Sizes

  • mm : number of examples in the dataset
  • nxn_x : input size
  • nyn_y : output size
  • nh[l]n^{[l]}_h : number of hidden unites of the lthl^{th} layer
  • LL : number of layers in teh network

Objects

  • XRnx×mX \in R^{n_x × m} : is the input matrix
  • x(i)Rnxx^{(i)} \in R^{n_x} : is the ithi^{th} example represented as a column vector
  • YRnx×mY \in R^{n_x × m} : is the label matrix
  • y(i)Rnxy^{(i)} \in R^{n_x} : is the output label for the ithi^{th} example
  • W[l]Rnh[l]×nh[l1]W^{[l]} \in R^{n_h^{[l]} × n_h^{[l-1]}} : is the weight matrix
  • b[l]Rnh[l]b^{[l]} \in R^{n_h^{[l]}} : is the bias vector in the lthl^{th} layer
  • y^Rny\hat y \in R^{n_y} : is the predicted output vector

2.2 构建逻辑回归模型的神经网路

深度学习 -- 神经网络 1
该网络十分简单,一共2层:输入和输出

  1. 根据上述的神经网络参数介绍,网络参数维度为:
    nxn_x \in 12288 x m
    nyn_y \in 1 x m
    W[1]W^{[1]} \in 1 x 12288
    b[1]b^{[1]} \in 1 x 1

然后需要对参数W[1]W^{[1]} b[1]b^{[1]}进行初始化。神经网络的参数初始化方法很多,后续的文章里面会专门讲。

  1. 那么有了上述的参数后,我们还需要了解几个神经网络中的函数:
  • Activation function:
    对于全连接网络中的一个样本 x(i)x^{(i)},它所有的输入单元都会连接到下一层的每一个神经元上面。每个神经元会做两件事:
    – 线性函数 zzz(i)=wTx(i)+bz^{(i)} = w^Tx^{(i)} + b
    – **函数 σ\sigmaa(i)=σ(z(i))a^{(i)} = \sigma(z^{(i)})
    该例中,它的**函数为sigmoid,y^=a(i)=sigmoid(z(i))\hat y = a^{(i)} = sigmoid(z^{(i)})

  • Loss function:
    用于表示每个样本的差异:L(a(i),y(i))=(y(i)log(a(i))+(1y(i))log(1a(i)))L(a^{(i)}, y^{(i)}) = - (y^{(i)} log(a^{(i)}) + (1 - y^{(i)}) log(1 - a^{(i)}))

  • Cost function:
    指的是计算所有样本的Loss function:J=1mi=1mL(a(i),y(i))J = \frac{1}{m} \sum_{i=1}^{m} L(a^{(i)}, y^{(i)})

  1. 接下来就是神经网络的计算过程了,主要包括2个过程:
  • 前向传播Forward propagration:
    它的目的就是将mm个样本的输入的特征向量XX通过层层的网络计算后得到最后的输出Y^\hat Y,然后计算出cost JJ。具体来讲,前向传播的过程是:
    Z[1]=W[1]X+b[1]Z^{[1]} = W^{[1]}X + b^{[1]}
    A[1]=sigmoid(Z[1])A^{[1]} = sigmoid(Z^{[1]})
    Z[2]=W[2]A[1]+b[2]Z^{[2]} = W^{[2]}A^{[1]} + b^{[2]}
    A[2]=sigmoid(Z[2])A^{[2]} = sigmoid(Z^{[2]})
    Y^=A[2]\hat Y = A^{[2]}
    J=1m(Ylog(Y^)T+(1Y)log(1Y^)T)J = -\frac{1}{m} (Y log(\hat Y)^T + (1 - Y)log(1 - \hat Y)^T)

  • 反向传播Backward propagation:
    它的目的就是通过最小化JJ,更新参数W[1]W^{[1]}b[1]b^{[1]}。而最小化的过程就是梯度下降的过程:
    dW[1]=LW[1] dW^{[1]} = \frac{\partial \mathcal{L} }{\partial W^{[1]}}
    db[1]=Lb[1] db^{[1]} = \frac{\partial \mathcal{L} }{\partial b^{[1]}}
    W[1]=W[1]α dW[1] W^{[1]} = W^{[1]} - \alpha \text{ } dW^{[1]}
    b[1]=b[1]α db[1] b^{[1]} = b^{[1]} - \alpha \text{ } db^{[1]}

总结:构建一个神经网络的主要步骤如下

  • Define the model structure (such as number of input features)
  • Initialize the model’s parameters(W[1],b[1]W^{[1]}, b^{[1]})
  • Loop:
    • Caltulate current loss(forward propagation)
    • Calculate current gradient(backward propagation)
    • Udpate parameters(gradient descent)

2.3 代码实例

LogisticRegressionNeuralNetwork

相关文章: