繁体   English   中英

PyTorch 非确定性辍学

[英]PyTorch non-deterministic dropout

我正在尝试使 BLSTM 的 output 具有确定性,经过调查,似乎我的 dropout 层创建了不确定的 dropout 掩码,因此我正在研究如何修复pytorch中的随机种子。 尽管我将所有内容都放入了代码中,但我发现了此页面和其他建议,但没有帮助。 这是我的代码:

import sys 
import random 
import datetime as dt 

import numpy as np 
import torch 

torch.manual_seed(42) 
torch.cuda.manual_seed(42) 
np.random.seed(42)
random.seed(42) 
torch.backends.cudnn.deterministic = True  

ex = torch.ones(10) 
torch.nn.functional.dropout(ex, p=0.5, training=True)                  
# Out[29]: tensor([0., 0., 2., 0., 0., 0., 0., 0., 2., 2.])

torch.nn.functional.dropout(ex, p=0.5, training=True)                  
# Out[30]: tensor([0., 2., 0., 2., 2., 0., 0., 2., 2., 2.])

请帮助我从相同输入的辍学中获得确定性 output

每次您想要相同的 output 时,您都需要再次重置随机种子,因此:

>>> import torch

>>> torch.manual_seed(42)
<torch._C.Generator object at 0x127cd9170>

>>> ex = torch.ones(10)

>>> torch.nn.functional.dropout(ex, p=0.5, training=True)
tensor([0., 0., 2., 2., 2., 2., 2., 0., 2., 0.])

>>> torch.manual_seed(42)
<torch._C.Generator object at 0x127cd9170>

>>> torch.nn.functional.dropout(ex, p=0.5, training=True)
tensor([0., 0., 2., 2., 2., 2., 2., 0., 2., 0.])

不过,您可能希望继续重置所有这些随机种子,但在 python 中构建神经网络时,您会发现很多不同的随机性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM