python - 使用元图 tensorflow 运行模型失败

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

我尝试在程序中加载模型,以便实时运行它。
以下是我正在尝试的方法:
保护者=

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”。

最佳答案

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

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

输出也是如此:

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

关于python - 使用元图 tensorflow 运行模型失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54461795/

相关文章:

Python, 'open' 和 'with open' 之间的区别

python - 基于用户定义的类创建动态 ABC 类

python - 在 Python turtle 中查找光标的当前位置

javascript - tensorflow js RGB从输入img到Lab

python-3.x - 函数不起作用、语法错误等等

python - 如何在 Tensorflow 中计算 Spearman 相关性

Python HL7 Listener 套接字消息确认

python - 使用python对词袋模型进行简单的k均值聚类

python - DeepFace verify() 可以接受图像数组或 PIL Image 对象吗?

创建从字典键到它的值的所有组合的 Pythonic 方法