save

paddle. save ( obj, path, pickle_protocol=2 ) [source]

Save an object to the specified path.

Note

Now only supports save state_dict of Layer or Optimizer.

Note

Different from paddle.jit.save, since the save result of paddle.save is a single file, there is no need to distinguish multiple saved files by adding a suffix. The argument path of paddle.save 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.

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

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

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

Returns

None

Examples

import paddle

emb = paddle.nn.Embedding(10, 10)
layer_state_dict = emb.state_dict()
paddle.save(layer_state_dict, "emb.pdparams")
scheduler = paddle.optimizer.lr.NoamDecay(
    d_model=0.01, warmup_steps=100, verbose=True)
adam = paddle.optimizer.Adam(
    learning_rate=scheduler,
    parameters=emb.parameters())
opt_state_dict = adam.state_dict()
paddle.save(opt_state_dict, "adam.pdopt")