tf version: 1.14.0
keras version: 2.2.4
我的张量流内存不足。
从本质上讲,我多次调用该工具,并且每次该工具向张量流图添加更多张量时。我想知道是否有一种方法可以在不重置整个图的情况下删除(并从内存中清除)张量流图中的张量。
我不希望重置整个图的原因是我在图上加载了一个keras模型,并且我不想在每次迭代时都重新加载模型
我尝试摆弄tensorflow的急切执行,但是我正在使用占位符加载预训练的keras模型,因此不兼容。
此外,该工具创建的张量基于keras模型,因此我无法使用多个图。
代码:
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()