Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
975aeee7
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
975aeee7
编写于
4月 16, 2019
作者:
L
lujun
提交者:
GitHub
4月 16, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16904 from junjun315/hot-fix-saveload
fix save and load bug, test=release/1.4
上级
24faf9e8
d6d18ad4
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
35 addition
and
15 deletion
+35
-15
paddle/fluid/imperative/layer.h
paddle/fluid/imperative/layer.h
+5
-1
python/paddle/fluid/dygraph/checkpoint.py
python/paddle/fluid/dygraph/checkpoint.py
+17
-7
python/paddle/fluid/dygraph/layers.py
python/paddle/fluid/dygraph/layers.py
+4
-1
python/paddle/fluid/tests/unittests/test_imperative_checkpoint.py
...addle/fluid/tests/unittests/test_imperative_checkpoint.py
+9
-6
未找到文件。
paddle/fluid/imperative/layer.h
浏览文件 @
975aeee7
...
@@ -464,8 +464,12 @@ class PYBIND11_HIDDEN RuntimeInferVarTypeContext
...
@@ -464,8 +464,12 @@ class PYBIND11_HIDDEN RuntimeInferVarTypeContext
void
SetType
(
const
std
::
string
&
name
,
void
SetType
(
const
std
::
string
&
name
,
framework
::
proto
::
VarType
::
Type
type
)
override
{
framework
::
proto
::
VarType
::
Type
type
)
override
{
if
(
name
==
"kLookupTablePath"
)
{
VLOG
(
2
)
<<
"SUPER UGLY FIX, remove this when move imperative mode in C++"
;
}
else
{
var_set_
[
name
]
->
SetType
(
type
);
var_set_
[
name
]
->
SetType
(
type
);
}
}
}
framework
::
proto
::
VarType
::
Type
GetDataType
(
framework
::
proto
::
VarType
::
Type
GetDataType
(
const
std
::
string
&
name
)
const
override
{
const
std
::
string
&
name
)
const
override
{
...
...
python/paddle/fluid/dygraph/checkpoint.py
浏览文件 @
975aeee7
...
@@ -113,14 +113,17 @@ def load_persistables(vardict, dirname, filename=None):
...
@@ -113,14 +113,17 @@ def load_persistables(vardict, dirname, filename=None):
def
_save_var_to_file
(
stat_dict
,
file_dir
,
file_name
):
def
_save_var_to_file
(
stat_dict
,
file_dir
,
file_name
):
save_block
=
default_main_program
().
global_block
()
save_block
=
default_main_program
().
global_block
()
save_var_map
=
{}
save_var_map
=
{}
for
each_var
in
stat_dict
.
items
():
for
var_key
,
each_var
in
stat_dict
.
items
():
save_var_map
[
each_var
.
name
]
=
each_var
save_var_map
[
each_var
.
name
]
=
each_var
if
file_name
is
None
:
if
file_name
is
None
:
save_block
.
append_op
(
save_block
.
append_op
(
type
=
'save'
,
type
=
'save'
,
inputs
=
{
'X'
:
[
each_var
]},
inputs
=
{
'X'
:
[
each_var
]},
outputs
=
{},
outputs
=
{},
attrs
=
{
'file_path'
:
os
.
path
.
join
(
file_dir
,
each_var
.
name
)})
attrs
=
{
'file_path'
:
os
.
path
.
join
(
file_dir
,
os
.
path
.
normpath
(
each_var
.
name
))
})
if
file_name
is
not
None
:
if
file_name
is
not
None
:
save_var_list
=
[]
save_var_list
=
[]
...
@@ -131,14 +134,16 @@ def _save_var_to_file(stat_dict, file_dir, file_name):
...
@@ -131,14 +134,16 @@ def _save_var_to_file(stat_dict, file_dir, file_name):
type
=
'save_combine'
,
type
=
'save_combine'
,
inputs
=
{
'X'
:
save_var_list
},
inputs
=
{
'X'
:
save_var_list
},
outputs
=
{},
outputs
=
{},
attrs
=
{
'file_path'
:
os
.
path
.
join
(
file_dir
,
file_name
)})
attrs
=
{
'file_path'
:
os
.
path
.
join
(
file_dir
,
os
.
path
.
normpath
(
file_name
))
})
def
_load_var_from_file
(
stat_dict
,
file_dir
,
file_name
):
def
_load_var_from_file
(
stat_dict
,
file_dir
,
file_name
):
load_block
=
default_main_program
().
global_block
()
load_block
=
default_main_program
().
global_block
()
load_var_map
=
{}
load_var_map
=
{}
for
each_var
in
stat_dict
.
items
():
for
var_key
,
each_var
in
stat_dict
.
items
():
assert
isinstance
(
each_var
,
Variable
)
assert
isinstance
(
each_var
,
Variable
)
if
each_var
.
type
==
core
.
VarDesc
.
VarType
.
RAW
:
if
each_var
.
type
==
core
.
VarDesc
.
VarType
.
RAW
:
continue
continue
...
@@ -148,7 +153,10 @@ def _load_var_from_file(stat_dict, file_dir, file_name):
...
@@ -148,7 +153,10 @@ def _load_var_from_file(stat_dict, file_dir, file_name):
type
=
'load'
,
type
=
'load'
,
inputs
=
{},
inputs
=
{},
outputs
=
{
'Out'
:
[
new_var
]},
outputs
=
{
'Out'
:
[
new_var
]},
attrs
=
{
'file_path'
:
os
.
path
.
join
(
file_dir
,
each_var
.
name
)})
attrs
=
{
'file_path'
:
os
.
path
.
join
(
file_dir
,
os
.
path
.
normpath
(
each_var
.
name
))
})
load_var_map
[
new_var
.
name
]
=
new_var
load_var_map
[
new_var
.
name
]
=
new_var
...
@@ -161,7 +169,9 @@ def _load_var_from_file(stat_dict, file_dir, file_name):
...
@@ -161,7 +169,9 @@ def _load_var_from_file(stat_dict, file_dir, file_name):
type
=
'load_combine'
,
type
=
'load_combine'
,
inputs
=
{},
inputs
=
{},
outputs
=
{
"Out"
:
load_var_list
},
outputs
=
{
"Out"
:
load_var_list
},
attrs
=
{
'file_path'
:
os
.
path
.
join
(
file_dir
,
file_name
)})
attrs
=
{
'file_path'
:
os
.
path
.
join
(
file_dir
,
os
.
path
.
normpath
(
file_name
))
})
for
res_var
in
load_var_list
:
for
res_var
in
load_var_list
:
load_var_map
[
res_var
.
name
]
=
res_var
load_var_map
[
res_var
.
name
]
=
res_var
...
@@ -175,5 +185,5 @@ def _clone_var_in_block_(block, var):
...
@@ -175,5 +185,5 @@ def _clone_var_in_block_(block, var):
shape
=
var
.
shape
,
shape
=
var
.
shape
,
dtype
=
var
.
dtype
,
dtype
=
var
.
dtype
,
type
=
var
.
type
,
type
=
var
.
type
,
lod_level
=
var
.
lod_level
,
lod_level
=
0
,
persistable
=
True
)
persistable
=
True
)
python/paddle/fluid/dygraph/layers.py
浏览文件 @
975aeee7
...
@@ -246,7 +246,10 @@ class Layer(core.Layer):
...
@@ -246,7 +246,10 @@ class Layer(core.Layer):
def
load_dict
(
self
,
stat_dict
,
include_sublayers
=
True
):
def
load_dict
(
self
,
stat_dict
,
include_sublayers
=
True
):
for
name
,
item
in
self
.
__dict__
.
get
(
'_parameters'
,
None
).
items
():
for
name
,
item
in
self
.
__dict__
.
get
(
'_parameters'
,
None
).
items
():
if
item
.
name
in
stat_dict
:
if
item
.
name
in
stat_dict
:
self
.
__setattr__
(
name
,
stat_dict
[
item
.
name
])
var
=
item
.
_ivar
.
value
()
tensor
=
var
.
get_tensor
()
tensor
.
set
(
stat_dict
[
item
.
name
].
numpy
(),
framework
.
_current_expected_place
())
if
include_sublayers
:
if
include_sublayers
:
for
layer_name
,
layer_item
in
self
.
_sub_layers
.
items
():
for
layer_name
,
layer_item
in
self
.
_sub_layers
.
items
():
...
...
python/paddle/fluid/tests/unittests/test_imperative_checkpoint.py
浏览文件 @
975aeee7
...
@@ -99,7 +99,7 @@ class MNIST(fluid.Layer):
...
@@ -99,7 +99,7 @@ class MNIST(fluid.Layer):
class
TestDygraphCheckpoint
(
unittest
.
TestCase
):
class
TestDygraphCheckpoint
(
unittest
.
TestCase
):
def
save_load_persistables
(
self
):
def
test_
save_load_persistables
(
self
):
seed
=
90
seed
=
90
epoch_num
=
1
epoch_num
=
1
...
@@ -135,23 +135,26 @@ class TestDygraphCheckpoint(unittest.TestCase):
...
@@ -135,23 +135,26 @@ class TestDygraphCheckpoint(unittest.TestCase):
avg_loss
.
backward
()
avg_loss
.
backward
()
sgd
.
minimize
(
avg_loss
)
sgd
.
minimize
(
avg_loss
)
fluid
.
dygraph
.
save_persistables
(
mnist
,
"save_dir"
)
fluid
.
dygraph
.
save_persistables
(
mnist
.
state_dict
(),
"save_dir"
)
mnist
.
clear_gradients
()
mnist
.
clear_gradients
()
for
param
in
mnist
.
parameters
():
for
param
in
mnist
.
parameters
():
dy_param_init_value
[
param
.
name
]
=
param
.
numpy
()
dy_param_init_value
[
param
.
name
]
=
param
.
numpy
()
mnist
.
load_dict
(
mnist
.
load_dict
(
fluid
.
dygraph
.
load_persistables
(
mnist
,
"save_dir"
))
fluid
.
dygraph
.
load_persistables
(
mnist
.
state_dict
(),
"save_dir"
))
restore
=
mnist
.
parameters
()
restore
=
mnist
.
parameters
()
self
.
assertEqual
(
len
(
dy_param_init_value
),
len
(
restore
))
self
.
assertEqual
(
len
(
dy_param_init_value
),
len
(
restore
))
for
value
in
restore
:
for
value
in
restore
:
self
.
assertTrue
(
self
.
assertTrue
(
np
.
allclose
(
value
,
dy_param_init_value
[
value
.
name
]))
np
.
allclose
(
value
.
numpy
(),
dy_param_init_value
[
self
.
assertTrue
(
np
.
isfinite
(
value
.
all
()))
value
.
name
]))
self
.
assertFalse
(
np
.
isnan
(
value
.
any
()))
self
.
assertTrue
(
np
.
isfinite
(
value
.
numpy
().
all
()))
self
.
assertFalse
(
np
.
isnan
(
value
.
numpy
().
any
()))
step
+=
1
step
+=
1
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录