在 Tensorflow Eager Execution 中迭代时内存不断增加 [英] Memory Continually Increasing When Iterating in Tensorflow Eager Execution
问题描述
我正在尝试将一个函数迭代地拟合到需要大量迭代的研究应用程序中的数据.在每个函数中,我都在优化一个变量.为简单起见,我在一个最小的工作示例中重新创建了问题,不包括函数但内存仍在攀升:
I am trying to iteratively fit a function to data in a research application, where a large number of iterations are needed. In each function I am optimising on a variable. For simplicity I have recreated the problem in a minimal working example, excluding the function but the memory still climbs:
import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe
import psutil as ps
import gc
tf.enable_eager_execution()
for i in range (50000):
w0=tfe.Variable(initial_value=np.ones((8,1)))
print(ps.virtual_memory().percent)
如果迭代次数足够多,内存会不断攀升.随着图形的增长,这从图形构建的角度来看是有意义的,但是根据eager中的文档,您可以按如下方式释放内存:
The memory continually climbs given enough iterations. This would make sense from a graph building perspective as the graph would grow, however according to the docs in eager, you can release the memory as follows:
for i in range (50000):
w0=tfe.Variable(initial_value=np.ones((8,1)))
w0=None
print(ps.virtual_memory().percent)
然而内存继续攀升.如果我将变量从循环中取出并尝试分配给变量:
However the memory continues to climb. If I take the variable out of the loop and try and assign to the variable:
w0=tfe.Variable(initial_value=np.ones((8,1)))
for i in range (50000):
w0.assign(np.zeros((8,1)))
print(ps.virtual_memory().percent)
内存似乎在继续攀升.最后,我尝试在每次迭代中运行垃圾收集器:
the memory appears to continue to climb. Lastly, I tried running the garbage collector on every iteration:
for i in range (50000):
w0=tfe.Variable(initial_value=np.ones((8,1)))
w0=None
gc.collect()
print(ps.virtual_memory().percent)
而且我的内存仍然增加.
and I still get an increase in memory.
我是否遗漏了某些东西,或者我是否误解了在 Eager Execution 中可能使用的变量?
Is there something that I am missing or am I misunderstanding the possible use of the variable in eager execution?
推荐答案
Eager Execution 目前有一个比较严重的问题,内存似乎泄漏的比较多.
There is currently a rather serious issue in eager execution, memory seems to be leaking rather abundantly.
您可以在此处找到相关问题.那里给出的解决方法是:
You can find the related issue filed here. The workaround given there is:
如果您同时想要解决方法,tf.set_random_seed(1)
将清除内核缓存.
If you want a workaround in the meantime,
tf.set_random_seed(1)
will clear the kernel cache.
这篇关于在 Tensorflow Eager Execution 中迭代时内存不断增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!