【tensorflow】重置/清除计算图

调用tf.reset_default_graph()重置计算图

当在搭建网络查看计算图时,如果重复运行程序会导致重定义报错。为了可以在同一个线程或者交互式环境中(ipython/jupyter)重复调试计算图,就需要使用这个函数来重置计算图,随后修改计算图再次运行。

#重置计算图,清理当前定义节点
import tensorflow as tf
tf.reset_default_graph()

#Your model defined below
#

需要注意的是,下面三种情况使用这个函数会报错:

#1
with graph.as_default():
	#不能用

#2
with tf.Session(): block.
	#不能用
#3
tf.InteractiveSession() 
#Your regions
#不能用
sess.close().

也就是说这个函数需要在with tf.session()外部调用。

在这里插入图片描述
pic from pexels.com


ref:
https://stackoverflow.com/questions/46893824/do-not-use-tf-reset-default-graph-to-clear-nested-graphs
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-nmgf2idd.html

原文地址:https://www.cnblogs.com/Tom-Ren/p/10024290.html