-
Notifications
You must be signed in to change notification settings - Fork 822
paddle.save/load2.1 #3477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
paddle.save/load2.1 #3477
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d81192c
paddle.save/load2.1
hbwx24 eb3d9d7
paddle.save/load2.1
hbwx24 e8aae0d
polish doc
hbwx24 1465883
polish doc
hbwx24 ba1a24a
polish doc
hbwx24 e0669b2
polish doc
hbwx24 39f4313
polish doc
hbwx24 202a9d0
edit doc according comment
hbwx24 b4dfc63
polish doc
hbwx24 2a0403e
delete save layer
hbwx24 0821ad4
polish doc
hbwx24 669e77b
polish code
hbwx24 5c33d96
polish code
hbwx24 1935249
polish doc
hbwx24 b03a054
polish code
hbwx24 3d86759
polish doc
hbwx24 3e5857d
polish code according to comment
hbwx24 42dd21a
polish code
hbwx24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,21 +8,20 @@ load | |
从指定路径载入可以在paddle中使用的对象实例。 | ||
|
||
.. note:: | ||
目前仅支持载入 Layer 或者 Optimizer 的 ``state_dict`` 。 | ||
目前支持载入:Layer 或者 Optimizer 的 ``state_dict``,Layer对象,Tensor以及包含Tensor的嵌套list、tuple、dict,Program。 | ||
|
||
.. note:: | ||
为了更高效地使用paddle存储的模型参数, ``paddle.load`` 支持从除 ``paddle.save`` 之外的其他save相关API的存储结果中载入 ``state_dict`` ,但是在不同场景中,参数 ``path`` 的形式有所不同: | ||
1. 从 ``paddle.static.save`` 或者 ``paddle.Model().save(training=True)`` 的保存结果载入: ``path`` 需要是完整的文件名,例如 ``model.pdparams`` 或者 ``model.opt`` ; | ||
2. 从 ``paddle.jit.save`` 或者 ``paddle.static.save_inference_model`` 或者 ``paddle.Model().save(training=False)`` 的保存结果载入: ``path`` 需要是路径前缀, 例如 ``model/mnist`` , ``paddle.load`` 会从 ``mnist.pdmodel`` 和 ``mnist.pdiparams`` 中解析 ``state_dict`` 的信息并返回。 | ||
3. 从paddle 1.x API ``paddle.fluid.io.save_inference_model`` 或者 ``paddle.fluid.io.save_params/save_persistables`` 的保存结果载入: ``path`` 需要是目录,例如 ``model`` ,此处model是一个文件夹路径。 | ||
|
||
.. note:: | ||
如果从 ``paddle.static.save`` 或者 ``paddle.static.save_inference_model`` 等静态图API的存储结果中载入 ``state_dict`` ,动态图模式下参数的结构性变量名将无法被恢复。在将载入的 ``state_dict`` 配置到当前Layer中时,需要配置 ``Layer.set_state_dict`` 的参数 ``use_structured_name=False`` 。 | ||
如果想进一步了解这个API,请参考: | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
../../../../faq/save_cn.md | ||
|
||
参数 | ||
::::::::: | ||
- path (str) – 载入目标对象实例的路径。通常该路径是目标文件的路径,当从用于存储预测模型API的存储结果中载入state_dict时,该路径可能是一个文件前缀或者目录。 | ||
- **config (dict, 可选) - 其他用于兼容的载入配置选项。这些选项将来可能被移除,如果不是必须使用,不推荐使用这些配置选项。默认为 ``None``。目前支持以下配置选项:(1) model_filename (str) - paddle 1.x版本 ``save_inference_model`` 接口存储格式的预测模型文件名,原默认文件名为 ``__model__`` ; (2) params_filename (str) - paddle 1.x版本 ``save_inference_model`` 接口存储格式的参数文件名,没有默认文件名,默认将各个参数分散存储为单独的文件。 | ||
- **config (dict, 可选) - 其他用于兼容的载入配置选项。这些选项将来可能被移除,如果不是必须使用,不推荐使用这些配置选项。默认为 ``None``。目前支持以下配置选项:(1) model_filename (str) - paddle 1.x版本 ``save_inference_model`` 接口存储格式的预测模型文件名,原默认文件名为 ``__model__`` ; (2) params_filename (str) - paddle 1.x版本 ``save_inference_model`` 接口存储格式的参数文件名,没有默认文件名,默认将各个参数分散存储为单独的文件; (3) return_numpy(bool) - 如果被指定为 ``True`` ,``load`` 的结果中的Tensor会被转化为 ``numpy.ndarray`` ,默认为 ``False`` 。 | ||
|
||
返回 | ||
::::::::: | ||
|
@@ -33,10 +32,12 @@ Object,一个可以在paddle中使用的对象实例 | |
|
||
.. code-block:: python | ||
|
||
# example 1: dynamic graph | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 示例里面是不是没有Layer,Program,嵌套结构的示例,这个要补充一下吗?可能英文的也得补充 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thx. |
||
import paddle | ||
|
||
emb = paddle.nn.Embedding(10, 10) | ||
layer_state_dict = emb.state_dict() | ||
|
||
# save state_dict of emb | ||
paddle.save(layer_state_dict, "emb.pdparams") | ||
|
||
scheduler = paddle.optimizer.lr.NoamDecay( | ||
|
@@ -45,7 +46,77 @@ Object,一个可以在paddle中使用的对象实例 | |
learning_rate=scheduler, | ||
parameters=emb.parameters()) | ||
opt_state_dict = adam.state_dict() | ||
|
||
# save state_dict of optimizer | ||
paddle.save(opt_state_dict, "adam.pdopt") | ||
# save weight of emb | ||
paddle.save(emb.weight, "emb.weight.pdtensor") | ||
|
||
# load state_dict of emb | ||
load_layer_state_dict = paddle.load("emb.pdparams") | ||
# load state_dict of optimizer | ||
load_opt_state_dict = paddle.load("adam.pdopt") | ||
# load weight of emb | ||
load_weight = paddle.load("emb.weight.pdtensor") | ||
|
||
.. code-block:: python | ||
|
||
# example 2: Load 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' | ||
paddle.save(obj, path) | ||
obj_load = paddle.load(path) | ||
|
||
|
||
.. code-block:: python | ||
|
||
# example 3: static graph | ||
import paddle | ||
import paddle.static as static | ||
|
||
paddle.enable_static() | ||
|
||
# create network | ||
x = paddle.static.data(name="x", shape=[None, 224], dtype='float32') | ||
z = paddle.static.nn.fc(x, 10) | ||
|
||
place = paddle.CPUPlace() | ||
exe = paddle.static.Executor(place) | ||
exe.run(paddle.static.default_startup_program()) | ||
prog = paddle.static.default_main_program() | ||
for var in prog.list_vars(): | ||
if list(var.shape) == [224, 10]: | ||
tensor = var.get_value() | ||
break | ||
|
||
# save/load tensor | ||
path_tensor = 'temp/tensor.pdtensor' | ||
paddle.save(tensor, path_tensor) | ||
load_tensor = paddle.load(path_tensor) | ||
|
||
# save/load state_dict | ||
path_state_dict = 'temp/model.pdparams' | ||
paddle.save(prog.state_dict("param"), path_tensor) | ||
load_state_dict = paddle.load(path_tensor) | ||
|
||
.. code-block:: python | ||
|
||
# example 4: load program | ||
import paddle | ||
|
||
paddle.enable_static() | ||
|
||
data = paddle.static.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" | ||
paddle.save(main_program, path) | ||
load_main = paddle.load(path) | ||
print(load_main) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的s要去掉吗
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thx.