paddle. save ( obj, path, protocol=4, **configs ) [source]

Save an object to the specified path.


Now supports saving state_dict of Layer/Optimizer, Tensor and nested structure containing Tensor, Program.


Different from, since the save result of is a single file, there is no need to distinguish multiple saved files by adding a suffix. The argument path of will be directly used as the saved file name instead of a prefix. In order to unify the saved file name format, we recommend using the paddle standard suffix: 1. for Layer.state_dict , recommend to use .pdparams ; 2. for Optimizer.state_dict , recommend to use .pdopt . For specific examples, please refer to API code examples.

  • obj (Object) – The object to be saved.

  • path (str|BytesIO) – The path/buffer of the object to be saved. If saved in the current directory, the input path string will be used as the file name.

  • protocol (int, optional) – The protocol version of pickle module must be greater than 1 and less than 5. Default: 4

  • **configs (dict, optional) – optional keyword arguments. The following options are currently supported: use_binary_format(bool): When the saved object is static graph variable, you can specify use_binary_for_var. If True, save the file in the c++ binary format when saving a single static graph variable; otherwise, save it in pickle format. Default: False




# example 1: dynamic graph
import paddle
emb = paddle.nn.Embedding(10, 10)
layer_state_dict = emb.state_dict()

# save state_dict of emb, "emb.pdparams")

scheduler =
    d_model=0.01, warmup_steps=100, verbose=True)
adam = paddle.optimizer.Adam(
opt_state_dict = adam.state_dict()

# save state_dict of optimizer, "adam.pdopt")
# save weight of emb, "emb.weight.pdtensor")
# example 2: Save multiple state_dict at the same time
import paddle
from paddle import nn
from paddle.optimizer import Adam

layer = paddle.nn.Linear(3, 4)
adam = Adam(learning_rate=0.001, parameters=layer.parameters())
obj = {'model': layer.state_dict(), 'opt': adam.state_dict(), 'epoch': 100}
path = 'example/model.pdparams', path)
# example 3: static graph
import paddle
import paddle.static as static


# create network
x ="x", shape=[None, 224], dtype='float32')
z = paddle.static.nn.fc(x, 10)

place = paddle.CPUPlace()
exe = paddle.static.Executor(place)
prog = paddle.static.default_main_program()
for var in prog.list_vars():
    if list(var.shape) == [224, 10]:
        tensor = var.get_value()

# save/load tensor
path_tensor = 'temp/tensor.pdtensor', path_tensor)

# save/load state_dict
path_state_dict = 'temp/model.pdparams'"param"), path_tensor)
# example 4: save program
import paddle


data =
    name='x_static_save', shape=(None, 224), dtype='float32')
y_static = z = paddle.static.nn.fc(data, 10)
main_program = paddle.static.default_main_program()
path = "example/main_program.pdmodel", path)
# example 5: save object to memory
from io import BytesIO
import paddle
from paddle.nn import Linear

linear = Linear(5, 10)
state_dict = linear.state_dict()
byio = BytesIO(), byio)
tensor = paddle.randn([2, 3], dtype='float32'), byio)

Used in the guide/tutorials