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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…