当前位置:网站首页>Pytorch set random seed

Pytorch set random seed

2022-06-09 17:55:00 Brother prawn flying

pytorch Set random seeds - Ensure that all training processes of the model are reproduced


In the use of PyTorch when , If you want to seed random numbers , stay GPU or CPU Fix each training result on the , You need to add the following code at the beginning of program execution :

def seed_everything():
    '''  Set the overall development environment seed :param seed: :param device: :return: '''
    import os
    import random
    import numpy as np

    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    
    # some cudnn methods can be random even after fixing the seed
    # unless you tell it to be deterministic
    torch.backends.cudnn.deterministic = True

原网站

版权声明
本文为[Brother prawn flying]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091742470103.html