-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add doc for how to use C-API. #6596
Conversation
6426060
to
06c774d
Compare
06c774d
to
635a69b
Compare
14a921f
to
b110b6b
Compare
fbebb39
to
d2e9da7
Compare
10ad036
to
702743e
Compare
702743e
to
b915fde
Compare
|
||
```python | ||
from paddle.utils.merge_model import merge_v2_model | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
56行多余的空行可以去掉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
```c | ||
// Initalize the PaddlePaddle runtime environment. | ||
char* argv[] = {"--use_gpu=False"}; | ||
CHECK(paddle_init(1, (char**)argv)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里能加个例子说明下,如果初始化参数有两个,应该怎么写么?比如
char* argv[] = {"--use_gpu=False", "--use_mkldnn=True"};
CHECK(paddle_init(2, (char**)argv));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. 这个例子已经补充上了。
这里我感觉其实还需要一个可以传递参数有哪些的列表和说明。我暂时也不太清楚所有可选参数。会再查看一下,补上这部分。
|
||
每一个 `gradient machine` 都会管理维护一份训练好的模型,模型可以通过以下两种方式获取: | ||
1. 从磁盘加载;这时`gradient machine`会独立拥有一份训练好的模型; | ||
1. 共享自其它`gradient machine`的模型;这种情况多出现在使用多线程预测时; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 从磁盘加载:(分号改成冒号)
- 共享自其它
gradient machine
的模型:(分号改成冒号)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
*注:本文档使用的示例任务手写数字识别不涉及一维整型数组作为输入,因此,本文档仅讨论二维稠密矩阵作为输入的情形。更多输入数据格式请参考输入/输出数据一节的内容。* | ||
|
||
这篇文档的之后部分会使用`argument`来**特指** PaddlePaddle C-API中神经网的一个输入/输出,使用`paddle_matrix`**特指**`argument`中用于存储数据的`Matrix`类的对象。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
神经网-》神经网络
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
1. 链接`libpaddle_capi_shared.so` 动态库 | ||
- 使用 PaddlePaddle C-API 开发预测程序链接`libpaddle_capi_shared.so`时,需注意: | ||
1. 如果编译时指定编译CPU版本,且使用`OpenBLAS`矩阵库,在使用CAPI开发预测程序时,只需要链接`libpaddle_capi_shared.so`这一个库。 | ||
1. 如果是用编译时指定CPU版本,且使用`MKL`矩阵库,由于`MKL`库有自己独立的动态库文件,在使用PaddlePaddle CAPI开发预测程序时,需要自己链接MKL链接库。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. 链接`libpaddle_capi_shared.so` 动态库 | ||
- 使用 PaddlePaddle C-API 开发预测程序链接`libpaddle_capi_shared.so`时,需注意: | ||
1. 如果编译时指定编译CPU版本,且使用`OpenBLAS`矩阵库,在使用CAPI开发预测程序时,只需要链接`libpaddle_capi_shared.so`这一个库。 | ||
1. 如果是用编译时指定CPU版本,且使用`MKL`矩阵库,由于`MKL`库有自己独立的动态库文件,在使用PaddlePaddle CAPI开发预测程序时,需要自己链接MKL链接库。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
矩阵库,都改成数学库。不仅仅是矩阵运算哈,还有向量等。全文同。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
2. 链接静态库 `libpaddle_capi_whole.a` | ||
- 使用PaddlePaddle C-API 开发预测程序链接`libpaddle_capi_whole.a`时,需注意: | ||
1. 需要指定`-Wl,--whole-archive`链接选项。 | ||
1. 需要显式地链接 `gflags`、`glog`、`libz`、`protobuf` 等第三方库,可在`INSTALL_PREFIX\third_party`下找到。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
INSTALL_PREFIX/third_party 斜杠方向反了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
doc/howto/usage/capi/index_cn.rst
Outdated
|
||
compile_paddle_lib.md | ||
organization_of_the_inputs.md | ||
a_simple_example.md |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要加后缀,不然识别不出来。
</table> | ||
</html> | ||
<br> | ||
### 输出数据 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- **注意**:以上两种方式只需选择其一即可。 | ||
- 调用 PaddlePaddle C-API 开发预测序 | ||
1. 初始化PaddlePaddle运行环境。 | ||
1. 创建神经网络的输入,组织输入数据。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加载模型
应该在组织输入数据
前面。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. 已经修改。
运行目录下的 `python mnist_v2.py` 可以使用 PaddlePaddle 内置的 [MNIST 数据集](http://yann.lecun.com/exdb/mnist/)进行训练。脚本中的模型定义了一个简单的含有[两个隐层的全连接网络](https://github.com/PaddlePaddle/book/blob/develop/02.recognize_digits/README.cn.md#softmax回归softmax-regression),网络接受一幅图片作为输入,将图片分类到 0 ~ 9 类别标签之一。训练好的模型默认保存在当前运行目录下的`models`目录中。下面,我们将调用 C-API 加载训练好的模型进行预测。 | ||
|
||
|
||
### 外部准备 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
外部准备
修改成准备预测模型
。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
### 使用流程 | ||
|
||
使用 C-API 分为:准备工作和预测程序开发两部分。 | ||
- 准备 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以考虑修改成准备预测模型
。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
#### step 2. 创建神经网络输入,组织输入数据 | ||
|
||
基本使用概念: | ||
- 在PaddlePaddle内部,神经网络中一个计算层的输入/输出被组织为一个 `Argument` 结构体,如果神经网络有多个输入或者多个输入,每一个输入/输入都会对应有自己的`Argument`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
多个输入或者多个输入
-> 多个输出
输入/输入
-> 输入/输出
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. 多谢指出
|
||
### 编写预测代码 | ||
|
||
#### step 1. 初始化及加载模型 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
初始化及加载模型
可以分成两个步骤。加载模型有多种方式,也可以在程序运行过程中再加载另外一个模型。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
每一个 `gradient machine` 都会管理维护一份训练好的模型,模型可以通过以下两种方式获取: | ||
1. 从磁盘加载;这时`gradient machine`会独立拥有一份训练好的模型; | ||
1. 共享自其它`gradient machine`的模型;这种情况多出现在使用多线程预测时; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...使用多线程预测时,可以通过多个线程共享同一个模型来减少内存开销。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. 已经修改。同时,我把这个tips先加入到注意事项中,暂时没有做更多的解释。不知是否合适。
|
||
### 使用流程 | ||
|
||
使用 C-API 分为:准备工作和预测程序开发两部分。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用 C-API 预测分为:准备预测模型和预测程序开发两部分。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
这里我们以手写数字识别任务为例,介绍如何使用 C-API 进行预测,完整代码请查看[此目录](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/capi/examples/model_inference/dense)。 | ||
|
||
运行目录下的 `python mnist_v2.py` 可以使用 PaddlePaddle 内置的 [MNIST 数据集](http://yann.lecun.com/exdb/mnist/)进行训练。脚本中的模型定义了一个简单的含有[两个隐层的全连接网络](https://github.com/PaddlePaddle/book/blob/develop/02.recognize_digits/README.cn.md#softmax回归softmax-regression),网络接受一幅图片作为输入,将图片分类到 0 ~ 9 类别标签之一。训练好的模型默认保存在当前运行目录下的`models`目录中。下面,我们将调用 C-API 加载训练好的模型进行预测。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段话也可以放到准备预测模型
里面吧。准备预测模型
首先是获得一个训练模型,然后再转换成预测模型。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有道理,已经修改。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已经按照大家的意见进行了修改。
|
||
### 使用流程 | ||
|
||
使用 C-API 分为:准备工作和预测程序开发两部分。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
### 使用流程 | ||
|
||
使用 C-API 分为:准备工作和预测程序开发两部分。 | ||
- 准备 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
- **注意**:以上两种方式只需选择其一即可。 | ||
- 调用 PaddlePaddle C-API 开发预测序 | ||
1. 初始化PaddlePaddle运行环境。 | ||
1. 创建神经网络的输入,组织输入数据。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. 已经修改。
|
||
这里我们以手写数字识别任务为例,介绍如何使用 C-API 进行预测,完整代码请查看[此目录](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/capi/examples/model_inference/dense)。 | ||
|
||
运行目录下的 `python mnist_v2.py` 可以使用 PaddlePaddle 内置的 [MNIST 数据集](http://yann.lecun.com/exdb/mnist/)进行训练。脚本中的模型定义了一个简单的含有[两个隐层的全连接网络](https://github.com/PaddlePaddle/book/blob/develop/02.recognize_digits/README.cn.md#softmax回归softmax-regression),网络接受一幅图片作为输入,将图片分类到 0 ~ 9 类别标签之一。训练好的模型默认保存在当前运行目录下的`models`目录中。下面,我们将调用 C-API 加载训练好的模型进行预测。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有道理,已经修改。
运行目录下的 `python mnist_v2.py` 可以使用 PaddlePaddle 内置的 [MNIST 数据集](http://yann.lecun.com/exdb/mnist/)进行训练。脚本中的模型定义了一个简单的含有[两个隐层的全连接网络](https://github.com/PaddlePaddle/book/blob/develop/02.recognize_digits/README.cn.md#softmax回归softmax-regression),网络接受一幅图片作为输入,将图片分类到 0 ~ 9 类别标签之一。训练好的模型默认保存在当前运行目录下的`models`目录中。下面,我们将调用 C-API 加载训练好的模型进行预测。 | ||
|
||
|
||
### 外部准备 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
├── ...... | ||
``` | ||
|
||
### 链接方式说明 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
│ ├── libpaddle_capi_shared.dylib | ||
│ └── libpaddle_capi_whole.a | ||
└── third_party | ||
├── ...... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
1. 链接`libpaddle_capi_shared.so` 动态库 | ||
- 使用 PaddlePaddle C-API 开发预测程序链接`libpaddle_capi_shared.so`时,需注意: | ||
1. 如果编译时指定编译CPU版本,且使用`OpenBLAS`矩阵库,在使用CAPI开发预测程序时,只需要链接`libpaddle_capi_shared.so`这一个库。 | ||
1. 如果是用编译时指定CPU版本,且使用`MKL`矩阵库,由于`MKL`库有自己独立的动态库文件,在使用PaddlePaddle CAPI开发预测程序时,需要自己链接MKL链接库。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
1. 链接`libpaddle_capi_shared.so` 动态库 | ||
- 使用 PaddlePaddle C-API 开发预测程序链接`libpaddle_capi_shared.so`时,需注意: | ||
1. 如果编译时指定编译CPU版本,且使用`OpenBLAS`矩阵库,在使用CAPI开发预测程序时,只需要链接`libpaddle_capi_shared.so`这一个库。 | ||
1. 如果是用编译时指定CPU版本,且使用`MKL`矩阵库,由于`MKL`库有自己独立的动态库文件,在使用PaddlePaddle CAPI开发预测程序时,需要自己链接MKL链接库。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MKL库更多的操作,在这篇文档中我先没有增加更多。后续会考虑补充上(我自己再试一下,之前没有直接的经验,所以先没有增加到文档中)
2. 链接静态库 `libpaddle_capi_whole.a` | ||
- 使用PaddlePaddle C-API 开发预测程序链接`libpaddle_capi_whole.a`时,需注意: | ||
1. 需要指定`-Wl,--whole-archive`链接选项。 | ||
1. 需要显式地链接 `gflags`、`glog`、`libz`、`protobuf` 等第三方库,可在`INSTALL_PREFIX\third_party`下找到。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
d58bb9a
to
f635165
Compare
f635165
to
079b824
Compare
1. 进行前向计算,获得计算结果。 | ||
1. 清理。 | ||
|
||
本文档以手写数字识别任务为例,介绍如何使用 C-API 进行预测,完整代码请查看[此目录](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/capi/examples/model_inference/dense)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这句好像多余了,与最开始那句基本是重复的意思。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
### 准备预测模型 | ||
|
||
通过在终端执行`python mnist_v2.py` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
通过在终端执行...将图片分类到 0 ~ 9 类别标签之一。
这段话不是特别通顺。可以如下修改:
-> 通过执行python mnist_v2.py
,可以训练得到一个手写数字识别模型。
另外,可以把MNIST和模型相关部分的介绍放到本文最开始的地方(最开始那段话也提到了手写数字识别)。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
这一篇整体的内容也作了一些调整。MNIST和模型相关部分的介绍,也放到按照我自己理解能够移动的最靠前部分。
### 准备预测模型 | ||
|
||
通过在终端执行`python mnist_v2.py` | ||
运行[目录](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/capi/examples/model_inference/dense)下的 `mnist_v2.py` s可以使用 PaddlePaddle 内置的 [MNIST 数据集](http://yann.lecun.com/exdb/mnist/)进行训练。脚本中的模型定义了一个简单的含有[两个隐层的全连接网络](https://github.com/PaddlePaddle/book/blob/develop/02.recognize_digits/README.cn.md#softmax回归softmax-regression),网络接受一幅图片作为输入,将图片分类到 0 ~ 9 类别标签之一。训练好的模型默认保存在当前运行目录下的`models`目录中。下面,我们将调用 C-API 加载训练好的模型进行预测。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
下面,我们将调用 C-API 加载训练好的模型进行预测。
-> 下面,我们将训练好的模型转换成预测模型。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
output_file = "output.paddle.model" | ||
merge_v2_model(net, param_file, output_file) | ||
``` | ||
对本例,或者直接运行 `python merge_v2_model.py`,序列化结果将会写入当前目录下的`output.paddle.model`文件中,该文件在调用C-API时,可被直接加载。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
或者直接运行
-> 可以直接运行
该文件在调用C-API时,可被直接加载。
-> 使用这种方式,运行时C-API可以通过指定output.paddle.model
文件来加载模型。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
2. 合并模型文件(可选) | ||
|
||
一些情况下为了便于发布,希望能够将序列化后的神经网络结构和训练好的模型参数打包进一个文件,这时可以使用`paddle.utils.merge_model`中的`merge_v2_model`接口对神经网络结构和训练好的参数进行序列化,将序列化结果写入一个文件内,调用C-API时直接只需加载这一个文件。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接只需加载
-> 只需加载
将序列化结果写入一个文件内,调用C-API时直接只需加载这一个文件。
-> 将网络序列化结果和模型参数写入一个文件内
。后面会讲C-API如何加载。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
对本例,或运行 `python mnist_v2.py --task dump_config`,会对示例中的网络结构进行序列化,并将结果写入当前目录下的`trainer_config.bin`文件中。 | ||
|
||
当选择使用这种方式调用 C-API 时,如果神经网络有多个可学习参数,请将它们全部放在同一文件夹内,C-API会从指定的目录寻找并加载训练好的模型。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当选择使用这种方式...训练好的模型
->
使用这种方式,需要在运行时将神经网络的多个可学习参数放在同一个目录中,C-API可以通过分别指定trainer_config.bin
文件和参数目录来加载训练好的模型。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
对本例,或者直接运行 `python merge_v2_model.py`,序列化结果将会写入当前目录下的`output.paddle.model`文件中,该文件在调用C-API时,可被直接加载。 | ||
|
||
#### 注意事项 | ||
1. C-API 需要序列化之后神经网络结构,在调用`dump_v2_config`时,参数`binary`必须指定为`True`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C-API 需要序列化之后神经网络结构
-> 将训练模型转换成预测模型,需要序列化神经网络结构
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
### 编写预测代码 | ||
|
||
#### step 1. 初始化PaddlePaddle运行环境 | ||
使用C-API第一步需首先调用[`paddle_init`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/capi/main.h#L27) 初始化PaddlePaddle运行环境。接口接受两个参数:参数的个数和参数。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第一步需首先调用
-> 第一步需调用
接口接受两个参数:参数的个数和参数
-> 该接口接受两个参数:参数的个数和参数列表
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
follow comments.
1. 进行前向计算,获得计算结果。 | ||
1. 清理。 | ||
|
||
本文档以手写数字识别任务为例,介绍如何使用 C-API 进行预测,完整代码请查看[此目录](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/capi/examples/model_inference/dense)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
### 准备预测模型 | ||
|
||
通过在终端执行`python mnist_v2.py` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
这一篇整体的内容也作了一些调整。MNIST和模型相关部分的介绍,也放到按照我自己理解能够移动的最靠前部分。
### 准备预测模型 | ||
|
||
通过在终端执行`python mnist_v2.py` | ||
运行[目录](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/capi/examples/model_inference/dense)下的 `mnist_v2.py` s可以使用 PaddlePaddle 内置的 [MNIST 数据集](http://yann.lecun.com/exdb/mnist/)进行训练。脚本中的模型定义了一个简单的含有[两个隐层的全连接网络](https://github.com/PaddlePaddle/book/blob/develop/02.recognize_digits/README.cn.md#softmax回归softmax-regression),网络接受一幅图片作为输入,将图片分类到 0 ~ 9 类别标签之一。训练好的模型默认保存在当前运行目录下的`models`目录中。下面,我们将调用 C-API 加载训练好的模型进行预测。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
对本例,或运行 `python mnist_v2.py --task dump_config`,会对示例中的网络结构进行序列化,并将结果写入当前目录下的`trainer_config.bin`文件中。 | ||
|
||
当选择使用这种方式调用 C-API 时,如果神经网络有多个可学习参数,请将它们全部放在同一文件夹内,C-API会从指定的目录寻找并加载训练好的模型。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
2. 合并模型文件(可选) | ||
|
||
一些情况下为了便于发布,希望能够将序列化后的神经网络结构和训练好的模型参数打包进一个文件,这时可以使用`paddle.utils.merge_model`中的`merge_v2_model`接口对神经网络结构和训练好的参数进行序列化,将序列化结果写入一个文件内,调用C-API时直接只需加载这一个文件。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
output_file = "output.paddle.model" | ||
merge_v2_model(net, param_file, output_file) | ||
``` | ||
对本例,或者直接运行 `python merge_v2_model.py`,序列化结果将会写入当前目录下的`output.paddle.model`文件中,该文件在调用C-API时,可被直接加载。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
对本例,或者直接运行 `python merge_v2_model.py`,序列化结果将会写入当前目录下的`output.paddle.model`文件中,该文件在调用C-API时,可被直接加载。 | ||
|
||
#### 注意事项 | ||
1. C-API 需要序列化之后神经网络结构,在调用`dump_v2_config`时,参数`binary`必须指定为`True`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
### 编写预测代码 | ||
|
||
#### step 1. 初始化PaddlePaddle运行环境 | ||
使用C-API第一步需首先调用[`paddle_init`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/capi/main.h#L27) 初始化PaddlePaddle运行环境。接口接受两个参数:参数的个数和参数。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
f84cd45
to
ebd4cfe
Compare
ebd4cfe
to
4d4df08
Compare
doc/howto/usage/capi/index_cn.rst
Outdated
|
||
compile_paddle_lib_cn.md | ||
organization_of_the_inputs_cn.md | ||
workflow_of_capi.md |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
workflow_of_capi请加_cn后缀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
37670e8
to
476c617
Compare
476c617
to
a79a5bd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
由于很多用户都需要一个CAPI的文档,这个PR先merge,不完善的地方会在以后的PR中修改。
fixes #6527 #6456 #6227 #6178 #4303 #2853 #1949 #1946
related to #6353