2017-10-10 50 views
1

我構建了一個帶有兩個隱藏層的神經網絡。當我啓動了會議由我保存會話:當會話關閉時啓動模型 - Tensorflow

saver.save(sess, "model.ckpt") 

如果我留在同一個會議上,我推出這個代碼:

restorer=tf.train.Saver() 
with tf.Session() as sess: 
    restorer.restore(sess,"./prova") 
    new_graph = tf.train.import_meta_graph('prova.meta')  
    new_graph.restore(sess, 'prova.ckpt') 
    feed={ 
     pred1.inputs:test_data, 
     pred1.is_training:False 
    } 
    test_predict=sess.run(pred1.predicted,feed_dict=feed) 

我可以啓動模型進行測試。

問題是:在會話關閉時有一種啓動模型的方法嗎?特別是,我將我的訓練結果保存在.ckpt中,我可以在另一個時刻重新啓動模型?

+0

爲什麼你不想在測試時間開始另一個會話? – Maxim

+0

目前與火車或測試組一樣開始一個新的會話。我想要的是明白如果可能開始一個新的會議與在ckpt文件salved中的日期;而且,如果可能的話,該怎麼做。謝謝 – jjgasse

+0

您已經提供了在新會話中恢復模型的片段。那裏沒有培訓。這段代碼有什麼問題? – Maxim

回答

0

您不能在tf.Session之外運行模型。來自the documentation的報價:

Session對象封裝了執行Operation對象的環境,並且評估Tensor對象。

,但你可以很容易地打開和關閉會話很多次,用現有的圖形或加載以前保存的圖表,並在新的會話使用它。這裏有一個稍微修改example from the doc

import tensorflow as tf 

v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer) 
v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer) 

inc_v1 = v1.assign(v1+1) 
dec_v2 = v2.assign(v2-1) 
init_op = tf.global_variables_initializer() 
saver = tf.train.Saver() 

with tf.Session() as sess: 
    sess.run(init_op) 
    inc_v1.op.run() 
    dec_v2.op.run() 
    save_path = saver.save(sess, "/tmp/model.ckpt") 
    print("Model saved in file: %s" % save_path) 

with tf.Session() as sess: 
    saver.restore(sess, "/tmp/model.ckpt") 
    print("Model restored.") 
    print("v1 : %s" % v1.eval()) 
    print("v2 : %s" % v2.eval()) 

這兩屆會議之間,你不能評價v1v2,但你可以開始一個新的會話之後。