質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

0回答

426閲覧

tensorflowでLSTMを複数定義してモデルを復元するとき,エラーが発生してしまいます

abea

総合スコア32

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

1クリップ

投稿2019/11/15 07:26

tensorflow1.9.0で,LSTMの重みを保存するプログラムを以下のように書いて

Python

1import tensorflow as tf 2 3# Save variables 4graph = tf.Graph() 5with graph.as_default(): 6 with tf.variable_scope('1'): 7 cell1 = tf.nn.rnn_cell.LSTMCell(10) 8 outputs, _ = tf.nn.dynamic_rnn(cell1,inputs = tf.constant([[[1.],[1.]]]),dtype=tf.float32,sequence_length=tf.constant([2])) 9 10 with tf.Session() as sess1: 11 saver1 = tf.train.Saver() 12 tf.global_variables_initializer().run() 13 saver1.save(sess1, "model/sample2/export") 14 print(sess1.run(outputs)) 15 16

下のように,複数のLSTMを定義したコードでvariable scopeが”1”のLSTMの重みだけをリストアしたいのですが,下のコードを実行するとvariable scopeが"2"のLSTMのbiasが見つからないとエラーがでてしまいます.

Python

1import tensorflow as tf 2 3# Restore variables 4graph = tf.Graph() 5with graph.as_default(): 6 with tf.variable_scope('1'): 7 cell1 = tf.nn.rnn_cell.LSTMCell(10) 8 outputs1, _ = tf.nn.dynamic_rnn(cell1,inputs = tf.constant([[[1.],[1.]]]),dtype=tf.float32,sequence_length=tf.constant([2])) 9 10 with tf.variable_scope('2'): 11 cell2 = tf.nn.rnn_cell.LSTMCell(10) 12 outputs2, _ = tf.nn.dynamic_rnn(cell2,inputs = tf.constant([[[1.],[1.]]]),dtype=tf.float32,sequence_length=tf.constant([2])) 13 14 with tf.Session() as sess2: 15 saver2 = tf.train.Saver() 16 tf.global_variables_initializer().run() 17 saver2.restore(sess2, "model/sample2/export") 18 19 print(sess2.run(outputs1)) 20 print(sess2.run(outputs2))

エラーの内容は以下のようになっています.これを解決するにはどうすればよいでしょうか?教えてくださると助かります.

2019-11-15 16:20:50.264359: W tensorflow/core/framework/op_kernel.cc:1318] OP_REQUIRES failed at save_restore_v2_ops.cc:184 : Not found: Key 2/rnn/lstm_cell/bias not found in checkpoint

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1322, in _do_call
return fn(*args)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1307, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1409, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.NotFoundError: Key 2/rnn/lstm_cell/bias not found in checkpoint
[[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "tensor_restore_lstm.py", line 17, in <module>
saver2.restore(sess2, "model/sample2/export")
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 1768, in restore
six.reraise(exception_type, exception_value, exception_traceback)
File "/usr/local/lib/python3.5/dist-packages/six.py", line 693, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 1752, in restore
{self.saver_def.filename_tensor_name: save_path})
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 900, in run
run_metadata_ptr)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1135, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1316, in _do_run
run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1335, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Key 2/rnn/lstm_cell/bias not found in checkpoint
[[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

Caused by op 'save/RestoreV2', defined at:

File "tensor_restore_lstm.py", line 15, in <module>
saver2 = tf.train.Saver()
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 1284, in init
self.build()
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 1296, in build
self._build(self._filename, build_save=True, build_restore=True)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 1333, in _build
build_save=build_save, build_restore=build_restore)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 781, in _build_internal
restore_sequentially, reshape)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 400, in _AddRestoreOps
restore_sequentially)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 832, in bulk_restore
return io_ops.restore_v2(filename_tensor, names, slices, dtypes)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_io_ops.py", line 1463, in restore_v2
shape_and_slices=shape_and_slices, dtypes=dtypes, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3414, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1740, in init
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

NotFoundError (see above for traceback): Key 2/rnn/lstm_cell/bias not found in checkpoint

[[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問