Memory leak with TensorFlow

I have a memory leak with TensorFlow. I referred to Tensorflow: memory leak even when closing a session? to solve my problem, and I followed the advice of an answer that seemed to solve the problem. However, this does not work here.

To recreate a memory leak, I created a simple example. Firstly, I use this function (which I got here: How to find out the current processor and RAM usage in Python? ) To check the memory usage of the python process:

def memory():
    import os
    import psutil
    pid = os.getpid()
    py = psutil.Process(pid)
    memoryUse = py.memory_info()[0]/2.**30  # memory use in GB...I think
    print('memory use:', memoryUse)

Then, every time I build_modelfunction build_model, memory usage increases.

Here is build_modela memory leak function :

def build_model():

    '''Model'''

    tf.reset_default_graph()


    with tf.Graph().as_default(), tf.Session() as sess:
        tf.contrib.keras.backend.set_session(sess)

        labels = tf.placeholder(tf.float32, shape=(None, 1))
        input = tf.placeholder(tf.float32, shape=(None, 1))

        x = tf.contrib.keras.layers.Dense(30, activation='relu', name='dense1')(input)
        x1 = tf.contrib.keras.layers.Dropout(0.5)(x)
        x2 = tf.contrib.keras.layers.Dense(30, activation='relu', name='dense2')(x1)
        y = tf.contrib.keras.layers.Dense(1, activation='sigmoid', name='dense3')(x2)


        loss = tf.reduce_mean(tf.contrib.keras.losses.binary_crossentropy(labels, y))

        train_step = tf.train.AdamOptimizer(0.004).minimize(loss)

        #Initialize all variables
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        sess.close()

    tf.reset_default_graph()

    return 

, with tf.Graph().as_default(), tf.Session() as sess: tf.reset_default_graph , TensorFlow. .

:

memory()
build_model()
memory()
build_model()
memory()

( ):

memory use: 0.1794891357421875
memory use: 0.184417724609375
memory use: 0.18923568725585938

, , TensorFlow, . ?

100 build_model, :

Memory use over 100 iterations

, , .

+11
3

Tensorflow 0.11. Tensorflow 0.12, . , . tf.contrib.keras.backend.clear_session() tf.contrib.keras.backend.clear_session().

+2

, , . , , init_op = tf.global_variables_initializer(). , , . , .

,

, . , , . . , . , - - . , , .

0

- TF 1.12. . , , , , . , , .

Tensorflow

  • , , , . .
  • , REPL , . , , - , - .

, .

0

All Articles