180412 tf.reset_default_graph()を使用して、デフォルトのグラフ計算グラフとノードノードをリセットします
180412 Use Tf Reset_default_graph Reset Default Graph Calculation Graph
- 問題の説明 :テンソルフローで増加しているノードをクリアし、実行するたびにデフォルトグラフ全体をリセットするにはどうすればよいですか?
import tensorflow as tf # tf.reset_default_graph() with tf.variable_scope('Space_a'): a = tf.constant([1,2,3]) with tf.variable_scope('Space_b'): b = tf.constant([4,5,6]) with tf.variable_scope('Space_c'): c = a + b d = a + b with tf.Session()as sess: print(a) print(b) print(c) print(d) print(sess.run(c)) print(sess.run(d))
- 解決 :
tf.reset_default_graph()
グラフからノードを削除するか、デフォルトのグラフ全体をリセットします
- コード
import tensorflow as tf tf.reset_default_graph() # Use this to clear the defualt graph and nodes with tf.variable_scope('Space_a'): a = tf.constant([1,2,3]) with tf.variable_scope('Space_b'): b = tf.constant([4,5,6]) with tf.variable_scope('Space_c'): c = a + b d = a + b with tf.Session()as sess: print(a) print(b) print(c) print(d) print(sess.run(c)) print(sess.run(d))