Python 3.x Tensorflow函数RAM使用率持续上升

Python 3.x Tensorflow函数RAM使用率持续上升,python-3.x,tensorflow,memory-management,memory-leaks,Python 3.x,Tensorflow,Memory Management,Memory Leaks,我有一个非常简单的基于tensorflow的函数,它采用形状张量(1,6,64,64,64,1),并返回形状张量(1,6,3),其中包含原始张量中每个(64,64,64)体积的质心。我的工作没有任何问题,但每次我的循环(见下文)进入下一个迭代,我的电脑中使用的RAM都会增加。这将我的样本限制在500个左右,然后我就完全用完了。我想我在某个地方遗漏了什么,但我没有足够的经验去知道在哪里 代码: lm_vol是(1,6,64,64,64,1)数组。它们只是numpy数组,被转换成张量 """

我有一个非常简单的基于tensorflow的函数,它采用形状张量(1,6,64,64,64,1),并返回形状张量(1,6,3),其中包含原始张量中每个(64,64,64)体积的质心。我的工作没有任何问题,但每次我的循环(见下文)进入下一个迭代,我的电脑中使用的RAM都会增加。这将我的样本限制在500个左右,然后我就完全用完了。我想我在某个地方遗漏了什么,但我没有足够的经验去知道在哪里

代码:

lm_vol是(1,6,64,64,64,1)数组。它们只是numpy数组,被转换成张量

    """
    Get similarity matrix
    """
    pts_raw = get_raw_centroids(lm_vol)
    print(sess.run(pts_raw))
    sess.close()

我也尝试过将tf.Session()放在循环之外,但没有任何区别

上述代码中的问题是,当调用函数
get_raw_centroids
时,您正在循环中创建多个图形

让我们考虑一个更简单的例子:

def get_raw_centroids(lm_vol):
   raw_centroids = lm_vol * 2
   return raw_centroids

for i in range(10):

   sess = tf.Session()
   lm_vol = tf.constant(3)
   pts_raw = get_raw_centroids(lm_vol)
    print(sess.run(pts_raw))
    print('****Graph: ***\n')
    print([x for x in tf.get_default_graph().get_operations()])
    sess.close()
上述代码的输出为:

#6
#****Graph: ***

#[<tf.Operation 'Const' type=Const>, 
#<tf.Operation   'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>]

#6
#****Graph: ***

#[<tf.Operation 'Const' type=Const>,
# <tf.Operation 'mul/y' type=Const>, 
# <tf.Operation 'mul' type=Mul>, 
# <tf.Operation 'Const_1' type=Const>, 
# <tf.Operation 'mul_1/y' type=Const>, 
# <tf.Operation 'mul_1' type=Mul>]

#6
#****Graph: ***

#[<tf.Operation 'Const' type=Const>,
#<tf.Operation 'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>, 
#<tf.Operation 'Const_1' type=Const>, 
#<tf.Operation 'mul_1/y' type=Const>, 
#<tf.Operation 'mul_1' type=Mul>, 
#<tf.Operation 'Const_2' type=Const>, 
#<tf.Operation 'mul_2/y' type=Const>, 
#<tf.Operation 'mul_2' type=Mul>]

...
代码的输出将是:

#6.0
#****Graph: ***

#[<tf.Operation 'Placeholder' type=Placeholder>,
#<tf.Operation 'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>]

#6.0
#****Graph: ***

#[<tf.Operation 'Placeholder' type=Placeholder>, 
#<tf.Operation 'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>]

#6.0
#****Graph: ***

#[<tf.Operation 'Placeholder' type=Placeholder>, 
#<tf.Operation 'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>]
#6.0
#****图表:***
#[,
#, 
#]
#6.0
#****图表:***
#[, 
#, 
#]
#6.0
#****图表:***
#[, 
#, 
#]

上述代码中的问题是,在调用函数
get\u raw\u centroids
时,您正在循环中创建多个图形

让我们考虑一个更简单的例子:

def get_raw_centroids(lm_vol):
   raw_centroids = lm_vol * 2
   return raw_centroids

for i in range(10):

   sess = tf.Session()
   lm_vol = tf.constant(3)
   pts_raw = get_raw_centroids(lm_vol)
    print(sess.run(pts_raw))
    print('****Graph: ***\n')
    print([x for x in tf.get_default_graph().get_operations()])
    sess.close()
上述代码的输出为:

#6
#****Graph: ***

#[<tf.Operation 'Const' type=Const>, 
#<tf.Operation   'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>]

#6
#****Graph: ***

#[<tf.Operation 'Const' type=Const>,
# <tf.Operation 'mul/y' type=Const>, 
# <tf.Operation 'mul' type=Mul>, 
# <tf.Operation 'Const_1' type=Const>, 
# <tf.Operation 'mul_1/y' type=Const>, 
# <tf.Operation 'mul_1' type=Mul>]

#6
#****Graph: ***

#[<tf.Operation 'Const' type=Const>,
#<tf.Operation 'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>, 
#<tf.Operation 'Const_1' type=Const>, 
#<tf.Operation 'mul_1/y' type=Const>, 
#<tf.Operation 'mul_1' type=Mul>, 
#<tf.Operation 'Const_2' type=Const>, 
#<tf.Operation 'mul_2/y' type=Const>, 
#<tf.Operation 'mul_2' type=Mul>]

...
代码的输出将是:

#6.0
#****Graph: ***

#[<tf.Operation 'Placeholder' type=Placeholder>,
#<tf.Operation 'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>]

#6.0
#****Graph: ***

#[<tf.Operation 'Placeholder' type=Placeholder>, 
#<tf.Operation 'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>]

#6.0
#****Graph: ***

#[<tf.Operation 'Placeholder' type=Placeholder>, 
#<tf.Operation 'mul/y' type=Const>, 
#<tf.Operation 'mul' type=Mul>]
#6.0
#****图表:***
#[,
#, 
#]
#6.0
#****图表:***
#[, 
#, 
#]
#6.0
#****图表:***
#[, 
#, 
#]