c++ - 如何使用纯 C++ API 保存和恢复神经网络 (ClientSession)?

标签 c++ tensorflow

我想仅使用 C/C++ 接口(interface)保存经过训练的 ClientSession(神经网络)。 (其中我在带有 Visual Studio 2017 的 Windows 10 上使用 tensorflow 1.9 版)

我找到了很多关于如何用 python 来做的信息,但我必须(因此可以)只使用 C++。 在 How to save and restore a TensorFlow graph and its state in C++? 我找到了一些建议,但不幸的是,我的版本中没有针对此示例代码的“重载函数”。

基本上这是我的代码:

Scope scope = Scope::NewRootScope();
...
ClientSession session(scope);
...
TF_CHECK_OK(session.Run({ w1, w2, w3, b1, b2, b3 }, nullptr));

for (int i = 0; i < 100; ++i) {
    TF_CHECK_OK(session.Run({ {x, x_data}, {y, y_data} }, { loss }, &outputs));
    TF_CHECK_OK(session.Run({ {x, x_data}, {y, y_data} }, { w1, w2, w3, b1, b2, b3, layer_3 }, nullptr));
}
...
// And now I would like to save the session, scope or graph such that I can use it in/on another program/function/system.

提前致谢, 马丁

最佳答案

终于可以回答我自己的问题了。我在网上找到了 2 个“示例代码”: 在这里,可以看到如何保存图表:

https://github.com/rockzhuang/tensorflow/commit/fb6a6f4e3dd6e663a14b672ab5c616d968d62bc5#diff-a46c94d7dc93c6f7a3b632dc14e46479

在这里,如何恢复图形:

https://github.com/rockzhuang/tensorflow/commit/5b24dfcf7e235bbffd2d60ccf1689b4ecb06c63b

很棒的例子!它对我有用,所以“有兴趣的读者”也可以尝试一下。

关于c++ - 如何使用纯 C++ API 保存和恢复神经网络 (ClientSession)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55686265/

相关文章:

c++ - 从文件中读取并在 C++ 中交换某些字符

c++ - 返回字符串及其 .c_str() 的生命周期

python - TensorFlow - 在读取和写入 TFRecords 文件时设置图像的形状?

python - 如何在Tensorflow 2中使用CRF层(使用tfa.text)?

android - Tensorflow 自定义 TFLite java.lang.NullPointerException : Cannot allocate memory for the interpreter

c++ - 违反封装?

c++ - 抛出未处理的异常 : read access violation. this->String was 0x1C6F112

c++ - 输入运算符时程序输出奇怪?

python - 亚马逊网络服务 : -bash: activate: No such file or directory

python - TensorFlow 中的 tf.argmax() 与 tf.arg_max()