TensorFlow的内存泄漏 [英] Memory leak with TensorFlow

查看:207
本文介绍了TensorFlow的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的TensorFlow发生内存泄漏.我参考了 Tensorflow:即使关闭Session时也会发生内存泄漏?来解决我的问题,我听了答案的建议,似乎已经解决了问题.但是,它在这里不起作用.

I have a memory leak with TensorFlow. I refered to Tensorflow : Memory leak even while closing Session? to address my issue, and I followed the advices of the answer, that seemed to have solved the problem. However it does not work here.

为了重新创建内存泄漏,我创建了一个简单的示例.首先,我使用此函数(到达这里:如何在Python中获取当前的CPU和RAM使用率?)检查python进程的内存使用情况:

In order to recreate the memory leak, I have created a simple example. First, I use this function (that I got here : How to get current CPU and RAM usage in Python?) to check the memory use 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)

然后,每次我调用build_model函数时,对内存的使用都会增加.

Then, everytime I call the build_model function, the use of memory increases.

这是build_model函数,该函数存在内存泄漏:

Here is the build_model function that has a memory leak :

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占用的所有内存.显然不是.

I would have thought that using the block with tf.Graph().as_default(), tf.Session() as sess: and then closing the session and calling tf.reset_default_graph would clear all the memory used by TensorFlow. Apparently it does not.

可以如下重新创建内存泄漏:

The memory leak can be recreated as following :

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

此输出为(对于我的计算机):

The output of this is (for my computer) :

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

很明显,我们可以看到TensorFlow使用的所有内存之后都没有释放.为什么?

Clearly we can see that all the memory used by TensorFlow is not freed afterwards. Why?

我绘制了调用build_model的100次迭代中的内存使用情况,这就是我得到的:

I plotted the use of memory over 100 iterations of calling build_model, and this is what I get :

我认为这表明存在内存泄漏.

I think that goes to show that there is a memory leak.

推荐答案

该问题归因于Tensorflow版本0.11.到今天为止,Tensorflow 0.12已经发布,并且该错误已解决.升级到较新的版本,它应该可以按预期的方式工作.不要忘了最后打tf.contrib.keras.backend.clear_session().

The problem was due to Tensorflow version 0.11. As of today Tensorflow 0.12 is out and the bug is resolved. Upgrade to a newer version and it should work as expected. Don't forget to call tf.contrib.keras.backend.clear_session() at the end.

这篇关于TensorFlow的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆