Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
cb4560b7
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看板
未验证
提交
cb4560b7
编写于
2月 15, 2020
作者:
S
songyouwei
提交者:
GitHub
2月 15, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
python 2 and 3 compatible pickle (#22555)
test=develop
上级
90ee3666
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
17 addition
and
10 deletion
+17
-10
python/paddle/fluid/dygraph/checkpoint.py
python/paddle/fluid/dygraph/checkpoint.py
+6
-3
python/paddle/fluid/io.py
python/paddle/fluid/io.py
+11
-7
未找到文件。
python/paddle/fluid/dygraph/checkpoint.py
浏览文件 @
cb4560b7
...
...
@@ -18,6 +18,7 @@ import os
import
collections
from
..framework
import
Variable
,
default_main_program
,
in_dygraph_mode
,
dygraph_only
,
Parameter
,
ParamBase
import
pickle
import
six
from
.
import
learning_rate_scheduler
import
warnings
from
..
import
core
...
...
@@ -88,7 +89,7 @@ def save_dygraph(state_dict, model_path):
os
.
makedirs
(
dir_name
)
with
open
(
file_name
,
'wb'
)
as
f
:
pickle
.
dump
(
model_dict
,
f
)
pickle
.
dump
(
model_dict
,
f
,
protocol
=
2
)
@
dygraph_only
...
...
@@ -130,7 +131,8 @@ def load_dygraph(model_path, keep_name_table=False):
params_file_path
))
with
open
(
params_file_path
,
'rb'
)
as
f
:
para_dict
=
pickle
.
load
(
f
)
para_dict
=
pickle
.
load
(
f
)
if
six
.
PY2
else
pickle
.
load
(
f
,
encoding
=
'bytes'
)
if
not
keep_name_table
and
"StructuredToParameterName@@"
in
para_dict
:
del
para_dict
[
"StructuredToParameterName@@"
]
...
...
@@ -138,6 +140,7 @@ def load_dygraph(model_path, keep_name_table=False):
opti_file_path
=
model_path
+
".pdopt"
if
os
.
path
.
exists
(
opti_file_path
):
with
open
(
opti_file_path
,
'rb'
)
as
f
:
opti_dict
=
pickle
.
load
(
f
)
opti_dict
=
pickle
.
load
(
f
)
if
six
.
PY2
else
pickle
.
load
(
f
,
encoding
=
'bytes'
)
return
para_dict
,
opti_dict
python/paddle/fluid/io.py
浏览文件 @
cb4560b7
...
...
@@ -765,7 +765,7 @@ def load_vars(executor,
var_temp
=
paddle
.
fluid
.
global_scope
().
find_var
(
each_var
.
name
)
assert
var_temp
!=
None
,
"can't not find var: "
+
each_var
.
name
new_shape
=
(
np
.
array
(
var_temp
.
get_tensor
())).
shape
assert
each_var
.
name
in
orig_para_shape
,
ea
r
ch_var
.
name
+
"MUST in var list"
assert
each_var
.
name
in
orig_para_shape
,
each_var
.
name
+
"MUST in var list"
orig_shape
=
orig_para_shape
.
get
(
each_var
.
name
)
if
new_shape
!=
orig_shape
:
raise
RuntimeError
(
...
...
@@ -1541,14 +1541,14 @@ def save(program, model_path):
parameter_list
=
list
(
filter
(
is_parameter
,
program
.
list_vars
()))
param_dict
=
{
p
.
name
:
get_tensor
(
p
)
for
p
in
parameter_list
}
with
open
(
model_path
+
".pdparams"
,
'wb'
)
as
f
:
pickle
.
dump
(
param_dict
,
f
)
pickle
.
dump
(
param_dict
,
f
,
protocol
=
2
)
optimizer_var_list
=
list
(
filter
(
is_belong_to_optimizer
,
program
.
list_vars
()))
opt_dict
=
{
p
.
name
:
get_tensor
(
p
)
for
p
in
optimizer_var_list
}
with
open
(
model_path
+
".pdopt"
,
'wb'
)
as
f
:
pickle
.
dump
(
opt_dict
,
f
)
pickle
.
dump
(
opt_dict
,
f
,
protocol
=
2
)
main_program
=
program
.
clone
()
program
.
desc
.
flush
()
...
...
@@ -1695,7 +1695,8 @@ def load(program, model_path, executor=None, var_list=None):
global_scope
(),
executor
.
_default_executor
)
with
open
(
parameter_file_name
,
'rb'
)
as
f
:
load_dict
=
pickle
.
load
(
f
)
load_dict
=
pickle
.
load
(
f
)
if
six
.
PY2
else
pickle
.
load
(
f
,
encoding
=
'bytes'
)
for
v
in
parameter_list
:
assert
v
.
name
in
load_dict
,
\
"Can not find [{}] in model file [{}]"
.
format
(
...
...
@@ -1715,7 +1716,8 @@ def load(program, model_path, executor=None, var_list=None):
optimizer_var_list
,
global_scope
(),
executor
.
_default_executor
)
with
open
(
opt_file_name
,
'rb'
)
as
f
:
load_dict
=
pickle
.
load
(
f
)
load_dict
=
pickle
.
load
(
f
)
if
six
.
PY2
else
pickle
.
load
(
f
,
encoding
=
'bytes'
)
for
v
in
optimizer_var_list
:
assert
v
.
name
in
load_dict
,
\
"Can not find [{}] in model file [{}]"
.
format
(
...
...
@@ -1839,12 +1841,14 @@ def load_program_state(model_path, var_list=None):
"Parameter file [{}] not exits"
.
format
(
parameter_file_name
)
with
open
(
parameter_file_name
,
'rb'
)
as
f
:
para_dict
=
pickle
.
load
(
f
)
para_dict
=
pickle
.
load
(
f
)
if
six
.
PY2
else
pickle
.
load
(
f
,
encoding
=
'bytes'
)
opt_file_name
=
model_prefix
+
".pdopt"
if
os
.
path
.
exists
(
opt_file_name
):
with
open
(
opt_file_name
,
'rb'
)
as
f
:
opti_dict
=
pickle
.
load
(
f
)
opti_dict
=
pickle
.
load
(
f
)
if
six
.
PY2
else
pickle
.
load
(
f
,
encoding
=
'bytes'
)
para_dict
.
update
(
opti_dict
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录