无效参数:在图形中找不到在源设备或获取设备中指定的参数

2024-04-24 14:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

<>我尝试使用C++ API运行Keras在TysFooFrand中导出的模型。该模型是正确导出和导入的,但是当我尝试在C++中运行会话时,我在^ {< CD1>}成员中的变量名为{{CD2>}得到修改,导致错误:

Invalid argument: Tensor Output0:0, specified in either feed_devices or fetch_devices was not found in the Graph

这是我在Keras建立的模型:

^{pr2}$

使用tensorflow.python.framework.graph_util导出模型 和tensorflow.python.framework.graph_io,遵循https://github.com/amir-abdi/keras_to_tensorflow中实现的方法

模型,然后使用C++ API:T/P

ParseProtoUnlimited(&graph_def, <graph_protoprotobuffer>, <graph_size> );

图似乎导入成功,事实上,当我在Tensorflow中打印图形中包含的节点名称时,我得到以下结果:

Names : Input_5
Names : Hidden1_5/kernel
Names : Hidden1_5/kernel/read
Names : Hidden1_5/bias
Names : Hidden1_5/bias/read
Names : Hidden1_5/MatMul
Names : Hidden1_5/BiasAdd
Names : activation_5_1/Relu
Names : Hidden2_5/kernel
Names : Hidden2_5/kernel/read
Names : Hidden2_5/bias
Names : Hidden2_5/bias/read
Names : Hidden2_5/MatMul
Names : Hidden2_5/BiasAdd
Names : activation_6_1/Relu
Names : dense_3_1/kernel
Names : dense_3_1/kernel/read
Names : dense_3_1/bias
Names : dense_3_1/bias/read
Names : dense_3_1/MatMul
Names : dense_3_1/BiasAdd
Names : Output_5/Tanh
Names : output0

现在,如果要尝试运行会话,请执行以下操作:

Tensor input(DT_FLOAT, TensorShape( { 4 }));
...
[init tensor]
...
session->Run({{"Input_5", input}}, {"Output0"}, {},
                &outputs);

但应用程序抛出以下错误:

Invalid argument: Tensor Output0:0, specified in either feed_devices or fetch_devices was not found in the Graph

为什么函数Run session将:0附加到我要检索的输出张量?在

也曾尝试过用Tensorflow构建的一个简单的图形来完成上述操作,C++上的会话没有问题。以下是有效的图表:

with tf.Session() as sess:
    a = tf.Variable(5.0, name='a')
    b = tf.Variable(6.0, name='b')
    c = tf.multiply(a, b, name="c")
    sess.run(tf.global_variables_initializer())
    exported = sess.graph_def.SerializeToString()
    size = sess.graph_def.ByteSize()
Tensor a(DT_FLOAT, TensorShape());
a.scalar<float>()() = 5.0;

Tensor b(DT_FLOAT, TensorShape());
b.scalar<float>()() = 6.0;

std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
                { "a", a }, { "b", b }, };

std::vector<tensorflow::Tensor> outputs;
Run the session, evaluating our "c" operation from the graph
status = session->Run(inputs, { "c" }, { }, &outputs);

为什么在第二种情况下我设法运行会话,而在第一种情况下我得到了那个错误?在

Keras版本是2.2.2 Tensorflow版本是1.11.0

任何帮助都将不胜感激。在


Tags: thein模型readnamestftensorflowkernel