采用GPU训练神经网络时,由于并行计算的多线程,加之GPU的单精度,导致应用GPU计算时的结果并不能可重复性(即使控制了tf.random.set_seed)。需要在控制随机数种子的基础上,进一步控制并行计算的线程数。具体代码如下:
SEED = 24
def set_seeds(seed=SEED):
os.environ['PYTHONHASHSEED'] = str(seed)
random.seed(seed)
tf.random.set_seed(seed)
np.random.seed(seed)
def set_global_determinism(seed=SEED):
set_seeds(seed=seed)
os.environ['TF_DETERMINISTIC_OPS'] = '1'
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
tf.config.threading.set_inter_op_parallelism_threads(1)
tf.config.threading.set_intra_op_parallelism_threads(1)
set_global_determinism(seed=SEED)