【Pytorch】torch.nn.Dropout()

113 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第4天,点击查看活动详情

在这里插入图片描述

简介

Hello!

非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~  

ଘ(੭ˊᵕˋ)੭

昵称:海轰

标签:程序猿|C++选手|学生

简介:因C语言结识编程,随后转入计算机专业,获得过国家奖学金,有幸在竞赛中拿过一些国奖、省奖...已保研

学习经验:扎实基础 + 多做笔记 + 多敲代码 + 多思考 + 学好英语!

唯有努力💪

torch.nn.Dropout()

语法

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

  • p:元素归零的概率。 默认值:0.5
  • inplace : 如果设置为 True,将就地执行此操作。 默认值:False

个人理解:以概率p进行归零

作用

在训练期间,使用来自伯努利分布的样本以概率 p 将输入张量的一些元素随机归零,每个通道将在每次前转呼叫时独立归零

During training, randomly zeroes some of the elements of the input tensor with probability p using samples from a Bernoulli distribution. Each channel will be zeroed out independently on every forward call.

这已被证明是一种用于正则化和防止神经元共同适应的有效技术,如通过防止特征检测器的共同适应改善神经网络一文中所述

This has proven to be an effective technique for regularization and preventing the co-adaptation of neurons as described in the paper Improving neural networks by preventing co-adaptation of feature detectors .

此外,在训练中输出的比例为 11p\frac{1}{1-p} 。 这意味着在评估期间,模块只计算一个恒等函数。

举例

m = nn.Dropout(p=0.2)
input = torch.randn(3, 4)
output = m(input)

print('input : \n', input)
print('output : \n', output)

在这里插入图片描述

inplace=True时

m = nn.Dropout(p=0.2, inplace=True)
input = torch.randn(3, 4)
output = m(input)

print('input : \n', input)
print('output : \n', output)

在这里插入图片描述

参考

结语

文章仅作为个人学习笔记记录,记录从0到1的一个过程

希望对您有一点点帮助,如有错误欢迎小伙伴指正

在这里插入图片描述