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)