是否可以修改现有的 TensorFlow 计算图? [英] Is it possible to modify an existing TensorFlow computation graph?

查看:28
本文介绍了是否可以修改现有的 TensorFlow 计算图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TensorFlow 图通常从输入到输出逐渐构建,然后执行.查看 Python 代码,操作的输入列表是不可变的,这表明不应修改输入.这是否意味着无法更新/修改现有图表?

TensorFlow graph is usually built gradually from inputs to outputs, and then executed. Looking at the Python code, the inputs lists of operations are immutable which suggests that the inputs should not be modified. Does that mean that there is no way to update/modify an existing graph?

推荐答案

TensorFlow tf.Graph 类是一个append-only 数据结构,这意味着您可以在执行部分图后向图中添加节点,但不能删除或修改现有节点.由于当您调用 Session.run() 时,TensorFlow 仅执行必要的子图,在图中有冗余节点没有执行时间成本(尽管它们会继续消耗内存).

The TensorFlow tf.Graph class is an append-only data structure, which means that you can add nodes to the graph after executing part of the graph, but you cannot remove or modify existing nodes. Since TensorFlow executes only the necessary subgraph when you call Session.run(), there is no execution-time cost to having redundant nodes in the graph (although they will continue to consume memory).

要删除图中的所有节点,您可以使用新图创建会话:

To remove all nodes in the graph, you can create a session with a new graph:

with tf.Graph().as_default():  # Create a new graph, and make it the default.
  with tf.Session() as sess:  # `sess` will use the new, currently empty, graph.
    # Build graph and execute nodes in here.

这篇关于是否可以修改现有的 TensorFlow 计算图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆