从包含新操作的python中创建的pb graph用c++创建图形

2024-05-04 07:37:43 发布

您现在位置:Python中文网/ 问答频道 /正文

如何链接我的libtensorflow_抄送so库和我的自定义操作,这样我可以执行c++培训?我的自定义操作是用c++编写的,使用它的图形是用python计算的。我可以加载pb图形,但不能创建图形。在

我用python创建了一个图形,并用write-gugraph保存为protobuf文件。我的图使用了一个自定义操作,其内核是用c++编写的,并在https://www.tensorflow.org/versions/master/extend/adding_an_op#compile_the_op_using_your_system_compiler_tensorflow_binary_installation中注册。我想在c++中加载图形以进行培训,该图形在没有警告的情况下加载,但是对于“session->Create(graph_def)”,我得到一个错误:

Non-OK-status: session->Create(graph_def) status: Not found: 
Op type not registered 'MyOp' in binary running on user-linux. 
Make sure the Op and Kernel are registered in the binary running in this process. 
Note that if you are loading a saved graph which used ops from tf.contrib,
accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph,
as contrib ops are lazily registered when the module is first accessed.

我没有用tf.contrib公司. 这是我的c++代码的一个简单版本:

^{pr2}$

Tags: thein图形sessiontftensorflowdefstatus
1条回答
网友
1楼 · 发布于 2024-05-04 07:37:43

似乎需要使用^{}中定义的TF_LoadLibrary来加载库。在^{}中,您有一个它如何工作的示例:

// Load the library.
TF_Status* status = TF_NewStatus();
TF_Library* lib =
    TF_LoadLibrary("tensorflow/c/test_op.so", status);
TF_Code code = TF_GetCode(status);
string status_msg(TF_Message(status));
TF_DeleteStatus(status);
ASSERT_EQ(TF_OK, code) << status_msg;

函数加载库并注册它在TensorFlow运行时中定义的操作和内核。在


编辑:

从技术上讲,加载库的重要函数是tensorflow::LoadLibrary,它在^{}中定义。但是,这个函数1)似乎没有在任何头中公开声明(实际上,^{}包含此函数的声明供内部使用)2)有相当模糊的参数。就我所见,C API似乎是目前在C++程序中加载库的一种更简单的方法,即使你必须小心地适当地破坏对象(即调用^ {< CD7}})。顺便说一句,似乎也没有卸载库的方法;可以销毁库句柄对象,但这不会卸载库。在

相关问题 更多 >