在使用PyTorch框架进行训练时,若需要设置随机数种子,在GPU或CPU上固定每次的训练结果,需在程序执行的开始处添加以下代码。随机数种子seed确定时,模型的训练结果将始终保持一致。
当前对aicpu的卷积和卷积算法等算子的随机固定使用为:
torch.use_deterministic_algorithms(True)
验证是否设置成功可参考以下方法。
torch.are_deterministic_algorithms_enabled()
验证示例:
print(torch.are_deterministic_algorithms_enabled())
执行训练时,打印此接口的返回值为True表示当前已开启确定性计算开关,返回False则表示未开启。
aclSetCompileopt: Set compile option [ge.deterministic] and value [1]