0

#Lets say we first load certain models which we use later for calculating errors: 

pre_trained_models = []

for i in range(1,6):

  pre_trained_models.append(keras.models.load_model("model"+str(i))
  
#Now we create another loop where we create a different models for different hyperparameters:

from keras import backend as K
import gc

for i in range(5):
   model = Sequential(....)

   K.clear_session()
   del model
   gc.collect()

Does this clear session also delete the previously loaded pre_trained_models or it only deletes the "model"?

Also how does these clear session, del model, and gc.collect() work in general?

anonymous
  • 3
  • 4

1 Answers1

0

keras.clear_session: will clear all models currently loaded in memory, check here
del model deletes the reference to the given object (model in this case).
gc.collect() calls the garbage collector to remove the objects that are not referenced from memory.

From the description of keras.clear_session you probably don't need to use the other two, since it already removes the models from memory, but I've seem multiple codes using both, probably just to be sure.

This is all tensorflow 1.x, it's recommended to migrate to tensorflow 2.x which relies more on eager execution now, if you want to delete a model, it's like deleting any object.

del model
gc.collect()