简体   繁体   English

TFLearn加载经过训练的模型时引发错误

[英]TFLearn throws error while loading trained model

I am new to TFLearn and Tensorflow and I try to learn about them by making neural nets about any task. 我是TFLearn和Tensorflow的新手,我尝试通过制作有关任何任务的神经网络来了解它们。 I successfully trained a model to do sentiment analysis on tweets. 我成功地训练了一个模型来对推文进行情感分析。 I used model.save(path) to save my model. 我使用model.save(path)保存我的模型。

Now, when I run model.load(path) , it throws an error. 现在,当我运行model.load(path) ,它将引发错误。 Specifically, the code that loads the model from a file is this: 具体来说,从文件加载模型的代码是这样的:

def loadModel(model = None, max_size=40, path="model.save"):
    # Load the saved model from the specified path
    global VOCABULARY_SIZE

    if model == None:
        plh = tf.placeholder(tf.float32, shape = (None, max_size)) # Creates a Placeholder that is used in buildNetwork() to create the input layer.
        model = buildNetwork(plh, dictionary_size=len(VOCABULARY_SIZE))

    # At this point, the model has the same architecture as the trained one.

    model = model.load(path)
    return model

And my error is this: 我的错误是这样的:

NotFoundError: Tensor name "Embedding_1/W" not found in checkpoint files model.save
     [[Node: save_11/restore_slice_1 = RestoreSlice[dt=DT_FLOAT, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_11/Const_0, save_11/restore_slice_1/tensor_name, save_11/restore_slice_1/shape_and_slice)]]
Caused by op u'save_11/restore_slice_1', defined at:
  File "/opt/local/bin/ipython", line 5, in <module>
    start_ipython()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/__init__.py", line 119, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 348, in start
    self.shell.mainloop()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 440, in mainloop
    self.interact()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 431, in interact
    self.run_cell(code, store_history=True)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
    if self.run_code(code, result):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-7-2cfc4af80f36>", line 1, in <module>
    stm_a.loadModelandVocabulary()
  File "stm_a.py", line 42, in loadModelandVocabulary
    model = buildNetwork(plh, dictionary_size=len(vocabulary_object.vocabulary_))
  File "stm_a.py", line 98, in buildNetwork
    return tflearn.DNN(g, checkpoint_path = checkpoint_path)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tflearn/models/dnn.py", line 63, in __init__
    best_val_accuracy=best_val_accuracy)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tflearn/helpers/trainer.py", line 142, in __init__
    keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tensorflow/python/training/saver.py", line 861, in __init__
    restore_sequentially=restore_sequentially)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tensorflow/python/training/saver.py", line 519, in build
    filename_tensor, vars_to_save, restore_sequentially, reshape)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tensorflow/python/training/saver.py", line 272, in _AddRestoreOps
    values = self.restore_op(filename_tensor, vs, preferred_shard)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tensorflow/python/training/saver.py", line 187, in restore_op
    preferred_shard=preferred_shard)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tensorflow/python/ops/io_ops.py", line 203, in _restore_slice
    preferred_shard, name=name)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tensorflow/python/ops/gen_io_ops.py", line 359, in _restore_slice
    preferred_shard=preferred_shard, name=name)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tensorflow/python/framework/op_def_library.py", line 703, in apply_op
    op_def=op_def)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tensorflow/python/framework/ops.py", line 2317, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python/tensorflow/python/framework/ops.py", line 1239, in __init__
    self._traceback = _extract_stack()

I see something is wrong with my embedding layer which follows my input layer. 我发现输入层后面的嵌入层出了问题。 I use these (default) parameters for that layer: trainable=True, restore=True, reuse=False . 我在该层使用了这些(默认)参数: trainable=True, restore=True, reuse=False

I am new to this, so I don't know how to debug TFLearn and Tensorflow errors properly. 我是新手,所以我不知道如何正确调试TFLearn和Tensorflow错误。

Any help is appreciated! 任何帮助表示赞赏!

I'm not sure what your problem is, but model.load() doesn't return anything. 我不确定您的问题是什么,但是model.load()不返回任何内容。 Here's example usage from tflearn: 这是tflearn的示例用法:

model = DNN(network)
model.load('model.tflearn')
model.predict(X)

You're assigning model to None . 您正在将model分配给None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM