I'm trying to run lstm example https://github.com/tflearn/tflearn/blob/master/examples/nlp/lstm.py, which works fine. I intend to save the model for later use:
# Network building
net = tflearn.input_data([None, 100], name='input')
net = tflearn.embedding(net, input_dim=vocabulary_size, output_dim=128, name='embedding')
net = tflearn.lstm(net, 128, dropout=0.8, name='lstm')
net = tflearn.fully_connected(net, 2, activation='softmax', name='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001,
loss='categorical_crossentropy', name='regression')
# Training
model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(trainX, trainY, validation_set=(testX, testY), n_epoch=1, show_metric=True, batch_size=32)
model.save('imdb_lstm_SA.tflearn')
After that, I try to load the model via building the same network with the same structure and corresponding name:
# Network building
net = tflearn.input_data([None, 100], name='input')
net = tflearn.embedding(net, input_dim=vocabulary_size, output_dim=128, name='embedding')
net = tflearn.lstm(net, 128, dropout=0.8, name='lstm')
net = tflearn.fully_connected(net, 2, activation='softmax', name='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001,
loss='categorical_crossentropy', name='regression')
# Training
model = tflearn.DNN(net, tensorboard_verbose=0)
model.load('imdb_lstm_SA.tflearn', weights_only=True)
predY = model.predict(trainX)
I referred to post (https://github.com/tflearn/tflearn/issues/316) by adding weights_only=True, but it seems not solving the problem.
The stacktrace is as follows:
NotFoundError Traceback (most recent call last)
<ipython-input-8-126d1ae60989> in <module>()
9 # Training
10 model = tflearn.DNN(net, tensorboard_verbose=0)
---> 11 model.load('imdb_lstm_SA.tflearn', weights_only=True)
12
13 predY = model.predict(trainX)
/usr/local/lib/python2.7/dist-packages/tflearn/models/dnn.pyc in load(self, model_file, weights_only, **optargs)
258 created for the restored variables.
259 """
--> 260 self.trainer.restore(model_file, weights_only, **optargs)
261 self.session = self.trainer.session
262 self.predictor = Evaluator([self.net],
/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.pyc in restore(self, model_file, trainable_variable_only, variable_name_map, scope_for_restore, create_new_session, verbose)
434 self.restorer.restore(self.session, model_file)
435 else:
--> 436 self.restorer_trainvars.restore(self.session, model_file)
437 for o in self.train_ops:
438 o.session = self.session
/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.pyc in restore(self, sess, save_path)
1343
1344 sess.run(self.saver_def.restore_op_name,
-> 1345 {self.saver_def.filename_tensor_name: save_path})
1346
1347 @staticmethod
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
715 try:
716 result = self._run(None, fetches, feed_dict, options_ptr,
--> 717 run_metadata_ptr)
718 if run_metadata:
719 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
913 if final_fetches or final_targets:
914 results = self._do_run(handle, final_targets, final_fetches,
--> 915 feed_dict_string, options, run_metadata)
916 else:
917 results = []
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
963 if handle is None:
964 return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
--> 965 target_list, options, run_metadata)
966 else:
967 return self._do_call(_prun_fn, self._session, handle, feed_dict,
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
983 except KeyError:
984 pass
--> 985 raise type(e)(node_def, op, message)
986
987 def _extend_graph(self):
NotFoundError: Tensor name "embedding_1/W" not found in checkpoint files imdb_lstm_SA.tflearn
[[Node: save_6/restore_slice_1 = RestoreSlice[dt=DT_FLOAT, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_6/Const_0, save_6/restore_slice_1/tensor_name, save_6/restore_slice_1/shape_and_slice)]]
Caused by op u'save_6/restore_slice_1', defined at:
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py", line 3, in <module>
app.launch_new_instance()
File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelapp.py", line 474, in start
ioloop.IOLoop.instance().start()
File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 276, in dispatcher
return self.dispatch_shell(stream, msg)
File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
handler(stream, idents, msg)
File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 390, in execute_request
user_expressions, allow_stdin)
File "/usr/local/lib/python2.7/dist-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python2.7/dist-packages/ipykernel/zmqshell.py", line 501, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2821, in run_ast_nodes
if self.run_code(code, result):
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-8-126d1ae60989>", line 10, in <module>
model = tflearn.DNN(net, tensorboard_verbose=0)
File "/usr/local/lib/python2.7/dist-packages/tflearn/models/dnn.py", line 63, in __init__
best_val_accuracy=best_val_accuracy)
File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.py", line 152, in __init__
keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 986, in __init__
self.build()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1015, in build
restore_sequentially=self._restore_sequentially)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 620, in build
restore_sequentially, reshape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 357, in _AddRestoreOps
tensors = self.restore_op(filename_tensor, saveable, preferred_shard)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 270, in restore_op
preferred_shard=preferred_shard))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/io_ops.py", line 204, in _restore_slice
preferred_shard, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_io_ops.py", line 359, in _restore_slice
preferred_shard=preferred_shard, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
self._traceback = _extract_stack()
NotFoundError (see above for traceback): Tensor name "embedding_1/W" not found in checkpoint files imdb_lstm_SA.tflearn
[[Node: save_6/restore_slice_1 = RestoreSlice[dt=DT_FLOAT, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_6/Const_0, save_6/restore_slice_1/tensor_name, save_6/restore_slice_1/shape_and_slice)]]
UPDATE_1
If I restart my python kernel in IPython and merely run the load-part code, it works fine. And if I run the same piece of code again (just like running two times sequencially in a single run), it complains not-found-tensor-name-error. Is there anything in network or model of tflearn that is not idempotent?
Anyone could help me out? Thanks!
Solved. add tf.reset_default_graph() before building the net and model.
Reference:
http://stackoverflow.com/questions/33765336/remove-nodes-from-graph-or-reset-entire-default-graph
https://github.com/tflearn/tflearn/blob/master/examples/basics/weights_loading_scope.py
Check whether the parameters in checkpoint are same as the load file.
Solved. add
tf.reset_default_graph()before building the net and model.Reference:
http://stackoverflow.com/questions/33765336/remove-nodes-from-graph-or-reset-entire-default-graph
https://github.com/tflearn/tflearn/blob/master/examples/basics/weights_loading_scope.py
Hi, I tried your method, but it just jumped from not finding "Conv2d_10/W" to "Conv2D_5/W". And I'm stuck again. Pls help.
Most helpful comment
Solved. add
tf.reset_default_graph()before building the net and model.Reference:
http://stackoverflow.com/questions/33765336/remove-nodes-from-graph-or-reset-entire-default-graph
https://github.com/tflearn/tflearn/blob/master/examples/basics/weights_loading_scope.py