0

I am very new to TensorFlow C++ API and trying to just build a very simple graph in Python and load/test it in C++ API. Here is the python code to create the graph:

with tf.Session() as sess: 
   a = tf.placeholder(tf.float32, shape=[2,2], name='a')
   b = tf.placeholder(tf.float32, shape=[2,2], name='b')
   c = tf.matmul(a, b, name="c")

   sess.run(tf.initialize_all_variables())

tf.train.write_graph(sess.graph_def, 'models/', 'graph.pb', as_text=False)

and here is the C code to load and run the graph:

Tensor a(DT_FLOAT, TensorShape({2,2}));

Tensor b(DT_FLOAT, TensorShape({2,2}));
std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
  { "a", a },
  { "b", b },
};

std::vector<tensorflow::Tensor> outputs;

status = session->Run(inputs, {"c"}, {}, &outputs);

However I get this error message:

./tensorflow/core/framework/tensor.h:500] Check failed: 1 == NumElements() (1 vs. 4)Must have a one element tensor

What could be the problem? I noticed if I define my tensors in he python and C++ as [1,1], it goes through without any problem!

1 Answer 1

1

Take a look at here, I tried to explain everything completely To summarize you should use 'freeze_graph' file to make trained variables constant and then import it in your C++ file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.