torch.nn.Dropout()细节记录

说明: 

1. 在model.train()模式下Dropout层是有效的,在model.eval()模式下,Dropout层是无效的。

2. torch.nn.Dropout是采用的inverted 版本,为了“保持期望不变”而进行了rescale(缩放),缩放比例是1/(1-p),p是的丢弃比率。

Furthermore, the outputs are scaled by a factor of :math:`\frac{1}{1-p}` during
training. This means that during evaluation the module simply computes an
identity function.
#-*- coding:utf-8 -*-
#Author LJB Create on 2021/8/25
import torch.nn as nn
import torch
class Test(nn.Module):
    def __init__(self):
        super(Test,self).__init__()
        self.droprate=0.8
        self.dropout = nn.Dropout(self.droprate)

    def forward(self,x):
        o = self.dropout(x)
        print('++before dropout:',x/(1-self.droprate))
        print('---after dropout:',o)

T = Test()

data = torch.Tensor([1,2,3,4,5,6,7,8,9,10])
T.train()
for i in range(5):
    T(data)

print('#'*40)
T.eval()
for i in range(5):
    T(data)

运行结果:

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值