【Pytorch】torch.nn.Dropout()

本文介绍了PyTorch中的Dropout模块,它用于神经网络的正则化,防止过度拟合。Dropout以给定的概率p随机将输入张量的部分元素置零。在训练期间,它能独立地对每个通道进行操作,而在评估时则不改变输入。通过实例展示了如何使用Dropout,并解释了inplace参数的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

简介

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 .

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

举例

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)

在这里插入图片描述

参考

  • https://pytorch.org/docs/stable/generated/torch.nn.Dropout.html#torch.nn.Dropout

结语

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

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

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

海轰Pro

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

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

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

打赏作者

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

抵扣说明:

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

余额充值