目的:提高泛化能力,防止特征检测器的共同适应
参数:
p (float) –置0概率. Default: 0.5
inplace (bool) – If set to True
, 执行‘就地’操作. Default: False
操作:
1.在训练阶段,以概率p随机将input中的元素置0,概率分布满足Bernoulli 分布
2.在训练阶段,其他未被置0的元素乘于一个因子(1/(1-p))
example: 明显可以看出来其余未被置0的元素被乘了一个因子 2 (1/(1-0.5))
import torch.nn as nn
import torch
m = nn.Dropout(p=0.5,inplace=True)
input = torch.randn(3, 5)
print(input)
output = m(input)
print(output)
tensor([[ 0.2398, -1.6088, -0.9147, -0.7416, 0.8053],
[ 1.6897, 1.4464, 1.4659, 1.9410, -0.6452],
[-1.3300, -0.5658, -0.3383, 0.4237, 0.1850]])tensor([[ 0.0000, -3.2176, -1.8293, -1.4831, 0.0000],
[ 0.0000, 2.8927, 0.0000, 0.0000, -0.0000],
[-2.6599, -0.0000, -0.6767, 0.8474, 0.3701]])