tensorflow 筆記 15:如何使用 Supervisor

如何使用Supervisor
在不使用Supervisor的時候,咱們的代碼常常是這麼組織的session

variables
...
ops
...
summary_op
...
merge_all_summarie
saver
init_op

with tf.Session() as sess:
writer = tf.tf.train.SummaryWriter()
sess.run(init)
saver.restore()
for ...:
train
merged_summary = sess.run(merge_all_summarie)
writer.add_summary(merged_summary,i)
saver.save

 

下面介紹如何用Supervisor來改寫上面程序spa

import tensorflow as tf
a = tf.Variable(1)
b = tf.Variable(2)
c = tf.add(a,b)
update = tf.assign(a,c)
tf.scalar_summary("a",a)
init_op = tf.initialize_all_variables()
merged_summary_op = tf.merge_all_summaries()
sv = tf.train.Supervisor(logdir="/home/keith/tmp/",init_op=init_op) #logdir用來保存checkpoint和summary
saver=sv.saver #建立saver
with sv.managed_session() as sess: #會自動去logdir中去找checkpoint,若是沒有的話,自動執行初始化
for i in xrange(1000):
update_ = sess.run(update)
print update_
if i % 10 == 0:
merged_summary = sess.run(merged_summary_op)
sv.summary_computed(sess, merged_summary,global_step=i)
if i%100 == 0:
saver.save(sess,logdir="/home/keith/tmp/",global_step=i)

 

總結
從上面代碼能夠看出,Supervisor幫助咱們處理一些事情
(1)自動去checkpoint加載數據或初始化數據
(2)自身有一個Saver,能夠用來保存checkpoint
(3)有一個summary_computed用來保存Summary
因此,咱們就不須要:
(1)手動初始化或從checkpoint中加載數據
(2)不須要建立Saver,使用sv內部的就能夠
(3)不須要建立summary writer

scala

相關文章
相關標籤/搜索