参数无效:在图形

时间:2018-11-28 13:58:41

标签: python c++ tensorflow keras

我正在尝试使用C ++ API在Tensorflow中运行从Keras导出的模型。该模型已正确导出和导入,但是当我尝试在C ++中运行会话时,我在Run的{​​{1}}成员中传递的变量名称被修改,从而导致错误:

tensorflow::Session

这是我在Keras中建立的模型:

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

使用S = Input(shape=[state_size], name='Input') h0 = Dense(self.HIDDEN1_UNITS, name='Hidden1', kernel_initializer=keras.initializers.RandomUniform())(S) h0_a = Activation('relu')(h0) h1 = Dense(self.HIDDEN2_UNITS, name='Hidden2', kernel_initializer=keras.initializers.RandomUniform())(h0_a) h1_a = Activation('relu')(h1) output = Dense(1)(h1_a) output_a = Activation('tanh', name='Output0')(output) model = Model(S,output_a) 导出模型 和tensorflow.python.framework.graph_util,遵循https://github.com/amir-abdi/keras_to_tensorflow

中实施的方法

然后使用C ++ API将模型导入Tensorflow:

tensorflow.python.framework.graph_io

该图似乎已成功导入,实际上,当我在Tensorflow中打印图中包含的节点名称时,会得到以下信息:

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

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

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);

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

此外,我已经尝试过使用Tensorflow构建的简单图形进行上述操作,并且C ++上的会话运行没有问题。这是起作用的图:

Invalid argument: Tensor Output0:0, specified in either feed_devices or fetch_devices was not found in the Graph
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()

为什么在第二种情况下我设法运行该会话,而在第一种情况下却遇到该错误?

Keras版本为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); Tensorflow版本为2.2.2

任何帮助将不胜感激。

0 个答案:

没有答案