From 94faa112e9d85b9f9dba51080c268ef0a7f82936 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Tue, 13 Oct 2020 12:32:00 +0800 Subject: [PATCH] Polish save load en doc details (#27845) * polish save load en doc details, test=document_fix * polish details, test=document_fix * remove blank, test=document_fix * fix ascii error, test=document_fix * update fc's input to x, test=document_fix * update fc argument activation, test=document_fix --- python/paddle/fluid/dygraph/checkpoint.py | 6 ++--- python/paddle/fluid/dygraph/jit.py | 32 +++++++++++----------- python/paddle/framework/io.py | 33 +++++++++++++---------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/python/paddle/fluid/dygraph/checkpoint.py b/python/paddle/fluid/dygraph/checkpoint.py index 5ffbb8c1b5..a18a6ed5c3 100644 --- a/python/paddle/fluid/dygraph/checkpoint.py +++ b/python/paddle/fluid/dygraph/checkpoint.py @@ -140,12 +140,12 @@ def load_dygraph(model_path, **configs): Args: model_path(str) : The file prefix store the state_dict. (The path should Not contain suffix '.pdparams') - **configs (dict, optional): other save configuration options for compatibility. We do not + **configs (dict, optional): Other load configuration options for compatibility. We do not recommend using these configurations, if not necessary, DO NOT use them. Default None. The following options are currently supported: - (1) model_filename (string): The inference model file name of the paddle 1.x ``save_inference_model`` + (1) model_filename (str): The inference model file name of the paddle 1.x ``save_inference_model`` save format. Default file name is :code:`__model__` . - (2) params_filename (string): The persistable variables file name of the paddle 1.x ``save_inference_model`` + (2) params_filename (str): The persistable variables file name of the paddle 1.x ``save_inference_model`` save format. No default file name, save variables separately by default. Returns: diff --git a/python/paddle/fluid/dygraph/jit.py b/python/paddle/fluid/dygraph/jit.py index 6cdd13fba8..9eea6d659f 100644 --- a/python/paddle/fluid/dygraph/jit.py +++ b/python/paddle/fluid/dygraph/jit.py @@ -470,10 +470,10 @@ def save(layer, path, input_spec=None, **configs): format model, which can be used for inference or fine-tuning after loading. It will save the translated program and all related persistable - variables of input Layer to given ``path``. + variables of input Layer to given ``path`` . ``path`` is the prefix of saved objects, and the saved translated program file - suffix is ``.pdmodel``, the saved persistable variables file suffix is ``.pdiparams``, + suffix is ``.pdmodel`` , the saved persistable variables file suffix is ``.pdiparams`` , and here also saved some additional variable description information to a file, its suffix is ``.pdiparams.info``, these additional information is used in fine-tuning. @@ -483,18 +483,17 @@ def save(layer, path, input_spec=None, **configs): - Other C++ inference APIs Args: - layer (Layer): the Layer to be saved. The Layer should be decorated by `@paddle.jit.to_static`. + layer (Layer): The Layer to be saved. path (str): The path prefix to save model. The format is ``dirname/file_prefix`` or ``file_prefix``. - input_spec (list[InputSpec|Tensor], optional): Describes the input of the saved model. - It is the example inputs that will be passed to saved TranslatedLayer's forward - function. If None, all input variables of the original Layer's forward function - would be the inputs of the saved model. Default None. - **configs (dict, optional): other save configuration options for compatibility. We do not + input_spec (list[InputSpec|Tensor], optional): Describes the input of the saved model's forward + method, which can be described by InputSpec or example Tensor. If None, all input variables of + the original Layer's forward method would be the inputs of the saved model. Default None. + **configs (dict, optional): Other save configuration options for compatibility. We do not recommend using these configurations, they may be removed in the future. If not necessary, DO NOT use them. Default None. The following options are currently supported: (1) output_spec (list[Tensor]): Selects the output targets of the saved model. - By default, all return variables of original Layer's forward function are kept as the + By default, all return variables of original Layer's forward method are kept as the output of the saved model. If the provided ``output_spec`` list is not all output variables, the saved model will be pruned according to the given ``output_spec`` list. @@ -735,14 +734,14 @@ def load(path, **configs): 4. The parameter's ``trainable`` information is lost and can not be recovered. Args: - path (str): The path prefix to load model. The format is ``dirname/file_prefix`` or ``file_prefix``. - **configs (dict, optional): other load configuration options for compatibility. We do not + path (str): The path prefix to load model. The format is ``dirname/file_prefix`` or ``file_prefix`` . + **configs (dict, optional): Other load configuration options for compatibility. We do not recommend using these configurations, they may be removed in the future. If not necessary, DO NOT use them. Default None. The following options are currently supported: - (1) model_filename (string): The inference model file name of the paddle 1.x + (1) model_filename (str): The inference model file name of the paddle 1.x ``save_inference_model`` save format. Default file name is :code:`__model__` . - (2) params_filename (string): The persistable variables file name of the paddle 1.x + (2) params_filename (str): The persistable variables file name of the paddle 1.x ``save_inference_model`` save format. No default file name, save variables separately by default. @@ -844,7 +843,6 @@ def load(path, **configs): import numpy as np import paddle - import paddle.fluid as fluid import paddle.static as static import paddle.nn as nn import paddle.optimizer as opt @@ -870,9 +868,11 @@ def load(path, **configs): def __len__(self): return self.num_samples + paddle.enable_static() + image = static.data(name='image', shape=[None, 784], dtype='float32') label = static.data(name='label', shape=[None, 1], dtype='int64') - pred = static.nn.fc(input=image, size=10, act='softmax') + pred = static.nn.fc(x=image, size=10, activation='softmax') loss = F.cross_entropy(input=pred, label=label) avg_loss = paddle.mean(loss) @@ -901,7 +901,7 @@ def load(path, **configs): fetch_list=[avg_loss]) model_path = "fc.example.model" - fluid.io.save_inference_model( + paddle.fluid.io.save_inference_model( model_path, ["image"], [pred], exe) # 2. load model diff --git a/python/paddle/framework/io.py b/python/paddle/framework/io.py index a5555a8494..7e8c717bb1 100644 --- a/python/paddle/framework/io.py +++ b/python/paddle/framework/io.py @@ -204,10 +204,13 @@ def save(obj, path): Now only supports save ``state_dict`` of Layer or Optimizer. .. note:: - ``paddle.save`` will not add a suffix to the saved results, - but we recommend that you use the following paddle standard suffixes: - 1. for ``Layer.state_dict`` -> ``.pdparams`` - 2. for ``Optimizer.state_dict`` -> ``.pdopt`` + 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. Args: obj(Object) : The object to be saved. @@ -272,9 +275,10 @@ def load(path, **configs): Now only supports load ``state_dict`` of Layer or Optimizer. .. note:: - ``paddle.load`` supports loading ``state_dict`` of Layer or Optimizer from - the result of other save APIs except ``paddle.load`` , but the argument - ``path`` format is different: + In order to use the model parameters saved by paddle more efficiently, + ``paddle.load`` supports loading ``state_dict`` of Layer from the result of + other save APIs except ``paddle.save`` , but the argument ``path`` format is + different: 1. loading from ``paddle.static.save`` or ``paddle.Model().save(training=True)`` , ``path`` needs to be a complete file name, such as ``model.pdparams`` or ``model.pdopt`` ; @@ -287,22 +291,23 @@ def load(path, **configs): directory, such as ``model`` and model is a directory. .. note:: - If you load ``state_dict`` from the saved result of + If you load ``state_dict`` from the saved result of static mode API such as ``paddle.static.save`` or ``paddle.static.save_inference_model`` , - the structured variable name will cannot be restored. You need to set the argument - ``use_structured_name=False`` when using ``Layer.set_state_dict`` later. + the structured variable name in dynamic mode will cannot be restored. + You need to set the argument ``use_structured_name=False`` when using + ``Layer.set_state_dict`` later. Args: path(str) : The path to load the target object. Generally, the path is the target - file path. When compatible with loading the saved results other APIs, the path - can be a file prefix or directory. + file path. When loading state_dict from the saved result of the API used to save + the inference model, the path may be a file prefix or directory. **configs (dict, optional): other load configuration options for compatibility. We do not recommend using these configurations, they may be removed in the future. If not necessary, DO NOT use them. Default None. The following options are currently supported: - (1) model_filename (string): The inference model file name of the paddle 1.x + (1) model_filename (str): The inference model file name of the paddle 1.x ``save_inference_model`` save format. Default file name is :code:`__model__` . - (2) params_filename (string): The persistable variables file name of the paddle 1.x + (2) params_filename (str): The persistable variables file name of the paddle 1.x ``save_inference_model`` save format. No default file name, save variables separately by default. -- GitLab