save_params

paddle.fluid.io. save_params ( executor, dirname, main_program=None, filename=None ) [source]
Api_attr

Static Graph

This operator saves all parameters from the main_program to the folder dirname or file filename. You can refer to Save and Load a Model for more details.

Use the dirname to specify the saving folder. If you would like to save parameters in separate files, set filename None; if you would like to save all parameters in a single file, use filename to specify the file name.

Note

Some variables are not Parameter while they are necessary for training, such as learning rate, global step, etc. So you can NOT save and continue your training just by api_fluid_io_save_params and api_fluid_io_load_params. Please use api_fluid_io_save_persistables and api_fluid_io_load_persistables instead.

If you want to save your model for the inference, please use the api_fluid_io_save_inference_model. You can refer to Save and Load a Model for more details.

Parameters
  • executor (Executor) – The executor to run for saving parameters, You can refer to Executor.

  • dirname (str, optional) – The saving directory path. When you need to save the parameter to the memory, set it to None.

  • main_program (Program, optional) – The program whose parameters will be saved. You can refer to Basic Concept for more details. If it is None, the default main program will be used. Default: None

  • filename (str, optional) – The file to save all parameters. If you prefer to save parameters in different files, set it to None. Default: None

Returns

When saving parameters to a file, returns None.

When saving parameters to memory, returns a binary string containing parameters.

Return type

str

Examples

import paddle
import paddle.fluid as fluid


paddle.enable_static()
params_path = "./my_paddle_model"
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 = fluid.layers.mean(loss)

exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
fluid.io.save_params(executor=exe, dirname=params_path)
# The parameters weights and bias of the fc layer in the network are going to
# be saved in different files in the path "./my_paddle_model"