【PytorchLearning】Dropout | 内部机制及代码复现

Dropout

1.CLASS torch.nn.Dropout(p=0.5, inplace=False)

训练过程中按照概率p随机地将输入张量中的元素置为0

evere channel will be zeroed out independently on every forward call.

Parameters:

  • p(float):每个元素置为0的概率,默认是0.5
  • inplace(bool):是否对原始张量进行替换

Shape

  • intput(*):any shape
  • out(*):the same shape as input

Examples

m = nn.Dropout(p=0.2)
input = torch.randn(20, 16)
output = m(input)

2.TORCH.NN.FUNCTIONAL.DROPOUT

torch.nn.functional.dropout(input, p=0.5, training=True, inplace=False),内部细节与Dropout相同

Parameters:

  • p (float) – probability of an element to be zeroed. Default: 0.5
  • training (bool) – apply dropout if is True. Default: True
  • inplace (bool) – If set to True, will do this operation in-place. Default: False

Retrun Type:

Tensor

3.Question

  1. Dropout中没有training这个参数,那么他怎么区分train和test?
  2. 为什么Dropout在训练的时候的推理的时候运算逻辑不一样?
  3. pytorch内部是如何实现Dropout的(训练、推理)?
  4. Dropout在训练和推理过程中有较大的区别,那么如何去改进?
  5. 为什么要用Dropout,Dropout在网络中的直观影像是什么?

1.Dropout继承自torch.nn.module,torch.nn.module内置的成员变量就包含training选项,其默认值为True。当我们训练模型时,不用指定就内置为True;当推理模型时,model.eval()会自动将training设置为False,从而不采用dropout进行推理(也不采用BN)

2.因为dropout是带有随机性的,如果 infer 也做的话,网络的输出就不稳定(同样一个样本,整体预测结果每次都可能变化)

3.主要是用c实现的,包括二项伯努利分布、mask操作

4.使用Inverted Dropout的方式进行改进,只在训练过程中对数据分布进行改动,即先dropout再rescale,保证总期望不变

5 使用dropout相当于在引入多个不同的模型,可以使网络具有更好的泛化性能从而避免过拟合

Furthermore, the outputs are scaled by a factor of
f r e s c a l e = 1 1 − p f_{rescale}=\frac {1}{1-p} frescale=1p1
during training. This means that during evaluation the module simply computes an identity function.

首先,dropout是带有随机性的,如果 infer 也做的话,网络的输出就不稳定(同样一个样本,整体预测结果每次都可能变化)。在 infer 不做 dropout 的前提下,为了保证训练和预测过程的分布一致,需要对 infer 进行 rescale,也就是原始论文中将infer数据进行1-p倍缩小的做法,这种方式会导致预测过程依赖训练过程,模型推理的变动较大;于是Inverted Dropout提出只在训练过程中对数据分布进行修改,即先遮盖掉p的节点,然后再放大为1/(1-p)倍,这样在infer的过程中就不必对数据进行变动。即训练过程中随机扔掉了一些节点,但是rescale之后总期望又被拉回到了原来的水平。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HdMVGnbM-1667478261933)(C:\Users\marlowe\Desktop\paperReading\神经网络基础知识\IMG\2-1.png)]

numpy实现

1.vanilla dropout

imoport numpy as np

def van_train(reate,x,w1,b1,w2,b2):
    
    layer1=np.maxinum(0,np.dot(w1,x)+b1)
    mask1=np.random.binomial(1,1-rate,layer1.shape)# random.binomial(n, p, size=None)
    layer1=layer1*mask1
    
    layer2=np.maxinum(0,np.dot(w2,layer1)+b2)
    mask2=np.random.binomial(1,1-rate,layer2.shape)# random.binomial(n, p, size=None)
    layer2=layer2*mask2
    
    return layer2


def van_test(rate,x,w1,b1,w2,b2):
    layer1=np.maxinum(0,np.dot(w1,x)+b1)
    layer1=layer1*(1-rate)
    
    layer2=np.maximun(0,np.dot(w2,layer1)+b2)
    layer2=layer2*(1-rate)
    
    return layer2

2.inverted dropout

import numpy as np

def inv_train(rate,x,w1,b1,w2,b2):
    
    layer1=np.maxinum(0,np.dot(w1,x)+b1)
    mask1=np.random.binomial(1,1-rate,layer1.shape)# random.binomial(n, p, size=None)
    layer1=layer1*mask1
    layer1/=1-rate
    
    layer2=np.maxinum(0,np.dot(w2,layer1)+b2)
    mask2=np.random.binomial(1,1-rate,layer2.shape)# random.binomial(n, p, size=None)
    layer2=layer2*mask2
    layer2/=1-rate
    
    return layer2


def inv_test(x,w1,b1,w2,b2):# 不需要使用rate进行缩放
    layer1=np.maxinum(0,np.dot(w1,x)+b1)
    
    layer2=np.maximun(0,np.dot(w2,layer1)+b2)
    
    return layer2
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Marlowee

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

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

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

打赏作者

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

抵扣说明:

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

余额充值