Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
兽拳
Paddle
提交
ac32bf6f
P
Paddle
项目概览
兽拳
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ac32bf6f
编写于
3月 21, 2019
作者:
L
lujun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update input params type, test=develop
上级
09442fb2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
11 addition
and
18 deletion
+11
-18
python/paddle/fluid/imperative/checkpoint.py
python/paddle/fluid/imperative/checkpoint.py
+11
-18
未找到文件。
python/paddle/fluid/imperative/checkpoint.py
浏览文件 @
ac32bf6f
...
...
@@ -17,13 +17,12 @@ from __future__ import print_function
import
os
import
collections
from
..
import
core
from
..framework
import
Variable
,
Parameter
,
default_main_program
from
.layers
import
Layer
from
..framework
import
Variable
,
default_main_program
__all__
=
[
'save_persistables'
,
'load_persistables'
]
def
save_persistables
(
obj
,
dirname
,
filename
=
None
):
def
save_persistables
(
vardict
,
dirname
,
filename
=
None
):
"""
This function filters out all variables in layer.parameters from the
give `layer` and then trys to load these variables from the folder
...
...
@@ -35,7 +34,7 @@ def save_persistables(obj, dirname, filename=None):
the file name.
Args:
var
_list(dict of Parameters|Layer
): The parameters will
var
dict(dict of Parameters
): The parameters will
be saved. If it is None, nothing
will be deal.
dirname(str): The directory path.
...
...
@@ -69,17 +68,14 @@ def save_persistables(obj, dirname, filename=None):
dy_loss, last_hidden, last_cell = ptb_model(x, y, init_hidden,
init_cell)
param_path = "./my_paddle_model"
fluid.imperative.checkpoint.save_persistables(ptb_model.
parameters
(), dirname=param_path,
fluid.imperative.checkpoint.save_persistables(ptb_model.
state_dict
(), dirname=param_path,
layer=ptb_model)
"""
if
isinstance
(
obj
,
collections
.
OrderedDict
):
_save_var_to_file
(
obj
,
dirname
,
filename
)
elif
isinstance
(
obj
,
Layer
):
_save_var_to_file
(
obj
.
state_dict
(
include_sublayers
=
True
),
dirname
,
filename
)
if
isinstance
(
vardict
,
collections
.
OrderedDict
):
_save_var_to_file
(
vardict
,
dirname
,
filename
)
def
load_persistables
(
obj
,
dirname
,
filename
=
None
):
def
load_persistables
(
vardict
,
dirname
,
filename
=
None
):
"""
This function trys to load persistable variables from the folder
`dirname` or the file `filename`.
...
...
@@ -90,7 +86,7 @@ def load_persistables(obj, dirname, filename=None):
the file name.
Args:
obj(dict of Parameters|Layer
): The parameters will be loaded.
vardict(dict of Parameters
): The parameters will be loaded.
dirname(str): The directory path.
filename(str|None): The file which saved all variables, this file path should be end with '.npz'. If variables were
saved in differnet files, set it to None.
...
...
@@ -111,16 +107,13 @@ def load_persistables(obj, dirname, filename=None):
my_layer = layer(fluid.imperative.Layer)
param_path = "./my_paddle_model"
filename = "model.file"
param_dict = fluid.imperative.checkpoint.load_persistables(my_layer
, var_list
, param_path,
param_dict = fluid.imperative.checkpoint.load_persistables(my_layer
.state_dict()
, param_path,
filename=filename)
param_1 = param_dict['PtbModel_0.w_1']
"""
if
isinstance
(
obj
,
collections
.
OrderedDict
):
return
_load_var_from_file
(
obj
,
dirname
,
filename
)
elif
isinstance
(
obj
,
Layer
):
return
_load_var_from_file
(
obj
.
state_dict
(
include_sublayers
=
True
),
dirname
,
filename
)
if
isinstance
(
vardict
,
collections
.
OrderedDict
):
return
_load_var_from_file
(
vardict
,
dirname
,
filename
)
return
{}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录