论文笔记:Dropout(全方位刨析Dropout)

前言

之前就已经学习过dropout,并且实现且使用过,但总感觉对其中的一些细节掌握的不是太好,所以找来了Dropout的原论文来读一下。

论文地址:Dropout: A Simple Way to Prevent Neural Networks from Overfitting

概述:

当我们训练一个比较大、参数比较多的模型的时候,我们往往都需要用各种手段去解决过拟合问题。Dropout的就是用来解决过拟合的,它是指在训练的过程中,从网络中随机抛弃一些隐藏单元,这么做就好像是从一个很大的模型里,随机抽取了一个子模型,每个子模型都共享参数。到最后,就相当于我们训练了非常多的子模型,进行预测的时候,综合考虑所有子模型的预测结果,这就在一定程度上解决了过拟合问题。

为了解决过拟合,我们可能会训练很多单个模型,这些模型的结构和训练数据均不相同,然后将他组合起来,综合评估预测结构。这确实非常有效,但是这需要非常大的训练数据集,以及需要非常大的训练资源。 即使我们训练出了这些模型,我们还需要考虑怎么样对这些模型进行评估。 作者原文是这样说的:

Combining several models is most helpful when the individual models are different from each other and in order to make neural net models different, they should either have different architectures or be trained on different data. Training many different architectures is hard because finding optimal hyperparameters for each architecture is a daunting task and training each large network requires a lot of computation. Moreover, large networks normally require large amounts of training data and there may not be enough data available to train different networks on different subsets of the data. Even if one was able to train many different large networks, using them all at test time is infeasible in applications where it is important to respond quickly.

为了解决模型综合评估训练数据集巨大的问题,作者提出了dropout。


dropout(无数学分析)

dropout的做法,其实就是在训练过程中,随机弃用一些神经元(不接受它的输入,也不让它输出)。这个过程是动态的,也就是说每次训练弃用的神经元都可能不同。可以用一个固定的概率值来描述,即每个神经元在训练的过程中只有P的概率被保留。原文描述如下

The term “dropout” refers to dropping out units (hidden and visible) in a neural network. By dropping a unit out, we mean temporarily removing it from the network, along with all its incoming and outgoing connections, as shown in Figure 1. The choice of which units to drop is random. In the simplest case, each unit is retained with a fixed probability p independent of other units, where p can be chosen using a validation set or can simply be set at 0.5, which seems to be close to optimal for a wide range of networks and tasks. For the input units, however, the optimal probability of retention is usually closer to 1 than to 0.5.

原论文中的图片就可以很好的描述上述过程
在这里插入图片描述
左边是原始的网络,右边是网络中的每一层都有部分神经元被弃用。


当我们使用dropout的时候,其实就相当于在训练一个一个的比原始网络更瘦、更小的子模型。这些子模型使用的参数都是相同的,而且这些子模型的数量是指数级别的,所以不可能每个模型都被训练的很好,在极端的情况下就是某些参数(模型模型)一直都没有被训练。但是这并不影响我们模型的整体水平。类似于Bagging算法但又不一样,因为后者的参数都是共享的。

原论文描述如下:

Applying dropout to a neural network amounts to sampling a “thinned” network from it. The thinned network consists of all the units that survived dropout (Figure 1b). A neural net with n units, can be seen as a collection of 2n possible thinned neural networks. These networks all share weights so that the total number of parameters is still O(n2), or less. For each presentation of each training case, a new thinned network is sampled and trained. So training a neural network with dropout can be seen as training a collection of 2n thinned networks with extensive weight sharing, where each thinned network gets trained very rarely, if at all.


那么在训练的时候,我们使用了dropout,即每个神经元只有概率P的可能性被保留下来,那在真正的生产环境中,我们是否还要dropout呢?

肯定不需要了,如果还dropout就相当于使用子模型进行预测了。这时候问题就是我们该如何综合其所有子模型的预测结果呢?

其实非常简单,既然每个神经元被保留下的概率是P,那么在进行真正的预测的时候,我们只需将每个神经元的权重乘以P即可,如下图,
在这里插入图片描述
你可能会问这么做效果咋样?不用担心,效果很好的。

原论文描述如下:

However, a very simple approximate averaging method works well in practice. The idea is to use a single neural net at test time without dropout. The weights of this network are scaled-down versions of the trained weights. If a unit is retained with probability p during training, the outgoing weights of that unit are multiplied by p at test time as shown in Figure 2. This ensures that for any hidden unit the expected output (under the distribution used to drop units at training time) is the same as the actual output at test time. By doing this scaling, 2n networks with shared weights can be combined into a single neural network to be used at test time. We found that training a network with dropout and using this approximate averaging method at test time leads to significantly lower generalization error on a wide variety of classification problems compared to training with other regularization methods.


Motivation(动机)

作者脑洞为啥这么大,是咋想出来如此巧妙的设计方法呢?
作则在论文中说到,其灵感来自a theory of the role of sex in evolution。这里不再赘述。

作者给出了一些dropout取得如此好的效果的一些直观的解释。

因为每个神经元都必须学会与所有不同的神经元相互配合,不能产生依赖,因为如果某个神经元依赖另外一个神经元的话,如果被依赖的那个神经元效果很不好,那么就会产生连锁反应,导致整个网络过拟合且崩溃。在训练过程中,每个神经元都可能会被dropout掉,所以它就很难产生依赖了。
原文描述如下:

Similarly, each hidden unit in a neural network trained with dropout must learn to work with a randomly chosen sample of other units. This should make each hidden unit more robust and drive it towards creating useful features on its own without relying on other hidden units to correct its mistakes. However, the hidden units within a layer will still learn to do different things from each other. One might imagine that the net would become robust against dropout by making many copies of each hidden unit, but this is a poor solution for exactly the same reason as replica codes are a poor way to deal with a noisy channel.

作者给出的另外一个解释就是,较小的相互依赖的团体在多数情况下比较大的相互依赖的团体效果更好。举个例子,一个有50人的团队,一起工作的效果肯定比只有10个人的团队工作效果好,但前提是你必须将50人管理好,分配好各自的任务,但如果不能管理的好,就会产生比较大的副作用。相对来说,10个团队肯定更容易管理,工作起来更有序一些。

Ten conspiracies each involving five people is probably a better way to create havoc than one big conspiracy that requires fifty people to all play their parts correctly. If conditions do not change and there is plenty of time for rehearsal, a big conspiracy can work well, but with non-stationary conditions, the smaller the conspiracy the greater its chance of still working. Complex co-adaptations can be trained to work well on a training set, but on novel test data they are far more likely to fail than multiple simpler co-adaptations that achieve the same thing

dropout(数学描述)

在这里,我们假设

  1. z l z^l zl是第l层的输入
  2. y l y^l yl是第l层的输出
  3. W l W^l Wl是第l层的权重矩阵
  4. f f f是激活函数
  5. n l n^l nl是第l层的神经元的个数

如果没有dropout的话,那么我们可以用数学公式进行如下描述

在这里插入图片描述
如果加了dropout的话,我们可以用数学公式进行如下描述
在这里插入图片描述
其中Bernouli(p)指的是伯努利分布,p指的就是每个神经元被保留下的概率,也就是说生成参数为p的伯努利分布。其实这里Bernouli§我们可以很轻松的用python代码实现。如下

    d = np.random.rand(p)
    d[d > p] = 0
    d[d > 0] = 1

下面这部分就是为了进行抽样,或者说是dropout其中 ∗ * 指的是对应元素相乘
在这里插入图片描述
假设该隐藏层有6个隐藏单元、p = 0.5,那么
r = [ 1 , 0 , 0 , 1 , 0 , 1 ] r = [1,0,0,1,0,1] r=[1,0,0,1,0,1]
y = [ y 1 , y 2 , y 3 , y 4 , y 5 , y 6 ] y = [y_1,y_2,y_3,y_4,y_5,y_6] y=[y1,y2,y3,y4,y5,y6]
那么
y ∗ r = [ y 1 , 0 , 0 , y 4 , 0 , y 6 ] y * r = [y_1,0,0,y_4,0,y_6] yr=[y1,0,0,y4,0,y6]
上述过程中,我们随机删除掉了3个神经元。

然后剩下的步骤就是进行把 y ˉ \bar{y} yˉ作为输入,计算的输出即为dropout之后的结果了。

下面给出dropout的前向代码(仅供参考,下面代码里还有BatchNormal层)

def forward(X,p,Train=True):
    global  U,V
    cashs = []
    '第一层隐藏层'
    z1 = np.dot(X,p['W1']) + p['B1']
    # 对该层进行BN
    U = np.mean(z1,axis=0) * OMEGA + (1 - OMEGA) * U
    V = np.std(z1,axis=0) * OMEGA + (1 - OMEGA) * V
    z1_nor = (z1 - U) / (V + 1e-15)
    z1_nor_scale = Y1 * z1_nor + C1
    # 计算其输出
    a1 = relu(z1_nor_scale)
    cash = {'z_nor':z1_nor,'z':z1,'a':a1,'a_p':X,'w':p['W1'],'z_p':X}
    cashs.append(cash)
    '第二层隐藏层'
    z2 = np.dot(a1,p['W2']) + p['B2']
    a2 = relu(z2)
    d = np.random.rand(HIDDEN_LAYER_2)
    d[d > KEEP_PROP] = 0
    if Train:
        # 进行dropout 操作
        d[d > 0] = 1
        a2 = ( a2 * d ) / KEEP_PROP
    cash = {'a_p': a1, 'w': p['W2'], 'z_p': z1,'d':d}
    cashs.append(cash)
    '第三层输出层'
    z3 = np.dot(a2,p['W3']) + p['B3']
    y_pred = softmax(z3)
    cash = {'a_p':a2,'w':p['W3'],'z_p':z2}
    cashs.append(cash)
    return y_pred,cashs
训练过程

其实dropout看似挺复杂的,但是其实现和反向传播过程都非常简单。使用了dropout的网络,在进行反向传播的时候,就相当于对当前子模型进行反向传播,也就是说只训练当前子模型使用了的参数,对于没有使用过的参数,我们不进行更新。这里我简单的实现了一下,仅供参考。

# 全连接层的反向传播
def full_backprop(delta,cash,dropout = False,BN = False):
    if dropout:
        d = cash['d']
        delta = (delta * d) / KEEP_PROP
    if BN:
        dY1 = np.mean(delta * cash['z'] / V,axis=0)
        dC1 = np.mean(delta * (Y1 / V),axis=0)
        delta = delta * (Y1 / V)
    dw = np.dot(cash['a_p'].T,delta) / BATCH_SIZE
    db = np.sum(delta,axis=0) / BATCH_SIZE
    delta_pre = np.dot(delta,cash['w'].T) * drelu(cash['z_p'])
    grad_dict = {'dw':dw,'db':db,'delta_pre':delta_pre}
    if BN:
        grad_dict['dY1'] = dY1
        grad_dict['dC1'] = dC1
    return grad_dict

作者也使用了一种可以辅助dropout的方法,即max-norm,该方法让其使用了dropout的网络起来训练更容易,允许学习率设置更大。该方法就是,将每一个神经元的权重矩阵的二范数限制在一定范围内。即
∣ w ∣ 2 < = c |w|_2<=c w2<=c
这个c是我们认为设置的参数。

作者在原论文中是如下描述的

A possible justification is that constraining weight vectors to lie inside a ball of fixed radius makes it possible to use a huge learning rate without the possibility of weights blowing up. The noise provided by dropout then allows the optimization process to explore different regions of the weight space that would have otherwise been difficult to reach. As the learning rate decays, the optimization takes shorter steps, thereby doing less exploration and eventually settles into a minimum.

实验结果
一、Minist

在这里插入图片描述
在这里插入图片描述
上图说明了,dropout在降低test error上,效果是非常好的。

二、SVHN

在这里插入图片描述
在这部分实验中,作者将dropout添加到卷积层中了,并且取得了非常好的效果,误差率从(3.02–>2.47)。这说明,并非只有全连接层中可以使用dropout,虽然说卷积层的参数不多,但使用了dropout也可以取得较好的效果。

三、CIFAR-10 和 CIFAR-100

在这里插入图片描述
通过上述的几种实验我们发现,Goodfellow的Maxout效果非常好,其实这种模型GoodFellow本人也非常推荐将Maxout和Dropout结合一起使用。
想了解Maxout的点击这里

四、与其他正则化方式进行比较

作者使用了5种正则化方式,进行部分组合来进行实验。实验结果如下:
在这里插入图片描述
可以看到Dropout + Max-norm 效果是最好的

salient feature
一、dropout对特征提取的影响

在这里插入图片描述
作者分别对使用了dropout的网络和没有使用dropout的网络隐藏层学习到的特征进行可视化,对比上图可以发现,对于使用了dropout的网络可以更好的学习一些手写数字的特征(边界、形状),而没有使用dropout的网络则产生了比较严重的过拟合。

简言之,dropout可以让模型拥有更好的泛化性能。

二、dropout对稀疏性的影响

在这里插入图片描述
上图说明了,使用了dropout会让隐藏层的激活值更加稀疏

三、dropout rate的选取

在这里插入图片描述
作者通过实验,比较了概率值P对Test Error的影响,结果如上图。可以发现效果比较好的,差不多在0.4-0.8。 这里P指的是神经元被保留下的概率

It becomes flat when 0.4 ≤ p ≤ 0.8 and then increases as p becomes close to 1.

四、数据集大小对dropout的影响

在这里插入图片描述
作者也进行了实现,探索dropout是否适用于各种规模的数据集。上图可以发现,当数据集数量较少的时候,不使用dropout的网络效果反而还好一些。当数据逐渐增多时,dropout的提升效果会越来阅好,但是当数据集大到一定程度时,过拟合问题就不存在了,所以到最后,其实两者效果相差不大。

五、Model Averaging和Weight Scaling

在这里插入图片描述
上文提到过,训练使用dropout的网络,实际上就是训练非常多个更加瘦小的子网络。在最终预测阶段,我们采用的一种简单的方式来综合这些所有子网络的预测结果,即对原始权重乘以p,这是一种简单高效的近似方法,但实际上不是最好的方法。实际上比较正式的方法应该就是对所有模型的预测结果进行加权平均,当然了这种做法是非常昂贵的。

作者做了一个有趣的实验,即从子模型的集合中抽取k个子模型,然后使用比较正式的方法评估这k个子模型的预测结果,该实验目的就是看看,当k取值多少时,这种评估模型的方法会比简单的权重乘以p评估的方法更好。

上图可以看到,当采样出50个模型的时候,其实就可以跟权重乘以p评估出的模型效果一样好了。

附录A

1. 网络大小的选择
假设某一网络,没有使用dropout,其中某一层神经元的个数为n,如果我们想对该层使用Dropout,并且还想保持网络原有的性能,那么可以将该网络的神经元的个数增加到 n p \frac{n}{p} pn

3. 学习率的选择
使用了dropout的网络,通常学习率可以设置的比较大。原论文中说到,一般是普通网络学习率的10-100倍

4. Dropout Rate的选择
上面实验结果就说明了,Dropout rate设置在0.4-0.8之间比较合适。

5. Max-norm上限值的选择
作者推荐一般设置到3-4之间。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReWz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值