其他分享
首页 > 其他分享> > 随机种子、torch.backends.cudnn.benchmark\.deterministic

随机种子、torch.backends.cudnn.benchmark\.deterministic

作者:互联网

为什么使用相同的网络结构,跑出来的效果完全不同,用的学习率,迭代次数,batch size 都是一样?
固定随机数种子是非常重要的。
但是如果你使用的是PyTorch等框架,还要看一下框架的种子是否固定了。还有,如果你用了cuda,别忘了cuda的随机数种子。这里还需要用到torch.backends.cudnn.deterministic和torch.backends.cudnn.benchmark确保调度的卷积算法一致

一、torch.backends.cudnn.deterministic是啥?

torch.backends.cudnn.deterministic=True每次返回的卷积算法将是确定的,即默认算法
如果配合上设置 Torch 的随机种子为固定值的话,应该可以保证每次运行网络的时候相同输入的输出是固定的,代码大致这样

def init_seeds(seed=0):
    torch.manual_seed(seed) # sets the seed for generating random numbers.
    torch.cuda.manual_seed(seed) # Sets the seed for generating random numbers for the current GPU. It’s safe to call this function if CUDA is not available; in that case, it is silently ignored.
    torch.cuda.manual_seed_all(seed) # Sets the seed for generating random numbers on all GPUs. It’s safe to call this function if CUDA is not available; in that case, it is silently ignored.

    if seed == 0:
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False
编辑于 2020-05-15

二、torch.backends.cudnn.benchmark是啥?

如果设置为True,可能无法保证结果可以复现。
torch.backends.cudnn.benchmark=True 将会让程序在开始时花费一点额外时间,为整个网络的每个卷积层搜索最适合它的卷积实现算法,进而实现网络的加速。适用场景是网络结构固定(不是动态变化的),网络的输入形状(包括 batch size,图片大小,输入的通道)是不变的,其实也就是一般情况下都比较适用。反之,如果卷积层的设置一直变化,将会导致程序不停地做优化,反而会耗费更多的时间。

三、随机种子

myseed = 20211024  # set a random seed for reproducibility
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(myseed)
torch.manual_seed(myseed)
if torch.cuda.is_available():
    torch.cuda.manual_seed_all(myseed)

标签:torch,backends,deterministic,benchmark,cudnn,seed,cuda
来源: https://blog.csdn.net/M_arshal_/article/details/120932479