【问题标题】:How to Save and Restore a Neural Network (ClientSession) with a purly C++ API?如何使用纯 C++ API 保存和恢复神经网络 (ClientSession)?
【发布时间】:2019-04-15 09:32:23
【问题描述】:

我想仅使用 C/C++ 接口保存经过训练的 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.

提前致谢, 马丁

【问题讨论】:

  • 可惜我没有“重载函数””是什么意思?在链接的问题中,没有提到这样的事情。
  • 嗨 Acorn,感谢您的回复,我尝试使用上面链接中建议的“模板”代码。但是我的版本没有适合代码的重载函数,并且我通过更改函数调用中的元素进行的每个“测试”都不起作用。特别是我的释放接缝的“sess->Run(”与另一个聊天中讨论的不同。
  • 我发现“freeze_saved_model.cc”中有一些可能对我有用的函数。但这只是为更高版本 r1.13 准备的,我根本无法创建高于 r1.9 的有效 Windows/C++/VisualStudio 解决方案......我完全按照tensorflow.org/install/source_windows 的说明进行操作,但我可以不要管理它让它运行。我已经看到,在 r1.9 版本中有一个名为“freeze_saved_model_test.cc”的文件,其中包含我需要的功能。有谁知道如何使用测试文件?

标签: c++ tensorflow


【解决方案1】:

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

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

这里,如何恢复图形:

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

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

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2012-08-05
    • 2012-10-18
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多