I'm training a series of models in a for loop - to test a certain architecture. While doing so, I run out of memory and the system shuts down the process.
The same problem appears in this question and this question. To try their solutions, I did a test run with a similar loop to the one that is giving me problems. The code is:
def mem_test(n):
train_data = np.random.rand(1000,1500)
train_labels = np.random.randint(2,size= 1000)
mem = []
for i in range(n):
model = keras.Sequential([keras.layers.Dense(1000, activation= tf.nn.relu),
keras.layers.Dense(2,activation = tf.nn.softmax)])
model.compile(optimizer= tf.train.AdamOptimizer(.001), loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
model.fit(train_data,train_labels, epochs = 1)
mem.append(psutil.virtual_memory())
return mem
def mem_test_clear(n):
train_data = np.random.rand(1000,1500)
train_labels = np.random.randint(2,size= 1000)
mem = []
for i in range(n):
model = keras.Sequential([keras.layers.Dense(1000, activation= tf.nn.relu),
keras.layers.Dense(2,activation = tf.nn.softmax)])
model.compile(optimizer= tf.train.AdamOptimizer(.001), loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
model.fit(train_data,train_labels, epochs = 1)
mem.append(psutil.virtual_memory())
keras.backend.clear_session()
tf.reset_default_graph()
return mem
while the latter seems to do slightly better than the former, they both still end up accumulating memory usage. So, for my actual application of this, I'm left without a solution. What do I need to do in order to actually free up memory in this situation? What am I doing wrong?