无法使用metagraph运行TensorFlow模型

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

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必须是占位符。错误显示您在feed_dict中使用了Operation作为键。如果input变量确实是placeholder,那么请按以下方式加载它:

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

对于outputs也是如此:

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

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注