6.4节的代码复现
#《python深度学习实战——————神经网络基础》中dropput段 代码复现
from torch.distributions import Bernoulli
from torch import nn
import torch
activations=torch.rand((5,5))#随机生成一个五乘五的矩阵
m=Bernoulli(0.5)#调用伯努利函数
mask=m.sample(activations.shape)#tensor.shape 查看维度
activations*=mask
#调用nn库自带的dropout函数
p,count,iters,shape=0.5,0.,50,(5,5)#伯努利参数:初始化count用来计数:循环次数:矩阵形状
dropput=nn.Dropout(p)
dropput.train()
for _ in range(iters):
activations=torch.rand(shape)+1e-5#猜测为干扰 去掉后结果影响不大
output=dropput(activations)
count+=torch.sum(output==activations*(1/(1-p)))
print("train模式Dropout影响了{}的神经元".format(1-float(count)/(activations.nelement()*iters)))
count =0
dropput.eval()
for _ in range(iters):
activations=torch.rand(shape)+1e-5
output=dropput(activations)
count+=torch.sum(output==activations)
print("eval模式Dropout影响了{}的神经元".format(1-float(count)/(activations.nelement()*iters)))