7

I got a simple code form tutorial and output it to .pb file as below:

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

In C++, I load the same graph, and feed in fake data for testing:

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

However, this fails with the error:

Invalid argument: Input 0 of node train_step/update_W/ApplyGradientDescent was passed float from _recv_W_0:0 incompatible with expected float_ref.

The graph runs correctly in Python. How can I run it correctly in C++?

mrry
  • 125,488
  • 26
  • 399
  • 400
Leo Lin
  • 73
  • 1
  • 4
  • 1
    What error do you get when you try to run this? – mrry Dec 18 '15 at 17:18
  • Sorry, I had modified my code. If I run the train_step graph, there will be a error message "Invalid argument: Input 0 of node train_step/update_W/ApplyGradientDescent was passed float from _recv_W_0:0 incompatible with expected float_ref." – Leo Lin Dec 21 '15 at 09:34
  • [Loading a TensorFlow graph with the C++ API](https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f) – Brent Bradburn Oct 01 '16 at 01:47

1 Answers1

6

The issue here is that you are running the "train_step" target, which performs much more work than just inference. In particular, it attempts to update the variables W and b with the result of the gradient descent step. The error message

Invalid argument: Input 0 of node train_step/update_W/ApplyGradientDescent was passed float from _recv_W_0:0 incompatible with expected float_ref.

...means that one of the nodes you attempted to run ("train_step/update_W/ApplyGradientDescent") expected a mutable input (with type float_ref) but it got an immutable input (with type float) because the value was fed in.

There are (at least) two possible solutions:

  1. If you only want to see predictions for a given input and given weights, fetch "softmax:0" instead of "train_step" in the call to Session::Run().

  2. If you want to perform training in C++, do not feed W and b, but instead assign values to those variables, then continue to execute "train_step". You may find it easier to create a tf.train.Saver when you build the graph in Python, and then invoke the operations that it produces to save and restore values from a checkpoint.

mrry
  • 125,488
  • 26
  • 399
  • 400
  • Thanks so much! But what do you mean "do not feed W and b"? Can't I create a Tensor for W or b as input in C++ ? – Leo Lin Dec 22 '15 at 03:08
  • The issue is that `W` and `b` are TensorFlow *variables*, which means that feeding them isn't supported. Instead, to set their value, you should run an Assign op that assigns a value to them. One way you could do this would be to add some ops to your graph (in Python) like `W.assign(tf.placeholder(tf.float32, name="w_placeholder"), name="w_assign")`, then you could call `Session::Run()` passing `"w_assign"` as the target, and `"w_placeholder:0"` as the tensor to be fed (and similarly for `b`). – mrry Dec 22 '15 at 05:58
  • Thanks again, by the way, what is the correct method to save training data in .pb file which I can use later in C++? I use tf.train.write_graph() but it seems that the protobuf didn't hold training data. – Leo Lin Dec 22 '15 at 09:49
  • We're still developing support for this at the moment, but [my answer to this question](http://stackoverflow.com/questions/34343259/is-there-an-example-on-how-to-generate-protobuf-files-holding-trained-tensorflow/34343517#34343517) has a rough set of steps for generating a portable proto. – mrry Dec 22 '15 at 15:46
  • So, if I understood correctly - there is no way to train the graph from C++? I'm asking this because sometimes feeding data through Python is not practical (imagine custom storage for very large voice data, written on C++/C#). Or there is a way? – Maksym Diachenko Dec 22 '15 at 20:54
  • No, that's not correct. You *can* train the graph from C++. In that case you would certainly not feed the variables that you are trying to train. Instead, you'd add a `Session::Run()` call to run their initializers (i.e., in Python, the `tf.initialize_all_variables()` operation), and then run the `train_step` operation in a loop. – mrry Dec 22 '15 at 20:57