简体   繁体   中英

Tensorflow (v1.12.0) - what is the difference between keras.backend.clear_session() and tf.keras.backend.clear_session()?

Using TensorFlow (version 1.12.0) and Keras (version 2.2.4) on a GPU cluster, I trained 10 simple and identical classifiers in a loop. I encountered unexpectedly wide variation in performance. After some troubleshooting, I decided to look into the way I was clearing the Keras session between models. I found that

import tensorflow as tf
import keras.backend as K

from keras import Sequential
from keras.layers import Lambda, Dense, Flatten

for i in range(10):
    K.clear_session()
    # train models

did not solve my problem. When I switched to

import tensorflow as tf
import keras.backend as K

from keras import Sequential
from keras.layers import Lambda, Dense, Flatten

for i in range(10):
    tf.keras.backend.clear_session()
    # train models

the problem went away. All of my models are built on objects from keras , so I would have thought that having keras clear the session would work, but evidently it didn't.

What is the difference between K.clear_session() and tf.keras.backend.clear_session() in this case? Why did the first not seem to have much effect, while the second brought my classifiers closer to their expected performance?

The difference is that TF v1.12 was released on Nov 5, 2018 whereas Keras v2.2.4 was released on Oct 03, 2018 which means Keras v2.2.4 uses TF v1.11 as backend.

Take a look at Tensorflow release history here and the Keras release history here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM