Results not reproducible with Keras and TensorFlow in Python

前端 未结 4 1710
春和景丽
春和景丽 2020-12-16 18:27

I have the problem, that I am not able to reproduce my results with Keras and ThensorFlow.

It seems like recently there has been a workaround published on the Keras

4条回答
  •  渐次进展
    2020-12-16 19:08

    My answer is the following, which uses Keras with Tensorflow as backend. Within your nested for loop, where one typically iterates through the various parameters you wish to explore for your model's development, immediately add this function after your last for loop.

    for...
       for...
          reset_keras()
          .
          .
          .
    

    where the reset function is defined as

    def reset_keras():
        sess = tf.keras.backend.get_session()
        tf.keras.backend.clear_session()
        sess.close()
        sess = tf.keras.backend.get_session()
        np.random.seed(1)
        tf.set_random_seed(2)
    

    PS: The function above also actually avoids your nvidia GPU from building up too much memory (which happens after many iteration) so that it eventually becomes very slow...so the function restores GPU performance and maintains results as reproducible.

提交回复
热议问题