[PYTHON] Guarantee of reproducibility with keras (as of September 22, 2020)

Introduction

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

Guarantee of reproducibility

Setting environment variables

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

Fixed library seed value

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 ...

Finally

Make sure the documentation is updated to the latest version. (Commandment)

Reference page

Keras FAQ: How can I obtain reproducible results using Keras during development? (Viewed on 2020/09/22)

Recommended Posts

Guarantee of reproducibility with keras (as of September 22, 2020)
Keras as wrapper of Theano & TensorFlow
Prediction of sine wave with keras
4/22 prediction of sine wave with keras
Face recognition of anime characters with Keras
Record of the first machine learning challenge with Keras
Summary of Tensorflow / Keras
Image recognition with keras
CIFAR-10 tutorial with Keras
Multivariate LSTM with Keras
As a result of mounting and tuning with POH! Lite
Using Docker (Hyper-V) with PyCharm on Windows 10 (as of August 2017)
How to register a package on PyPI (as of September 2017)
I tried handwriting recognition of runes with CNN using Keras