如何释放部分Tensorflow会话中不再使用的GPU内存?

时间:2019-09-18 18:45:19

标签: python tensorflow memory

在我的某些应用程序中,我使用大量数据评估梯度。在获得梯度的求和平均值之后,用于评估该梯度部分的数据将不再使用,并且应该释放相应的GPU内存,并且可以在同一会话中重用该数据。但是,我没有找到任何可以这种方式管理内存的方法。如果有人可以帮助我,我将非常感激。

我知道,如果会话关闭,我们可以释放内存。但是,由于应该保留变量和网络参数以进行进一步的迭代,因此我仍然无法通过关闭会话来释放那部分内存。

import tensorflow as tf


param = tf.trainable_variables()
grad = tf.gradients(loss, param)

with tf.session() as sess:
    sess.run(grad, feed_dict=feed_dict)
    '''
    After evaluating the grad, those data in used can be freed since grad is obtained.
    And I am able to reused that part of memory to do other operations.
    So I am wondering how can we release the memory within the same session?
    '''

0 个答案:

没有答案