使用 Keras 的机器学习项目中随机性的常见来源是什么?

What are common sources of randomness in Machine Learning projects with Keras?

再现性很重要。在我目前正在做的一个闭源机器学习项目中,很难实现。看什么部位?

播种

计算机具有伪随机数生成器,这些生成器使用称为种子的值进行初始化。对于机器学习,您可能需要执行以下操作:

# I've heard the order here is important
import random
random.seed(0)

import numpy as np
np.random.seed(0)

import tensorflow as tf
tf.set_random_seed(0)
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
                              inter_op_parallelism_threads=1)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)

from keras import backend as K
K.set_session(sess)  # tell keras about the seeded session

# now import keras stuff

另请参阅:Keras FAQ: How can I obtain reproducible results using Keras during development?

sklearn

sklearn.model_selection.train_test_split 有一个 random_state 参数。

检查什么

  1. 我每次加载数据的顺序都是一样的吗?
  2. 我是否以相同的方式初始化模型?
  3. 您是否使用可能会更改的外部数据?
  4. 您是否使用可能会改变的外部状态(例如 datetime.now)?