Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a feed_dict in c++ for Tensorflow models

This question is related to this one: Export Tensorflow graphs from Python for use in C++

I'm trying to export a Tensorflow model from Python to C++. The problem is, my neural net starts with a placeholder to receive input, which requires a feed_dict. I cannot find any c++ API to supply a feed_dict for my model. What can I do?

If there's no API for supplying feed_dicts, how should I change my model so that it can be trained and exported for c++ purposes without placeholders?

like image 530
Sander Avatar asked Apr 22 '16 23:04

Sander


1 Answers

The tensorflow::Session::Run() method is the C++ equivalent of the Python tf.Session.run() method, and it supports feeding tensors using the inputs argument. Like so many things in C++ versus Python, it's just a little more tricky to use (and in this case it looks like the documentation is a bit poorer...).

The inputs argument has type const std::vector<std::pair<string, Tensor>>&. Let's break this down:

  • Each element of inputs corresponds to a single tensor (such as a placeholder) that you want to feed in the Run() call. An element has type std::pair<string, Tensor>.

  • The first element of the std::pair<string, Tensor> is the name of the tensor in the graph that you want to feed. For example, let's say in Python you had:

    p = tf.placeholder(..., name="placeholder")
    # ...
    sess.run(..., feed_dict={p: ...})
    

    ...then in C++ the first element of the pair would be the value of p.name, which in this case would be "placeholder:0"

  • The second element of the std::pair<string, Tensor> is the value that you want to feed, as a tensorflow::Tensor object. You have to build this yourself in C++, and it's a bit more complicated that defining a Numpy array or a Python object, but here's an example of how to specify a 2 x 2 matrix:

    using tensorflow::Tensor;
    using tensorflow::TensorShape;
    
    Tensor t(DT_FLOAT, TensorShape({2, 2}));
    auto t_matrix = t.matrix<float>();
    t_matrix(0, 0) = 1.0;
    t_matrix(0, 1) = 0.0;
    t_matrix(1, 0) = 0.0;
    t_matrix(1, 1) = 1.0;
    

    ...and you can then pass t as the second element of the pair.

like image 83
mrry Avatar answered Oct 15 '22 09:10

mrry