Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
兽拳
Paddle
提交
cf670ec9
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,发现更多精彩内容 >>
提交
cf670ec9
编写于
10月 28, 2019
作者:
Y
Yang Zhang
提交者:
hong
10月 28, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Serialize to pickle format (#20820)
test=develop
上级
3b31b74e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
42 addition
and
10 deletion
+42
-10
python/paddle/fluid/io.py
python/paddle/fluid/io.py
+42
-10
未找到文件。
python/paddle/fluid/io.py
浏览文件 @
cf670ec9
...
@@ -19,6 +19,7 @@ import errno
...
@@ -19,6 +19,7 @@ import errno
import
warnings
import
warnings
import
six
import
six
import
logging
import
logging
import
pickle
from
functools
import
reduce
from
functools
import
reduce
import
numpy
as
np
import
numpy
as
np
...
@@ -1505,15 +1506,21 @@ def save(program, model_path):
...
@@ -1505,15 +1506,21 @@ def save(program, model_path):
assert
base_name
!=
""
,
\
assert
base_name
!=
""
,
\
"model_path MUST be format of dirname/filename [dirname
\\
filename in Window], Now filename is empty str"
"model_path MUST be format of dirname/filename [dirname
\\
filename in Window], Now filename is empty str"
def
get_tensor
(
var
):
t
=
global_scope
().
find_var
(
var
.
name
).
get_tensor
()
return
np
.
array
(
t
)
parameter_list
=
list
(
filter
(
is_parameter
,
program
.
list_vars
()))
parameter_list
=
list
(
filter
(
is_parameter
,
program
.
list_vars
()))
paddle
.
fluid
.
core
.
_save_static_dict
(
model_path
+
".pdparams"
,
param_dict
=
{
p
.
name
:
get_tensor
(
p
)
for
p
in
parameter_list
}
parameter_list
,
global_scope
())
with
open
(
model_path
+
".pdparams"
,
'wb'
)
as
f
:
pickle
.
dump
(
param_dict
,
f
)
optimizer_var_list
=
list
(
optimizer_var_list
=
list
(
filter
(
is_belong_to_optimizer
,
program
.
list_vars
()))
filter
(
is_belong_to_optimizer
,
program
.
list_vars
()))
paddle
.
fluid
.
core
.
_save_static_dict
(
model_path
+
".pdopt"
,
opt_dict
=
{
p
.
name
:
get_tensor
(
p
)
for
p
in
optimizer_var_list
}
optimizer_var_list
,
global_scope
())
with
open
(
model_path
+
".pdopt"
,
'wb'
)
as
f
:
pickle
.
dump
(
opt_dict
,
f
)
main_program
=
program
.
clone
()
main_program
=
program
.
clone
()
program
.
desc
.
flush
()
program
.
desc
.
flush
()
...
@@ -1552,11 +1559,30 @@ def load(program, model_path):
...
@@ -1552,11 +1559,30 @@ def load(program, model_path):
parameter_file_name
=
model_path
+
".pdparams"
parameter_file_name
=
model_path
+
".pdparams"
assert
os
.
path
.
exists
(
parameter_file_name
),
\
assert
os
.
path
.
exists
(
parameter_file_name
),
\
"Parameter file [{}] not exits"
.
format
(
parameter_file_name
)
"Parameter file [{}] not exits"
.
format
(
parameter_file_name
)
def
set_var
(
var
,
ndarray
):
t
=
global_scope
().
find_var
(
var
.
name
).
get_tensor
()
p
=
t
.
_place
()
if
p
.
is_cpu_place
():
place
=
paddle
.
fluid
.
CPUPlace
()
elif
p
.
is_cuda_pinned_place
():
place
=
paddle
.
fluid
.
CUDAPinnedPlace
()
else
:
p
=
paddle
.
fluid
.
core
.
Place
()
p
.
set_place
(
t
.
_place
())
place
=
paddle
.
fluid
.
CUDAPlace
(
p
.
gpu_device_id
())
t
.
set
(
ndarray
,
place
)
parameter_list
=
list
(
filter
(
is_parameter
,
program
.
list_vars
()))
parameter_list
=
list
(
filter
(
is_parameter
,
program
.
list_vars
()))
paddle
.
fluid
.
core
.
_load_static_dict
(
parameter_file_name
,
parameter_list
,
with
open
(
parameter_file_name
,
'rb'
)
as
f
:
global_scope
())
load_dict
=
pickle
.
load
(
f
)
for
v
in
parameter_list
:
assert
v
.
name
in
load_dict
,
\
"Can not find [{}] in model file [{}]"
.
format
(
v
.
name
,
parameter_file_name
)
set_var
(
v
,
load_dict
[
v
.
name
])
optimizer_var_list
=
list
(
optimizer_var_list
=
list
(
filter
(
is_belong_to_optimizer
,
program
.
list_vars
()))
filter
(
is_belong_to_optimizer
,
program
.
list_vars
()))
...
@@ -1564,6 +1590,12 @@ def load(program, model_path):
...
@@ -1564,6 +1590,12 @@ def load(program, model_path):
if
len
(
optimizer_var_list
)
>
0
:
if
len
(
optimizer_var_list
)
>
0
:
opt_file_name
=
model_path
+
".pdopt"
opt_file_name
=
model_path
+
".pdopt"
assert
os
.
path
.
exists
(
opt_file_name
),
\
assert
os
.
path
.
exists
(
opt_file_name
),
\
"Optimizer file [{}] not exits"
.
format
(
opt_file_name
)
"Optimizer file [{}] not exits"
.
format
(
opt_file_name
)
paddle
.
fluid
.
core
.
_load_static_dict
(
opt_file_name
,
optimizer_var_list
,
global_scope
())
with
open
(
opt_file_name
,
'rb'
)
as
f
:
load_dict
=
pickle
.
load
(
f
)
for
v
in
optimizer_var_list
:
assert
v
.
name
in
load_dict
,
\
"Can not find [{}] in model file [{}]"
.
format
(
v
.
name
,
opt_file_name
)
set_var
(
v
,
load_dict
[
v
.
name
])
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录