serialize_persistables
根据指定的 feed_vars,fetch_vars 和 executor,序列化模型参数。
参数
feed_vars (Variable | list[Variable]) – 模型的输入变量。
fetch_vars (Variable | list[Variable]) – 模型的输出变量。
executor (Executor) - 用于保存预测模型的
executor,详见 执行器 。kwargs - 支持的 key 包括 program。(注意:kwargs 主要是用来做反向兼容的)。
program - 指定想要序列化的 program,默认使用 default_main_program。
返回
参数序列化之后的字节数组。
代码示例
>>> import paddle
>>> paddle.enable_static()
>>> with paddle.pir_utils.OldIrGuard():
... path_prefix = "./infer_model"
...
... # User defined network, here a softmax regression example
... image = paddle.static.data(name='img', shape=[None, 28, 28], dtype='float32')
... label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
... predict = paddle.static.nn.fc(image, 10, activation='softmax')
...
... loss = paddle.nn.functional.cross_entropy(predict, label)
...
... exe = paddle.static.Executor(paddle.CPUPlace())
... exe.run(paddle.static.default_startup_program())
...
... # serialize parameters to bytes.
... serialized_params = paddle.static.serialize_persistables([image], [predict], exe)
...
... # deserialize bytes to parameters.
... main_program = paddle.static.default_main_program()
... deserialized_params = paddle.static.deserialize_persistables(main_program, serialized_params, exe)