Leo*_*Lin 6 protocol-buffers tensorflow
我得到了一个简单的代码表格教程并将其输出到.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})
Run Code Online (Sandbox Code Playgroud)
在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";
Run Code Online (Sandbox Code Playgroud)
但是,这失败并出现错误:
Invalid argument: Input 0 of node train_step/update_W/ApplyGradientDescent was passed float from _recv_W_0:0 incompatible with expected float_ref.
Run Code Online (Sandbox Code Playgroud)
该图在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.
Run Code Online (Sandbox Code Playgroud)
...意味着您尝试运行的节点之一("train_step/update_W/ApplyGradientDescent")期望一个可变输入(带有类型float_ref),但它有一个不可变输入(带有类型float),因为该值已被输入.
有(至少)两种可能的解决方案:
如果您只想查看给定输入和给定权重的预测,请提取"softmax:0"而不是"train_step"在调用中Session::Run().
如果要执行C++培训,不喂W和b,而是赋值的变量,然后继续执行"train_step".您可能会发现tf.train.Saver在Python中构建图形时更容易创建,然后调用它生成的操作以从检查点保存和恢复值.
| 归档时间: |
|
| 查看次数: |
3217 次 |
| 最近记录: |