Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[4] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...