【问题标题】:Failed to run the model using metagraph tensorflow使用元图张量流运行模型失败
【发布时间】:2019-01-31 13:34:56
【问题描述】:

我已经尝试在程序中加载模型,以便可以实时运行它。
以下是我正在尝试的方式:
储蓄者 =

tf.train.import_meta_graph(tf.train.latest_checkpoint(model_path)+".meta")
graph = tf.get_default_graph()
outputs = graph.get_operation_by_name('output')
native = graph.get_operation_by_name('input')
sess  = tf.Session()
sess.run(tf.global_variables_initializer())   
sess.run(tf.local_variables_initializer()) 
if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
    saver.restore(sess, tf.train.latest_checkpoint(model_path))
    print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")  

我试图得到输出:

y_test_output= sess.run(outputs, feed_dict={native: x_test})

我收到以下错误:

Traceback (most recent call last):
  File "testing_reality.py", line 90, in <module>
    main()
  File "testing_reality.py", line 62, in main
    handle_client_connection(client_sock)
  File "testing_reality.py", line 45, in handle_client_connection
    y_train_pred= sess.run(outputs, feed_dict={native: x_test})
  File "C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
    run_metadata_ptr)
  File "C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1095, in _run
    'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a Operation into a Tensor.

请让我知道我在这种情况下缺少什么。
在 Windows 10 上使用最新版本的 Tensorflow '1.12.0'。

【问题讨论】:

    标签: python python-3.x tensorflow machine-learning


    【解决方案1】:

    feed_dict 中的 keys 必须是占位符。该错误表明您使用Operation 作为feed_dict 中的键。如果input 变量确实是placeholder,则按以下方式加载它:

    native = graph.get_tensor_by_name("input:0")
    

    outputs 也是如此:

    outputs = graph.get_tensor_by_name("output:0")
    

    【讨论】:

    • 感谢您让我了解错误所在。你能告诉我如何改进它吗?是否可以从 Metagraph 中获取输入变量?
    • 我在训练时将 X 作为输入。 X = tf.placeholder(tf.float32, [None, n_steps, n_inputs],name="input") 现在在恢复模型时,我正在尝试我在问题中提到的内容。
    • 我再次尝试时得到了这个:&gt;&gt;&gt; native = graph.get_operation_by_name('input') &gt;&gt;&gt; native &lt;tf.Operation 'input' type=Placeholder&gt;。你看它是占位符。
    • 你跑native = graph.get_tensor_by_name("input")了吗?
    • 谢谢你成功了。我用native = graph.get_tensor_by_name("input:0")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多