0
点赞
收藏
分享

微信扫一扫

github和Visual Studio

双井暮色 2024-11-09 阅读 13
import os
import torch
import numpy as np
import random

os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8"
torch.use_deterministic_algorithms(True)



def set_seed(seed_value=42):

    print(seed_value)

    random.seed(seed_value)

    np.random.seed(seed_value)

    torch.manual_seed(seed_value)

    torch.cuda.manual_seed(seed_value)

    torch.cuda.manual_seed_all(seed_value)

    torch.backends.cudnn.deterministic = True

    torch.backends.cudnn.benchmark = False



试了n多次,每次即使设置了随机种子还是会有不一致的结果。感觉可能是因为模型包含写随机操作,使用torch.backends.cudnn.deterministic = True 好像就能解决这个问题,目前影响还没发现

举报

相关推荐

0 条评论