首页 > 解决方案 > 无法重现使用 hyperopt 从超参数调整中获得的结果

问题描述

我已经建立了一个 Pytorch 模型并使用库 Hyperopt 执行了超参数调整。尽管我已经在每次运行开始时调用了以下种子函数,但获得的结果是不可重现的:

实用程序.py

def seed_everything(seed=42):
    random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

培训师.py

    def train(self, params):
        util.seed_everything()
        #wrote my training code here

经过进一步检查,我发现 hyperopt 调优的第一个结果始终是可重现的,但随后的运行则不是。这对我来说是出乎意料的,因为我已经在 train 函数的开头调用了 seed_everthing() 。

另外,如果我按以下方式进行培训:

    for i in range(2):
        print ("in iteration ", i)
        trainer = Trainer(**configs)
        trainer.train(params)

迭代 1 和 2 的结果彼此不同,但它们始终相同(即迭代 1 始终给出 1.31714 的 train_loss,而迭代 2 始终给出 4.31235)

我希望迭代 1 和迭代 2 会给出相同的结果,因为它应该是可重现的。

标签: pytorchrandom-seedseedingreproducible-researchhyperopt

解决方案


推荐阅读