0

I want to create a population of different models and update the population every generation. This means I have to create a new list of models every generation. Every time the list of models is cleared, the memory appears to stay and will accumulate every generation until it consumes all the memory.

This is my simplified code:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM

class population:
    def __init__(self):
        self.pop = []

    def createModel(self, inputShape):
        model = Sequential([
                LSTM(64, input_shape=(inputShape), return_sequences=True),
                LSTM(64, input_shape=(inputShape)),
                Dense(32, activation="relu"),
                Dense(2, activation="softmax"),
        ])
        return model

    def createList(self, numModels):
        model_list = []
        for _ in range(numModels):
            model = self.createModel((5,5))
            model_list.append(model)
        
        return model_list

    def updateList(self, iterations):
        for i in range(iterations):
            print(f"Generation: {i+1}")
            self.pop.clear()
            self.pop = self.createList(50)

pop = population()
pop.updateList(10)
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143

1 Answers1

0

You may want to use tf.keras.backend.clear_session:

Keras manages a global state, which it uses to implement the Functional model-building API and to uniquify autogenerated layer names.

If you are creating many models in a loop, this global state will consume an increasing amount of memory over time, and you may want to clear it. Calling clear_session() releases the global state: this helps avoid clutter from old models and layers, especially when memory is limited.

You can also read this answer.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143