Python 在会话结束前从tensorflow张量中释放内存
我的tensorflow内存不足 本质上,我多次调用一个工具,每次该工具都会向tensorflow图添加更多的张量。我想知道是否有一种方法可以在不重置整个图的情况下删除tensorflow图上的单个张量(并从内存中清除) 我不想重置整个图的原因是我在图上加载了一个keras模型,我不想在每次迭代时都重新加载模型 我尝试过摆弄tensorflow的急切执行,但我正在加载一个带有占位符的预训练keras模型,所以这是不兼容的 此外,该工具创建的张量基于keras模型,因此我无法使用多个图 代码:Python 在会话结束前从tensorflow张量中释放内存,python,tensorflow,keras,Python,Tensorflow,Keras,我的tensorflow内存不足 本质上,我多次调用一个工具,每次该工具都会向tensorflow图添加更多的张量。我想知道是否有一种方法可以在不重置整个图的情况下删除tensorflow图上的单个张量(并从内存中清除) 我不想重置整个图的原因是我在图上加载了一个keras模型,我不想在每次迭代时都重新加载模型 我尝试过摆弄tensorflow的急切执行,但我正在加载一个带有占位符的预训练keras模型,所以这是不兼容的 此外,该工具创建的张量基于keras模型,因此我无法使用多个图 代码: t
tf version: 1.14.0
keras version: 2.2.4
import tensorflow as tf
import keras
def num_of_tensors():
# Gets the number of tensors in the graph
graph_names = [n.name for n in tf.get_default_graph().as_graph_def().node]
print(len(graph_names))
# Any model will work here.
model = keras.applications.ResNet50(weights='imagenet')
num_of_tensors()
gradients = tf.gradients(model.outputs, model.inputs)
num_of_tensors()
del gradients
# Even after you delete the gradients, the tensors are not cleared.
num_of_tensors()