2.2 正则化与与dropout(deep_ai)

背景

  1. 如果训练集不够大时,深度学习容易过拟合
    2)深度学习中,正则化
  2. 正则化模型
    a. regularization mode
    b. dropout mode

=================================================================

regularization注意事项:

2.2 正则化与与dropout(deep_ai)_第1张图片
a. 是超参数
b. L2正则可使得边界更加平滑, 如果 太大,可能会导致过度平滑。导致模型有较高的偏差。
c. L2正则实际逻辑: (缩小权重,输出变化变小)
What is L2-regularization actually doing?:
L2-regularization relies on the assumption that a model with small weights is simpler than a model with large weights.
Thus, by penalizing the square values of the weights in the cost function you drive all the weights to smaller values.
It becomes too costly for the cost to have large weights! This leads to a smoother model in which the output changes more slowly as the input changes.
What you should remember – the implications of L2-regularization on: - The cost computation: - A regularization term is added to the cost - The backpropagation function: - There are extra terms in the gradients with respect to weight matrices - Weights end up smaller (“weight decay”): - Weights are pushed to smaller values.

==================================================================

dropout

背景介绍:

(1)出现原因, 模型参数多,训练样本少,训练集损失函数小,预测准确率高,但测试数据损失大,预测准确率低。因此经常出现过拟合。
(2)机器学习过拟合一般采用: 模型集成, 训练多个模型进行组合。 但训练多个模型费时, 测试多个模型也很费时。
(3)训练深度学习网络遇到问题: 过拟合, 费时间。
(4)首次提出: Hinton 《Improving neural networks by preventing co-adaptation of feature detectors》
(5)dropout通过一定概率忽略一半的隐藏节点值,减少过拟合。
(6)这种方式也可以有效的减少隐藏节点间的相互作用, 相互作用指默写检测器以来其他检测器才能发挥作用。
(7)简单的说: 向前传播时候, 让某个神经元的激活值以一定的概率p停止工作,这样可以使模型泛化行更强。

Dropout (常用正则化技术)

a. 原理: 每次迭代的时候随机关掉一些神经单元
b. 每次迭代时, 按1-keep_prob的概率关掉一个层的某个单元。
c. 关掉的单元不会出现在前向和后项反馈中
d. 关掉的单元,以为着模型发生变化。 神经元与神经元的敏感度也会减少。
e. dropout不会用在输入层和输出层,一般用在隐藏层
f. Forward propagation with dropout ,以下四步:
    a). 每层有一个矩阵D, 与A的shape相同。第一隐藏层举例, 每个样本该隐藏层关闭的单元可能不一样,看原文是跟X相关的。
    b). 根据1-keep_prob设定D的值。(可以利用伯努利分布生成0,1向量)
    c). A = A * D  通过与矩阵D相乘,可以关闭特定位置的值
    d). A/keep_pro , 这样做的目的是确保cost跟没有drop_out是一样的。
    "deep_ai核型逻辑:
    D2 = np.random.rand(A2.shape[0], A2.shape[1]) #Step 1: initialize matrix D2 = np.random.rand(..., ...)
    D2 = D2 < keep_prob                                         # Step 2: convert entries of D2 to 0 or 1 (using keep_prob as the threshold)
    A2 = A2 * D2                                         # Step 3: shut down some neurons of A2
    A2 = A2/keep_prob "

注意事项:

1)只在训练的时候使用dropout
2)前向和后向反馈均需要使用dropout
3)在训练的时候, 输出A需要除以keep_pro(keep the same expected value for activations)
   大概意思是: 取平均期望。
   假设, keep_pro=0.5, 则会每层会关掉一半的节点,即只保留了一半的单元最用在最后的输出, 因此除以0.5相当于*2, 则此时与没有dropout时的期望一样。

dropout具体流程:

参考:https://blog.csdn.net/program_developer/article/details/80737724
2.2 正则化与与dropout(deep_ai)_第2张图片

(1)神经网络正常流程: 把X通过网络前向传播,然后把误差(即dA)法相传播决定如何更新参数
   (这里加深了DEEP_AI中backwark的理解。为什么先从dAL开始,且dAL= - (np.divide(Y, AL) - 1 * np.divide(1 - Y, 1 - AL)) 相符)。
(2) Dropout过程:
     1)首先随机(临时)删掉网络中一半的隐藏神经元, 输入输出神经元保持不变。
     2)然后把输入x通过修改后的网络向前传播, 再吧得到的损失结果通过修改的网络反响传播。
        注意: 一下批训练样本执行完成这个过程后, 在没有被删除的神经元傻姑娘按照随机提督下降法,更新对应的参数(w,b)
     3)继续重复如上过程:
        a. 恢复被删掉的神经元(此时被删除的神经元保持原样, 而没有被删除的神经元已经有所更新)
        b. 从隐藏层神经元中随机选择一个一半大小的子集临时除掉(备份被删除神经元的参数)
        c. 对小一批发训练样本, 先前向传播然后,反向传播损失并根据随机梯度下降法更新参数(w,b)
           (没有被删除的那一部分参数得到更新,删除的神经元参数保持删除前的结果)
     不断重复以上过程

Dropout实际应用

(1) 训练模型阶段:训练网络每个隐藏层单元需要添加一道概率流程。

2.2 正则化与与dropout(deep_ai)_第3张图片

1)r: 通过bernoulli函数随机生成一个0,1的向量。向量大小等于x输入大小。
如: 某一层网络神经元个数1000个, 激活函数输出值为y1~y1000. dropout比率选择0.4, 则经过dropout后, 1000个神经元会有400个值被置为0.
注意: 当某些神经元激活值为0后, 需要对向量y1~y1000进行缩放, 即乘以1/(1-p).(这里没太看明白, 还要再后面看看)

import numpy as np
# dropout函数的实现
def dropout(x, level):
    if level < 0. or level >= 1:  # level是概率值,必须在0~1之间
        raise Exception('Dropout level must be in interval [0, 1[.')
    retain_prob = 1. - level
    # 我们通过binomial函数,生成与x一样的维数向量。binomial函数就像抛硬币一样,我们可以把每个神经元当做抛硬币一样
    # 硬币 正面的概率为p,n表示每个神经元试验的次数
    # 因为我们每个神经元只需要抛一次就可以了所以n=1,size参数是我们有多少个硬币。
    sample = np.random.binomial(n=1, p=retain_prob, size=x.shape)  # 即将生成一个0、1分布的向量,0表示这个神经元被屏蔽,不工作了,也就是dropout了
    print ('sample', sample)
    x *= sample  # 0、1与x相乘,我们就可以屏蔽某些神经元,让它们的值变为0
    print('after dropout x', x)
    x /= retain_prob
    print('after rescale', x)
    #
    return x

# 对dropout的测试,大家可以跑一下上面的函数,了解一个输入x向量,经过dropout的结果
x = np.asarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=np.float32)
x = dropout(x, 0.4)

=================================
out_put:
sample [1 1 1 1 0 1 1 1 0 0]
after dropout x [1. 2. 3. 4. 0. 6. 7. 8. 0. 0.]
after rescale [ 1.6666666  3.3333333  5.         6.6666665  0.        10.
 11.666666  13.333333   0.         0.       ]

参考文献:
《Improving neural networks by preventing co-adaptation of feature detectors》hinton
《ImageNet Classification with Deep Convolutional》
《Improving neural networks by preventing co-adaptation of feature detectors》、
《Improving Neural Networks with Dropout》、
《Dropout: A Simple Way to Prevent Neural Networks from Overtting》

你可能感兴趣的:(深度学习)