天天看點

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

【tensorflow】重置/清除計算圖

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

繼續閱讀