reproducible.py 1.69 KB
Newer Older
1
2
3
4
import os
import numpy as np
import tensorflow as tf
import random as rn
5
# from tensorflow.contrib import keras
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# reproducible networks
# The below is necessary in Python 3.2.3 onwards to
# have reproducible behavior for certain hash-based operations.
# See these references for further details:
# https://docs.python.org/3.4/using/cmdline.html#envvar-PYTHONHASHSEED
# https://github.com/fchollet/keras/issues/2280#issuecomment-306959926
os.environ['PYTHONHASHSEED'] = '0'

# The below is necessary for starting Numpy generated random numbers
# in a well-defined initial state.
np.random.seed(42)

# The below is necessary for starting core Python generated random numbers
# in a well-defined state.
rn.seed(12345)

# Force TensorFlow to use single thread.
# Multiple threads are a potential source of
# non-reproducible results.
# For further details, see:
# https://stackoverflow.com/questions/42022950/which-seeds-have-to-be-set-where-to-realize-100-reproducibility-of-training-res
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
28
session_config = tf.ConfigProto(intra_op_parallelism_threads=1,
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
29
30
                                inter_op_parallelism_threads=1,
                                log_device_placement=True)
31
32
33
34
35

# The below tf.set_random_seed() will make random number generation
# in the TensorFlow backend have a well-defined initial state.
# For further details, see:
# https://www.tensorflow.org/api_docs/python/tf/set_random_seed
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
36
37
38
tf_random_seed = 1234
tf.set_random_seed(tf_random_seed)
# sess = tf.Session(graph=tf.get_default_graph(), config=session_config)
39
# keras.backend.set_session(sess)
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
40
41
42
43

run_config = tf.estimator.RunConfig()
run_config = run_config.replace(session_config=session_config)
run_config = run_config.replace(tf_random_seed=tf_random_seed)