save_persistables¶
该OP从给定 main_program
中取出所有持久性变量(详见 模型保存与加载 ),然后将它们保存到目录 dirname
中或 filename
指定的文件中。
dirname
用于指定保存持久性变量的目录。如果想将持久性变量保存到指定目录的若干文件中,请设置 filename=None
;若想将所有持久性变量保存在同一个文件中,请设置 filename
来指定文件的名称。
参数¶
返回¶
无
代码示例¶
import paddle
import paddle.fluid as fluid
paddle.enable_static()
dir_path = "./my_paddle_model"
file_name = "persistables"
image = fluid.data(name='img', shape=[None, 28, 28], dtype='float32')
label = fluid.data(name='label', shape=[None, 1], dtype='int64')
feeder = fluid.DataFeeder(feed_list=[image, label], place=fluid.CPUPlace())
predict = fluid.layers.fc(input=image, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=predict, label=label)
avg_loss = paddle.mean(loss)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
fluid.io.save_persistables(executor=exe, dirname=dir_path, filename=file_name)
# The persistables variables weights and bias in the fc layer of the network
# are going to be saved in the same file named "persistables" in the path
# "./my_paddle_model"