I built the C API by building the libtensorflow.so target. I want to download a pre-prepared model and make a conclusion to it in order to make predictions. I was told that I can do this by including the header file "c_api.h" (along with copying this file plus "libtensorflow.so" to the appropriate location), but I was not lucky to find examples on the Internet. All I could find were examples that use the Bazel build system, while I want to use a different build system and use TensorFlow as a library. Can someone help me with an example on how to import: a) a metagraph file; b) a protobuf graph file plus a checkpoint file to make predictions? C ++ equivalent of Python file below and built with g ++?
import tensorflow as tf
import numpy as np
with tf.Session() as sess:
saver = tf.train.import_meta_graph('./metagraph.meta')
saver.restore(sess, './checkpoint.ckpt')
x = tf.get_collection("x")[0]
yhat = tf.get_collection("yhat")[0]
print sess.run(yhat, feed_dict={x : np.array([[2, 3], [4, 5]])})
Thank Advance
ps: For completeness, I did the following to create the files:
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32, shape=[None, 2], name='x')
tf.add_to_collection("x", x)
y = tf.placeholder(tf.float32, shape=[None, 1], name='y')
w = tf.Variable(np.array([[10.0], [100.0]]), dtype=tf.float32, name='w')
b = tf.Variable(0.0, dtype=tf.float32, name='b')
yhat = tf.add(tf.matmul(x, w), b)
tf.add_to_collection("yhat", yhat)
mse_loss = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(y, yhat))))
step_size = tf.constant(0.01)
optimizer = tf.train.GradientDescentOptimizer(step_size)
init_op = tf.initialize_all_variables()
train_op = optimizer.minimize(mse_loss)
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init_op)
for i in xrange(10000):
train_x = np.random.random([100, 2]) * 10
train_y = np.dot(train_x, np.array([[100.0], [10.0]])) + 1.0
sess.run(train_op, feed_dict={x : train_x, y : train_y})
print sess.run(w)
print sess.run(b)
saver.save(sess, './checkpoint.ckpt')
saver.export_meta_graph('./metagraph.meta')
tf.train.write_graph(sess.graph_def, './', 'graph')