I have read the discussion in http://discuss.ai.google.dev/t/tape-gradient-c/3777, but still don’t know how to get gradients from the C++ API in my case. Briefly, I have a trained model saved from the python code, and then load it in C++ by
int main() {
// load model
const std::string model_dir{"model/"};
tensorflow::SavedModelBundleLite bundle;
auto status = tensorflow::LoadSavedModel(
tensorflow::SessionOptions(), tensorflow::RunOptions(), model_dir,
{tensorflow::kSavedModelTagServe}, &bundle);
if (status.ok()) {
std::cout << "Load OK\n";
inspect_model(bundle);
// initialize the input tensor
const tensorflow::Tensor input_tensor = vector_to_tensor_2d(read_gzipped_csv("input.csv.gz"));
const auto session = bundle.GetSession();
std::vector<tensorflow::Tensor> output;
// run the model over input data
status = session->Run(
{{"serving_default_layer_0:0", input_tensor}},
{"StatefulPartitionedCall:0"}, {}, &output);
if (status.ok()) {
std::cout << "Compute OK\n";
// write the output
write_2d_tensor_to_file("output.csv", output[0]);
} else {
std::cout << "Compute error: " << status.error_message() << "\n";
}
} else {
std::cout << "Load error: " << status.error_message() << "\n";
}
return 0;
}
Is there any way to obtain the gradients of the output
with respect to input_tensor
?
The python code should be
# load data
data = load_data('input.csv.gz')
# load model
model = load_model('model/')
# encode data
x = tf.Variable(tf.convert_to_tensor(data.to_numpy()))
with tf.GradientTape() as tape:
tape.watch(x)
nn_output_data = model(x)
grad = tape.gradient(nn_output_data, x)
# write to output
Is there any equivalent API in C++? I have read the API design in community/rfcs/20201201-cpp-gradients.md at master · tensorflow/community · GitHub but have no idea how to use it. For instance, I have no clue about converting between tensorflow::Tensor
and AbstractTensorHandle
.