如何在多次运行中重现 RNN 结果?
How to reproduce RNN results on several runs?
我连续两次对相同的输入调用相同的模型,但没有得到相同的结果,这个模型有 nn.GRU
层,所以我怀疑它有一些内部状态应该在第二次之前释放运行?
如何重置 RNN 隐藏状态以使其与模型最初加载时相同?
更新:
一些上下文:
我正在尝试从这里 运行 建模:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L93
我打电话给 generate
:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L148
这里实际上有一些在pytorch中使用随机生成器的代码:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L200
https://github.com/erogol/WaveRNN/blob/master/utils/distribution.py#L110
https://github.com/erogol/WaveRNN/blob/master/utils/distribution.py#L129
我已经放置(我在 CPU 上 运行ning 代码):
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(0)
在
https://github.com/erogol/WaveRNN/blob/master/utils/distribution.py
全部导入之后。
我检查了 运行 之间的 GRU 权重,它们是相同的:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L153
我还检查了 运行 和 logits
之间的 logits
和 sample
是相同的,但 sample
不是,所以@Andrew Naguib 似乎关于随机播种是正确的,但我不确定修复随机种子的代码应该放在哪里?
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L200
更新 2:
我在 generate
中放置了种子初始化,现在结果是一致的:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L148
我相信这可能与 Random Seeding. To ensure reproducible results (as stated by them 高度相关)你必须播种 torch
,如下所示:
import torch
torch.manual_seed(0)
还有 CuDNN
模块。
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
如果您使用的是 numpy
,您还可以:
import numpy as np
np.random.seed(0)
但是,他们警告您:
Deterministic mode can have a performance impact, depending on your model.
我经常使用的推荐脚本是:
# imports
import numpy as np
import random
import torch
# ...
""" Set Random Seed """
if args.random_seed is not None:
"""Following seeding lines of code are to ensure reproducible results
Seeding the two pseudorandom number generators involved in PyTorch"""
random.seed(args.random_seed)
np.random.seed(args.random_seed)
torch.manual_seed(args.random_seed)
# https://pytorch.org/docs/master/notes/randomness.html#cudnn
if not args.cpu_only:
torch.cuda.manual_seed(args.random_seed)
cudnn.deterministic = True
cudnn.benchmark = False
您可以使用model.init_hidden()
重置RNN隐藏状态。
def init_hidden(self):
# Initialize hidden and cell states
return Variable(torch.zeros(num_layers, batch_size, hidden_size))
因此,在下次对相同数据调用相同模型之前,您可以调用 model.init_hidden() 将隐藏状态和单元状态重置为初始值。
这将清除历史记录,换句话说,模型在 运行 之后首次对数据学习的权重。
我连续两次对相同的输入调用相同的模型,但没有得到相同的结果,这个模型有 nn.GRU
层,所以我怀疑它有一些内部状态应该在第二次之前释放运行?
如何重置 RNN 隐藏状态以使其与模型最初加载时相同?
更新:
一些上下文:
我正在尝试从这里 运行 建模:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L93
我打电话给 generate
:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L148
这里实际上有一些在pytorch中使用随机生成器的代码:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L200
https://github.com/erogol/WaveRNN/blob/master/utils/distribution.py#L110
https://github.com/erogol/WaveRNN/blob/master/utils/distribution.py#L129
我已经放置(我在 CPU 上 运行ning 代码):
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(0)
在
https://github.com/erogol/WaveRNN/blob/master/utils/distribution.py
全部导入之后。
我检查了 运行 之间的 GRU 权重,它们是相同的:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L153
我还检查了 运行 和 logits
之间的 logits
和 sample
是相同的,但 sample
不是,所以@Andrew Naguib 似乎关于随机播种是正确的,但我不确定修复随机种子的代码应该放在哪里?
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L200
更新 2:
我在 generate
中放置了种子初始化,现在结果是一致的:
https://github.com/erogol/WaveRNN/blob/master/models/wavernn.py#L148
我相信这可能与 Random Seeding. To ensure reproducible results (as stated by them 高度相关)你必须播种 torch
,如下所示:
import torch
torch.manual_seed(0)
还有 CuDNN
模块。
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
如果您使用的是 numpy
,您还可以:
import numpy as np
np.random.seed(0)
但是,他们警告您:
Deterministic mode can have a performance impact, depending on your model.
我经常使用的推荐脚本是:
# imports
import numpy as np
import random
import torch
# ...
""" Set Random Seed """
if args.random_seed is not None:
"""Following seeding lines of code are to ensure reproducible results
Seeding the two pseudorandom number generators involved in PyTorch"""
random.seed(args.random_seed)
np.random.seed(args.random_seed)
torch.manual_seed(args.random_seed)
# https://pytorch.org/docs/master/notes/randomness.html#cudnn
if not args.cpu_only:
torch.cuda.manual_seed(args.random_seed)
cudnn.deterministic = True
cudnn.benchmark = False
您可以使用model.init_hidden()
重置RNN隐藏状态。
def init_hidden(self):
# Initialize hidden and cell states
return Variable(torch.zeros(num_layers, batch_size, hidden_size))
因此,在下次对相同数据调用相同模型之前,您可以调用 model.init_hidden() 将隐藏状态和单元状态重置为初始值。
这将清除历史记录,换句话说,模型在 运行 之后首次对数据学习的权重。