管理图谱
您创建的任何节点都会自动添加到默认图形中:
>>> x1 = tf.Variable(1)>>> x1.graph is tf.get_default_graph()True
在大多数情况下,这是很好的,但有时您可能需要管理多个独立图形。 您可以通过创建一个新的图形并暂时将其设置为一个块中的默认图形,如下所示:
>>> graph = tf.Graph()>>> with graph.as_default():... x2 = tf.Variable(2)...>>> x2.graph is graphTrue>>> x2.graph is tf.get_default_graph()False
在 Jupyter(或 Python shell)中,通常在实验时多次运行相同的命令。 因此,您可能会收到包含许多重复节点的默认图形。 一个解决方案是重新启动 Jupyter 内核(或 Python shell),但是一个更方便的解决方案是通过运行tf.reset_default_graph()来重置默认图。
