When reproducing learning with keras, the information that comes out first when searching and the official Japanese document is the old version method, and now the method is changed, so record it.
version
Since python3.2.3, the reproducibility of python hash-based operations can be guaranteed by fixing the value of PYTHONHASHSEED.
export PYTHONHASHSEED=0
Also, PYTHONHASHSEED must be set before the program is executed, not in the code.
$ python -c 'import os;os.environ["PYTHONHASHSEED"]="0";print(hash("keras"))'
2834998937574676049
$ python -c 'import os;os.environ["PYTHONHASHSEED"]="0";print(hash("keras"))'
-1138434705774533911
$ PYTHONHASHSEED=0 python -c 'print(hash("keras"))'
4883664951434749476
$ PYTHONHASHSEED=0 python -c 'print(hash("keras"))'
4883664951434749476
Other articles say that it could be reproduced without setting environment variables, so it may not be necessary. https://sanshonoki.hatenablog.com/entry/2019/01/15/230054
Excerpt from the official Kersa documentation page. The code around here has changed from the previous version.
import numpy as np
import tensorflow as tf
import random as python_random
# The below is necessary for starting Numpy generated random numbers
# in a well-defined initial state.
np.random.seed(123)
# The below is necessary for starting core Python generated random numbers
# in a well-defined state.
python_random.seed(123)
# The below set_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/random/set_seed
tf.random.set_seed(1234)
# Rest of code follows ...
Make sure the documentation is updated to the latest version. (Commandment)
Keras FAQ: How can I obtain reproducible results using Keras during development? (Viewed on 2020/09/22)
Recommended Posts