2016-11-18 114 views
0

我正試圖學習如何保存和恢復模型。我的模型是有點過於複雜,所以這裏是一個MWE,直接從命令行Python,產生了同樣的錯誤:Tensorflow保存和加載

import tensorflow as tf 
v1 = tf.Variable(1, name="var1") 
init_op = tf.initialize_all_variables() 
saver = tf.train.Saver() 
with tf.Session() as sess: 
    sess.run(init_op) 
    save_path = saver.save(sess, "testchk.ckpt") 
    print "model saved" 

這種格式如下TensorFlow文檔瀏覽:https://www.tensorflow.org/versions/r0.11/api_docs/python/state_ops.html#Variable

錯誤我得到的是:

Traceback (most recent call last): 
    File "<stdin>", line 3, in <module> 
    File "/mnt/data/user/pkgs/enthought/canopy-1.5.1/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1080, in save 
self.last_checkpoints, latest_filename) 
    File "/mnt/data/user/pkgs/enthought/canopy-1.5.1/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 632, in update_checkpoint_state 
    file_io.rename(temp_pathname, coord_checkpoint_filename, overwrite=True) 
    File "/mnt/data/user/pkgs/enthought/canopy-1.5.1/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 79, in rename 
compat.as_bytes(oldname), compat.as_bytes(newname), overwrite, status) 
    File "/opt/enthought/canopy-1.5.1/appdata/canopy-1.5.1.2730.rh5-x86_64/lib/python2.7/contextlib.py", line 24, in __exit__ 
self.gen.next() 
    File "/mnt/data/user/pkgs/enthought/canopy-1.5.1/lib/python2.7/site-packages/tensorflow/python/framework/errors.py", line 450, in raise_exception_on_not_ok_status 
    pywrap_tensorflow.TF_GetCode(status)) 
tensorflow.python.framework.errors.FailedPreconditionError: checkpoint.tmp.fe418ea583db4995810d23d4ca308e3a 

如何解決此錯誤?

+0

看起來像文件系統錯誤,路徑是否存在?該程序是否具有寫入權限? – yuefengz

+0

默認情況下,它不只是目前的工作目錄嗎?是的,它具有寫入權限。 – StatsSorceress

+0

嘗試使用絕對路徑? – yuefengz

回答

0

好吧,絕對路徑並不完全是答案,但這裏有效。 在終端:

mkdir test_checkpoint 
python 

然後

import tensorflow as tf 
v1 = tf.Variable(1, name="var1") 
init_op = tf.initialize_all_variables() 
saver = tf.train.Saver() 
with tf.Session() as sess: 
    sess.run(init_op) 
    save_path = saver.save(sess, "./test_checkpoint/testchk.ckpt") 
    print "model saved" 

它打印出一堆東西,然後打印:model saved

所以我覺得它的工作!該目錄之前存在,所以我不知道爲什麼重新創建它,但至少有一個解決方案。