小编Leo*_*Lin的帖子

如何从C++中的protobuf执行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 }, …
Run Code Online (Sandbox Code Playgroud)

protocol-buffers tensorflow

6
推荐指数
1
解决办法
3217
查看次数

标签 统计

protocol-buffers ×1

tensorflow ×1