【问题标题】:Deleting all but a few nodes in TensorFlow graph删除 TensorFlow 图中除少数几个节点之外的所有节点
【发布时间】:2016-05-08 17:54:42
【问题描述】:

我的 TensorFlow 用例要求我为每个需要处理的实例构建一个新的计算图。这最终会增加内存需求。

除了几个tf.Variables 是模型参数之外,我想删除所有其他节点。其他有类似问题的人发现tf.reset_default_graph() 很有用,但这会摆脱我需要坚持的模型参数。

我可以用什么来删除除这些节点之外的所有节点?

编辑: 实例特定的计算实际上只是意味着我添加了很多新的操作。我相信这些操作是内存问题背后的原因。

更新: 请参阅最近发布的 tensorflow fold (https://github.com/tensorflow/fold),它允许动态构建计算图。

【问题讨论】:

  • tf.reset_default_graph

标签: python tensorflow deep-learning


【解决方案1】:

tf.graph 数据结构被设计为 append-only 数据结构。因此无法移除或修改现有节点。通常这不是问题,因为在运行会话时只处理必要的子图。

您可以尝试将图表的变量复制到新图表中并删除旧图表。要存档,只需运行:

old_graph = tf.get_default_graph() # Save the old graph for later iteration
new_graph = tf.graph() # Create an empty graph
new_graph.set_default() # Makes the new graph default

如果您想遍历旧图中的所有节点,请使用:

for node in old_graph.get_operations():
    if node.type == 'Variable':
       # read value of variable and copy it into new Graph

您也可以使用:

for node in old_graph.get_collection('trainable_variables'):
   # iterates over all trainable Variabels
   # read and create new variable

还可以查看python/framework/ops.py : 1759,了解更多在图中操作节点的方法。

但是,在您弄乱tf.Graph 之前,我强烈建议您考虑一下这是否真的需要。通常可以尝试泛化计算并使用共享变量构建图,以便您要处理的每个实例都是该图的子图。

【讨论】:

  • 我刚刚意识到内存爆炸的原因是为每次计算添加的新操作。我该如何清理这些?
猜你喜欢
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多