Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
4639f5de
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
4639f5de
编写于
5月 18, 2021
作者:
W
WeiXin
提交者:
GitHub
5月 18, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Cherry-pick]Add code examples for paddle.save/load (#32900) (#32929)
* doc of paddle.save/load * polish doc of paddle.save/load
上级
7b0b064d
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
55 addition
and
6 deletion
+55
-6
python/paddle/framework/io.py
python/paddle/framework/io.py
+55
-6
未找到文件。
python/paddle/framework/io.py
浏览文件 @
4639f5de
...
@@ -496,7 +496,7 @@ def save(obj, path, protocol=2, **configs):
...
@@ -496,7 +496,7 @@ def save(obj, path, protocol=2, **configs):
Save an object to the specified path.
Save an object to the specified path.
.. note::
.. note::
Now supports saving ``state_dict`` of Layer/Optimizer, Layer, Tensor and nested structure containing Tensor.
Now supports saving ``state_dict`` of Layer/Optimizer, Layer, Tensor and nested structure containing Tensor
, Program
.
.. note::
.. note::
Different from ``paddle.jit.save``, since the save result of ``paddle.save`` is a single file,
Different from ``paddle.jit.save``, since the save result of ``paddle.save`` is a single file,
...
@@ -544,7 +544,18 @@ def save(obj, path, protocol=2, **configs):
...
@@ -544,7 +544,18 @@ def save(obj, path, protocol=2, **configs):
# save weight of emb
# save weight of emb
paddle.save(emb.weight, "emb.weight.pdtensor")
paddle.save(emb.weight, "emb.weight.pdtensor")
# example 2: static graph
# example 2: Save multiple state_dict at the same time
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)
# example 3: static graph
import paddle
import paddle
import paddle.static as static
import paddle.static as static
...
@@ -570,6 +581,18 @@ def save(obj, path, protocol=2, **configs):
...
@@ -570,6 +581,18 @@ def save(obj, path, protocol=2, **configs):
# save/load state_dict
# save/load state_dict
path_state_dict = 'temp/model.pdparams'
path_state_dict = 'temp/model.pdparams'
paddle.save(prog.state_dict("param"), path_tensor)
paddle.save(prog.state_dict("param"), path_tensor)
# example 4: save 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)
'''
'''
# 1. input check
# 1. input check
filename
=
os
.
path
.
basename
(
path
)
filename
=
os
.
path
.
basename
(
path
)
...
@@ -667,7 +690,7 @@ def load(path, **configs):
...
@@ -667,7 +690,7 @@ def load(path, **configs):
Load an object can be used in paddle from specified path.
Load an object can be used in paddle from specified path.
.. note::
.. note::
Now supports loading ``state_dict`` of Layer/Optimizer, Layer, Tensor and nested structure containing Tensor.
Now supports loading ``state_dict`` of Layer/Optimizer, Layer, Tensor and nested structure containing Tensor
, Program
.
.. note::
.. note::
In order to use the model parameters saved by paddle more efficiently,
In order to use the model parameters saved by paddle more efficiently,
...
@@ -714,8 +737,6 @@ def load(path, **configs):
...
@@ -714,8 +737,6 @@ def load(path, **configs):
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
# example 1: dynamic graph
# example 1: dynamic graph
import paddle
import paddle
emb = paddle.nn.Embedding(10, 10)
emb = paddle.nn.Embedding(10, 10)
...
@@ -744,7 +765,19 @@ def load(path, **configs):
...
@@ -744,7 +765,19 @@ def load(path, **configs):
load_weight = paddle.load("emb.weight.pdtensor")
load_weight = paddle.load("emb.weight.pdtensor")
# example 2: static graph
# example 2: Load multiple state_dict at the same time
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)
# example 3: static graph
import paddle
import paddle
import paddle.static as static
import paddle.static as static
...
@@ -773,6 +806,22 @@ def load(path, **configs):
...
@@ -773,6 +806,22 @@ def load(path, **configs):
paddle.save(prog.state_dict("param"), path_tensor)
paddle.save(prog.state_dict("param"), path_tensor)
load_state_dict = paddle.load(path_tensor)
load_state_dict = paddle.load(path_tensor)
# 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)
'''
'''
if
os
.
path
.
isfile
(
path
):
if
os
.
path
.
isfile
(
path
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录