調用
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()外部調用。
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