2017-05-31 14 views
0

アーキテクチャ( "A")を読み込んで、アーキテクチャを保存した場合にのみ( "B" A "私は" B "のために保存した変数のみを保存します。Tensorflowのバグ?すべての変数で保存されたアーキテクチャに保存された単一の変数を復元することはできません

これは動作します:これは動作しません

import tensorflow as tf 

#################################################### 
# Architecture "A" 
w1 = tf.Variable(tf.linspace(0.0, 0.5, 6), name="w1") 
w2 = tf.Variable(tf.linspace(1.0, 5.0, 6), name="w2") 

saver = tf.train.Saver({'w1':w1}) #<---------- Save only w1 

sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 

saver.save(sess, './my_architecture') 

tf.reset_default_graph() 
#################################################### 
# Architecture "B" 
w1 = tf.Variable(tf.linspace(10.0, 50.0, 6), name="w1") 
w2 = tf.Variable(tf.linspace(100.0, 500.0, 6), name="w2") 

saver = tf.train.Saver({'w1':w1}) 
sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 

saver.save(sess, './my_variable') 

tf.reset_default_graph() 
###################################################### 
with tf.Session() as sess: 
    # Loading the model structure from 'my_test_model.meta' 
    new_saver = tf.train.import_meta_graph('./my_architecture.meta') 

    # Loading the saved "w1" Variable 
    new_saver.restore(sess,'./my_variable') 

。アーキテクチャを保存するときに「」私はすべての変数または単に私は建築のために保存変数ではありません任意の組み合わせ「Bを救うためならば、つまり

import tensorflow as tf 

#################################################### 
# Architecture "A" 
w1 = tf.Variable(tf.linspace(0.0, 0.5, 6), name="w1") 
w2 = tf.Variable(tf.linspace(1.0, 5.0, 6), name="w2") 

saver = tf.train.Saver() #<---------- Save everything 

sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 

saver.save(sess, './my_architecture') 

tf.reset_default_graph() 
#################################################### 
# Architecture "B" 
w1 = tf.Variable(tf.linspace(10.0, 50.0, 6), name="w1") 
w2 = tf.Variable(tf.linspace(100.0, 500.0, 6), name="w2") 

saver = tf.train.Saver({'w1':w1}) 
sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 

saver.save(sess, './my_variable') 

tf.reset_default_graph() 
###################################################### 
with tf.Session() as sess: 
    # Loading the model structure from 'my_test_model.meta' 
    new_saver = tf.train.import_meta_graph('./my_architecture.meta') 

    # Loading the saved "w1" Variable 
    new_saver.restore(sess,'./my_variable') 

:私はダウンにラインを変更しました「

は、私はこのエラーを取得する:デフォルトでは

INFO:tensorflow:Restoring parameters from ./my_variable 
--------------------------------------------------------------------------- 
NotFoundError        Traceback (most recent call last) 
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 
    1038  try: 
-> 1039  return fn(*args) 
    1040  except errors.OpError as e: 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata) 
    1020         feed_dict, fetch_list, target_list, 
-> 1021         status, run_metadata) 
    1022 

/home/paul/anaconda3/lib/python3.5/contextlib.py in __exit__(self, type, value, traceback) 
    65    try: 
---> 66     next(self.gen) 
    67    except StopIteration: 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status() 
    465   compat.as_text(pywrap_tensorflow.TF_Message(status)), 
--> 466   pywrap_tensorflow.TF_GetCode(status)) 
    467 finally: 

NotFoundError: Key w2 not found in checkpoint 
    [[Node: save/RestoreV2_1 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2_1/tensor_names, save/RestoreV2_1/shape_and_slices)]] 
    [[Node: save/RestoreV2/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_11_save/RestoreV2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"]()]] 

During handling of the above exception, another exception occurred: 

NotFoundError        Traceback (most recent call last) 
<ipython-input-1-bc6592a722bf> in <module>() 
    42 
    43 # Loading the saved "w1" Variable 
---> 44 new_saver.restore(sess,'./my_variable') 
    45 
    46 # initialize_uninitialized_vars(sess) 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/training/saver.py in restore(self, sess, save_path) 
    1455  logging.info("Restoring parameters from %s", save_path) 
    1456  sess.run(self.saver_def.restore_op_name, 
-> 1457    {self.saver_def.filename_tensor_name: save_path}) 
    1458 
    1459 @staticmethod 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 
    776  try: 
    777  result = self._run(None, fetches, feed_dict, options_ptr, 
--> 778       run_metadata_ptr) 
    779  if run_metadata: 
    780   proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 
    980  if final_fetches or final_targets: 
    981  results = self._do_run(handle, final_targets, final_fetches, 
--> 982        feed_dict_string, options, run_metadata) 
    983  else: 
    984  results = [] 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 
    1030  if handle is None: 
    1031  return self._do_call(_run_fn, self._session, feed_dict, fetch_list, 
-> 1032       target_list, options, run_metadata) 
    1033  else: 
    1034  return self._do_call(_prun_fn, self._session, handle, feed_dict, 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 
    1050   except KeyError: 
    1051   pass 
-> 1052  raise type(e)(node_def, op, message) 
    1053 
    1054 def _extend_graph(self): 

NotFoundError: Key w2 not found in checkpoint 
    [[Node: save/RestoreV2_1 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2_1/tensor_names, save/RestoreV2_1/shape_and_slices)]] 
    [[Node: save/RestoreV2/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_11_save/RestoreV2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"]()]] 

Caused by op 'save/RestoreV2_1', defined at: 
    File "/home/paul/anaconda3/lib/python3.5/runpy.py", line 184, in _run_module_as_main 
    "__main__", mod_spec) 
    File "/home/paul/anaconda3/lib/python3.5/runpy.py", line 85, in _run_code 
    exec(code, run_globals) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/ipykernel/__main__.py", line 3, in <module> 
    app.launch_new_instance() 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance 
    app.start() 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 474, in start 
    ioloop.IOLoop.instance().start() 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start 
    super(ZMQIOLoop, self).start() 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/tornado/ioloop.py", line 887, in start 
    handler_func(fd_obj, events) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/tornado/stack_context.py", line 275, in null_wrapper 
    return fn(*args, **kwargs) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events 
    self._handle_recv() 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv 
    self._run_callback(callback, msg) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback 
    callback(*args, **kwargs) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/tornado/stack_context.py", line 275, in null_wrapper 
    return fn(*args, **kwargs) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher 
    return self.dispatch_shell(stream, msg) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell 
    handler(stream, idents, msg) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 390, in execute_request 
    user_expressions, allow_stdin) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute 
    res = shell.run_cell(code, store_history=store_history, silent=silent) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 501, in run_cell 
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell 
    interactivity=interactivity, compiler=compiler, result=result) 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2821, in run_ast_nodes 
    if self.run_code(code, result): 
    File "/home/paul/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code 
    exec(code_obj, self.user_global_ns, self.user_ns) 
    File "<ipython-input-1-bc6592a722bf>", line 41, in <module> 
    new_saver = tf.train.import_meta_graph('./my_architecture.meta') 
    File "/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1595, in import_meta_graph 
    **kwargs) 
    File "/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/meta_graph.py", line 499, in import_scoped_meta_graph 
    producer_op_list=producer_op_list) 
    File "/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/importer.py", line 308, in import_graph_def 
    op_def=op_def) 
    File "/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__ 
    self._traceback = _extract_stack() 

NotFoundError (see above for traceback): Key w2 not found in checkpoint 
    [[Node: save/RestoreV2_1 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2_1/tensor_names, save/RestoreV2_1/shape_and_slices)]] 
    [[Node: save/RestoreV2/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_11_save/RestoreV2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"]()]] 

答えて

1

、メタグラフをインポートして作成したSaverはそのメタグラフの変数のすべてを復元しようとします(とされている変数に文句を言うだろうチェックポイントから抜けている)。しかし、別のチェックポイントに基づいてこれらの変数をフィルタリングすることが可能である:

import tensorflow as tf 

with tf.Graph().as_default(): 
    #################################################### 
    # Architecture "A" 
    w1 = tf.Variable(tf.linspace(0.0, 0.5, 6), name="w1") 
    w2 = tf.Variable(tf.linspace(1.0, 5.0, 6), name="w2") 

    saver = tf.train.Saver() #<---------- Save everything 

    with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 

    saver.save(sess, '/tmp/my_architecture') 

with tf.Graph().as_default(): 
    #################################################### 
    # Architecture "B" 
    w1 = tf.Variable(tf.linspace(10.0, 50.0, 6), name="w1") 
    w2 = tf.Variable(tf.linspace(100.0, 500.0, 6), name="w2") 

    saver = tf.train.Saver({'w1':w1}) 
    with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    saver.save(sess, '/tmp/my_variable') 

restored_graph = tf.Graph() 
with restored_graph.as_default(): 
    tf.train.import_meta_graph('/tmp/my_architecture.meta') 
    vars_to_restore = [ 
     restored_graph.get_tensor_by_name(var_name + ':0') for var_name, _ 
     in tf.contrib.framework.list_variables('/tmp/my_variable')] 
    filtered_saver = tf.train.Saver(var_list=vars_to_restore) 
    with tf.Session() as sess: 
    # Restore w1 from Architecture "B" into the metagraph from Architecture "A" 
    filtered_saver.restore(sess,'/tmp/my_variable') 
    print(restored_graph.get_tensor_by_name('w1:0').eval()) 

プリント:

[ 10. 18. 26. 34. 42. 50.]

関連する問題