tensorflow 启动Session(tf.Session(),tf.InteractivesSession(),tf.train.Supervisor().managed_session() )
(1)tf.Session()
???????? 計算圖構造完成后, 才能啟動圖. 啟動圖的第一步是創建一個?Session?對象。
示例程序:
#coding:utf-8 import tensorflow as tf #構造圖 a = tf.constant(4) b = tf.constant(5) c = a+b#啟動圖 with tf.Session() as sess: print(sess.run(c))?
(2)tf.InteractivesSession()
???????? 為了便于使用諸如 IPython之類的 Python 交互環境, 可以使用InteractiveSession 代替?Session?類, 使用?Tensor.eval()和?Operation.run()方法代替Session.run(). 這樣可以避免使用一個變量來持有會話。
# coding:utf-8import tensorflow as tfa = tf.constant(4) b = tf.constant(5) c = a + bsess = tf.InteractiveSession() tf.global_variables_initializer().run() print(c.eval() ) sess.close()?
(3)tf.train.Supervisor().managed_session()?
? ? ? ?? 與上面兩種啟動圖相比較來說,Supervisor() 幫助我們處理一些事情:
???????? (a) 自動去 checkpoint 加載數據或者初始化數據
?????? (b) 自動有一個 Saver ,可以用來保存 checkpoint
?????????????? eg: sv.saver.save(sess, save_path)
?
????????? (c) 有一個 summary_computed 用來保存 Summary
???????? 因此我們可以省略了以下內容:
????????? (a)手動初始化或者從 checkpoint? 中加載數據
????????? (b)不需要創建 Saver 類, 使用 sv 內部的就可以
????????? (c)不需要創建 Summary_Writer()
# coding:utf-8import tensorflow as tfa = tf.constant(4) b = tf.constant(5) c = a + bsup_sess = tf.train.Supervisor(logdir=None, init_op=tf.global_variables_initializer()) with sup_sess.managed_session() as sess:print(sess.run(c))?
總結
以上是生活随笔為你收集整理的tensorflow 启动Session(tf.Session(),tf.InteractivesSession(),tf.train.Supervisor().managed_session() )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab 去除pdf文档水印
- 下一篇: matlab 字符串处理