【发布时间】:2020-09-17 16:31:51
【问题描述】:
现在我正在使用torch.distributed 训练模型,但我不确定如何设置随机种子。例如,这是我当前的代码:
def main():
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
cudnn.enabled = True
cudnn.benchmark = True
cudnn.deterministic = True
mp.spawn(main_worker, nprocs=args.ngpus, args=(args,))
我应该移动
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
cudnn.enabled = True
cudnn.benchmark = True
cudnn.deterministic = True
进入函数main_worker() 以确保每个进程都有正确的种子和cudnn 设置?顺便说一句,我试过这个,这种行为会使训练慢 2 倍,这让我很困惑。
非常感谢您的帮助!
【问题讨论】:
-
使用
torch.cuda.manual_seed_all()而不是torch.cuda.manual_seed。 -
感谢您的评论!但是我可以在每个进程中使用 torch.cuda.manual_seed() 吗?(每个进程将使用单个 GPU)
标签: python parallel-processing pytorch distributed python-parallel