2017-04-19 190 views
0

的時候,這裏是代碼:NotFoundError恢復tensorflow會議

import tensorflow as tf 

def save(checkpoint_file='hello.chk'): 
    with tf.Session() as session: 
     x = tf.Variable(initial_value=[1, 2, 3], name="x") 
     y = tf.Variable(initial_value=[[1.0, 2.0], [3.0, 4.0]], name="y") 
     not_saved = tf.Variable(initial_value=[[11.0, 2.0], [3.0, 4.0]], name="not_saved") 
     session.run(tf.global_variables_initializer()) 
     print(session.run(tf.global_variables())) 
     saver = tf.train.Saver([x, y]) 
     saver.save(session, checkpoint_file) 
     print(session.run(tf.global_variables())) 
     print("saved!!!!!!!!!!") 

def restore(checkpoint_file='hello.chk'): 
    with tf.Session() as session: 
     saver = tf.train.Saver() 
     saver.restore(sess=session, save_path=checkpoint_file) 
     print(session.run(tf.global_variables())) 

def reset(): 
    tf.reset_default_graph() 

save() 
restore("/home/kaiyin/PycharmProjects/text-classify/hello.chk") 

我只是想保存和恢復這裏一些簡單的變量,沒有什麼複雜的。保存部分看起來很好,但在還原的時候,我得到了以下錯誤:

Traceback (most recent call last): 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1022, in _do_call 
    return fn(*args) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1004, in _run_fn 
    status, run_metadata) 
    File "/usr/lib/python3.5/contextlib.py", line 66, in __exit__ 
    next(self.gen) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status 
    pywrap_tensorflow.TF_GetCode(status)) 
tensorflow.python.framework.errors_impl.NotFoundError: Key not_saved not found in checkpoint 
    [[Node: save_1/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]] 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/home/kaiyin/PycharmProjects/text-classify/restore.py", line 25, in <module> 
    restore("/home/kaiyin/PycharmProjects/text-classify/hello.chk") 
    File "/home/kaiyin/PycharmProjects/text-classify/restore.py", line 18, in restore 
    saver.restore(sess=session, save_path=checkpoint_file) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1428, in restore 
    {self.saver_def.filename_tensor_name: save_path}) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 767, in run 
    run_metadata_ptr) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 965, in _run 
    feed_dict_string, options, run_metadata) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run 
    target_list, options, run_metadata) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors_impl.NotFoundError: Key not_saved not found in checkpoint 
    [[Node: save_1/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]] 

Caused by op 'save_1/RestoreV2', defined at: 
    File "/home/kaiyin/PycharmProjects/text-classify/restore.py", line 25, in <module> 
    restore("/home/kaiyin/PycharmProjects/text-classify/hello.chk") 
    File "/home/kaiyin/PycharmProjects/text-classify/restore.py", line 17, in restore 
    saver = tf.train.Saver() 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1040, in __init__ 
    self.build() 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1070, in build 
    restore_sequentially=self._restore_sequentially) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 675, in build 
    restore_sequentially, reshape) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 402, in _AddRestoreOps 
    tensors = self.restore_op(filename_tensor, saveable, preferred_shard) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 242, in restore_op 
    [spec.tensor.dtype])[0]) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/gen_io_ops.py", line 668, in restore_v2 
    dtypes=dtypes, name=name) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op 
    op_def=op_def) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__ 
    self._traceback = _extract_stack() 

NotFoundError (see above for traceback): Key not_saved not found in checkpoint 
    [[Node: save_1/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]] 


Process finished with exit code 1 

Tensorflow版本:

>>> print(tf.__version__) 
1.0.1 

回答

0

刪除在tf.train.Saver()瓦爾列表某種程度上解決了這個問題。這裏是工作代碼:

import tensorflow as tf 

filepath = "/home/kaiyin/PycharmProjects/text-classify/hello.chk" 

def save(checkpoint_file=filepath): 
    with tf.Session() as session: 
     x = tf.Variable(initial_value=[1, 2, 3], name="x") 
     y = tf.Variable(initial_value=[[1.0, 2.0], [3.0, 4.0]], name="y") 
     not_saved = tf.Variable(initial_value=[[11.0, 2.0], [3.0, 4.0]], name="not_saved") 
     session.run(tf.global_variables_initializer()) 
     print(session.run(tf.global_variables())) 
     saver = tf.train.Saver() 
     saver.save(session, checkpoint_file) 
     print(session.run(tf.global_variables())) 
     print("saved!!!!!!!!!!") 

def restore(checkpoint_file='hello.chk'): 
    with tf.Session() as session: 
     saver = tf.train.Saver() 
     saver.restore(sess=session, save_path=checkpoint_file) 
     print(session.run(tf.global_variables()[0])) 
     print(session.run(x)) 

def reset(): 
    tf.reset_default_graph() 

save() 
restore(filepath)