如何从 C++ 中的 protobuf 执行 TensorFlow 图?
How can I execute a TensorFlow graph from a protobuf in C++?
我得到了一个简单的代码形式教程并将其输出到.pb文件如下:
mnist_softmax_train.py
x = tf.placeholder("float", shape=[None, 784], name='input_x')
y_ = tf.placeholder("float", shape=[None, 10], name='input_y')
W = tf.Variable(tf.zeros([784, 10]), name='W')
b = tf.Variable(tf.zeros([10]), name='b')
tf.initialize_all_variables().run()
y = tf.nn.softmax(tf.matmul(x,W)+b, name='softmax')
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy, name='train_step')
train_step.run(feed_dict={x:input_x, y_:input_y})
在 C++ 中,我加载相同的图形,并输入假数据进行测试:
Tensor input_x(DT_FLOAT, TensorShape({10,784}));
Tensor input_y(DT_FLOAT, TensorShape({10,10}));
Tensor W(DT_FLOAT, TensorShape({784,10}));
Tensor b(DT_FLOAT, TensorShape({10,10}));
Tensor input_test_x(DT_FLOAT, TensorShape({1,784}));
for(int i=0;i<10;i++){
for(int j=0;j<10;j++)
input_x.matrix<float>()(i,i+j) = 1.0;
input_y.matrix<float>()(i,i) = 1.0;
input_test_x.matrix<float>()(0,i) = 1.0;
}
std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
{ "input_x", input_x },
{ "input_y", input_y },
{ "W", W },
{ "b", b },
{ "input_test_x", input_test_x },
};
std::vector<tensorflow::Tensor> outputs;
status = session->Run(inputs, {}, {"train_step"}, &outputs);
std::cout << outputs[0].DebugString() << "\n";
然而,这失败并出现错误:
Invalid argument: Input 0 of node train_step/update_W/ApplyGradientDescent was passed float from _recv_W_0:0 incompatible with expected float_ref.
图表 运行 在 Python 中正确。我如何在 C++ 中正确 运行 它?
这里的问题是您正在 运行 宁 "train_step"
目标,它执行的工作比推理多得多。特别是,它尝试用梯度下降步骤的结果更新变量 W
和 b
。错误信息
Invalid argument: Input 0 of node train_step/update_W/ApplyGradientDescent was passed float from _recv_W_0:0 incompatible with expected float_ref.
...意味着您尝试 运行 ("train_step/update_W/ApplyGradientDescent"
) 的节点之一需要一个可变输入(类型为 float_ref
),但它得到一个不可变输入(具有键入 float
) 因为输入了值。
有(至少)两种可能的解决方案:
如果您只想查看给定输入和给定权重的预测,请在对 Session::Run()
的调用中获取 "softmax:0"
而不是 "train_step"
。
如果要用C++进行训练,不要喂W
和b
,而是给那些变量赋值,然后继续执行"train_step"
.当您在 Python 中构建图形时,您可能会发现创建 tf.train.Saver
更容易,然后调用它生成的操作以从检查点保存和恢复值。
我得到了一个简单的代码形式教程并将其输出到.pb文件如下:
mnist_softmax_train.py
x = tf.placeholder("float", shape=[None, 784], name='input_x')
y_ = tf.placeholder("float", shape=[None, 10], name='input_y')
W = tf.Variable(tf.zeros([784, 10]), name='W')
b = tf.Variable(tf.zeros([10]), name='b')
tf.initialize_all_variables().run()
y = tf.nn.softmax(tf.matmul(x,W)+b, name='softmax')
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy, name='train_step')
train_step.run(feed_dict={x:input_x, y_:input_y})
在 C++ 中,我加载相同的图形,并输入假数据进行测试:
Tensor input_x(DT_FLOAT, TensorShape({10,784}));
Tensor input_y(DT_FLOAT, TensorShape({10,10}));
Tensor W(DT_FLOAT, TensorShape({784,10}));
Tensor b(DT_FLOAT, TensorShape({10,10}));
Tensor input_test_x(DT_FLOAT, TensorShape({1,784}));
for(int i=0;i<10;i++){
for(int j=0;j<10;j++)
input_x.matrix<float>()(i,i+j) = 1.0;
input_y.matrix<float>()(i,i) = 1.0;
input_test_x.matrix<float>()(0,i) = 1.0;
}
std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
{ "input_x", input_x },
{ "input_y", input_y },
{ "W", W },
{ "b", b },
{ "input_test_x", input_test_x },
};
std::vector<tensorflow::Tensor> outputs;
status = session->Run(inputs, {}, {"train_step"}, &outputs);
std::cout << outputs[0].DebugString() << "\n";
然而,这失败并出现错误:
Invalid argument: Input 0 of node train_step/update_W/ApplyGradientDescent was passed float from _recv_W_0:0 incompatible with expected float_ref.
图表 运行 在 Python 中正确。我如何在 C++ 中正确 运行 它?
这里的问题是您正在 运行 宁 "train_step"
目标,它执行的工作比推理多得多。特别是,它尝试用梯度下降步骤的结果更新变量 W
和 b
。错误信息
Invalid argument: Input 0 of node train_step/update_W/ApplyGradientDescent was passed float from _recv_W_0:0 incompatible with expected float_ref.
...意味着您尝试 运行 ("train_step/update_W/ApplyGradientDescent"
) 的节点之一需要一个可变输入(类型为 float_ref
),但它得到一个不可变输入(具有键入 float
) 因为输入了值。
有(至少)两种可能的解决方案:
如果您只想查看给定输入和给定权重的预测,请在对
Session::Run()
的调用中获取"softmax:0"
而不是"train_step"
。如果要用C++进行训练,不要喂
W
和b
,而是给那些变量赋值,然后继续执行"train_step"
.当您在 Python 中构建图形时,您可能会发现创建tf.train.Saver
更容易,然后调用它生成的操作以从检查点保存和恢复值。