Python 从图中删除节点或重置整个默认图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33765336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove nodes from graph or reset entire default graph
提问by Mohammed AlQuraishi
When working with the default global graph, is it possible to remove nodes after they've been added, or alternatively to reset the default graph to empty? When working with TF interactively in IPython, I find myself having to restart the kernel repeatedly. I would like to be able to experiment with graphs more easily if possible.
使用默认全局图时,是否可以在添加节点后删除节点,或者将默认图重置为空?在 IPython 中以交互方式使用 TF 时,我发现自己不得不反复重新启动内核。如果可能的话,我希望能够更轻松地试验图表。
采纳答案by Yaroslav Bulatov
Update 11/2/2016
2016 年 11 月 2 日更新
tf.reset_default_graph()
tf.reset_default_graph()
Old stuff
老东西
There's reset_default_graph
, but not part of public API (I think it should be, does someone wants to file an issueon GitHub?)
有reset_default_graph
,但不是公共 API 的一部分(我认为应该是,有人想在 GitHub 上提交问题吗?)
My work-around to reset things is this:
我重置事情的解决方法是这样的:
from tensorflow.python.framework import ops
ops.reset_default_graph()
sess = tf.InteractiveSession()
回答by Thomas Moreau
By default, a session is constructed around the default graph. To avoid leaving dead nodes in the session, you need to either control the default graph or use an explicit graph.
默认情况下,会话是围绕默认图形构建的。为了避免在会话中留下死节点,您需要控制默认图或使用显式图。
To clear the default graph, you can use the tf.reset_default_graphfunction.
tf.reset_default_graph() sess = tf.InteractiveSession()
You can also construct explicitly a graph and avoid using the default one. If you use a normal
Session
, you will need to fully create the graph before constructing the session. ForInteractiveSession
, you can just declare the graph and use it as a context to declare further changes:g = tf.Graph() sess = tf.InteractiveSession(graph=g) with g.asdefault(): # Put variable declaration and other tf operation # in the graph context .... b = tf.matmul(A, x) .... sess.run([b], ...)
要清除默认图形,您可以使用tf.reset_default_graph函数。
tf.reset_default_graph() sess = tf.InteractiveSession()
您还可以显式构建一个图形并避免使用默认图形。如果使用普通
Session
,则需要在构建会话之前完全创建图形。对于InteractiveSession
,您可以只声明图形并将其用作上下文来声明进一步的更改:g = tf.Graph() sess = tf.InteractiveSession(graph=g) with g.asdefault(): # Put variable declaration and other tf operation # in the graph context .... b = tf.matmul(A, x) .... sess.run([b], ...)
EDIT: For recent versions of tensorflow
(1.0+), the correct function is g.as_default
.
编辑:对于tensorflow
(1.0+) 的最新版本,正确的函数是g.as_default
.
回答by Serge
IPython / Jupyter notebook cells keep state between runs of a cell.
IPython / Jupyter 笔记本单元在单元运行之间保持状态。
Create a custom graph:
创建自定义图表:
def main():
# Define your model
data = tf.placeholder(...)
model = ...
with tf.Graph().as_default():
main()
Once ran, the graph is cleaned up.
一旦运行,图形就会被清理。
回答by John Doe
Not sure if I faced the very same problem, but
不确定我是否遇到了同样的问题,但是
tf.keras.backend.clear_session()
at the beginning of the cell in which the model (Keras, in my case) was constructed and trained helped to "cut the clutter" so only the current graph remains in the TensorBoard visualization after repeated runs of the same cell.
在构建和训练模型(Keras,在我的情况下)的单元格的开头有助于“消除混乱”,因此在重复运行同一单元格后,只有当前图形保留在 TensorBoard 可视化中。
Environment: TensorFlow 2.0 (tensorflow-gpu==2.0.0b1
) in Colab with built-in TensorBoard (using the %load_ext tensorboard
trick).
环境:tensorflow-gpu==2.0.0b1
带有内置 TensorBoard(使用%load_ext tensorboard
技巧)的Colab 中的TensorFlow 2.0 ( )。
回答by Tensorflow Support
Tensorflow 2.0 Compatible Answer: In Tensorflow Version >= 2.0
, the Command to Reset Entire Default Graph, when run in Graph Mode is tf.compat.v1.reset_default_graph
.
Tensorflow 2.0 兼容答案:在Tensorflow Version >= 2.0
图形模式下运行时,重置整个默认图形的命令是tf.compat.v1.reset_default_graph
.
NOTE: The default graph is a property of the current thread. This function applies only to the current thread. Calling this function while a tf.compat.v1.Session
or tf.compat.v1.InteractiveSession
is active will result in undefined behavior. Using any previously created tf.Operation
or tf.Tensor
objects after calling this function will result in undefined behavior.
注意:默认图形是当前线程的属性。此功能仅适用于当前线程。在 atf.compat.v1.Session
或tf.compat.v1.InteractiveSession
处于活动状态时调用此函数将导致未定义的行为。在调用此函数后使用任何先前创建的对象tf.Operation
或tf.Tensor
对象将导致未定义的行为。
Raises: AssertionError: If this function is called within a nested graph.
引发:断言错误:如果在嵌套图中调用此函数。