tf version: 1.14.0
keras version: 2.2.4
我的 tensorflow 内存不足。
本质上,我多次调用一个工具,每次该工具都会向张量流图添加更多张量。我想知道是否有一种方法可以在不重置整个图的情况下删除(并从内存中清除)张量流图上的单个张量。
我不想重置整个图表的原因是我在图表上加载了一个 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()