Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleRec
提交
5234589f
P
PaddleRec
项目概览
PaddlePaddle
/
PaddleRec
通知
68
Star
12
Fork
5
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
27
列表
看板
标记
里程碑
合并请求
10
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleRec
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
27
Issue
27
列表
看板
标记
里程碑
合并请求
10
合并请求
10
Pages
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
5234589f
编写于
4月 09, 2020
作者:
T
tangwei12
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add save stage
上级
405ec7ed
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
31 addition
and
25 deletion
+31
-25
trainer/single_train.py
trainer/single_train.py
+31
-25
未找到文件。
trainer/single_train.py
浏览文件 @
5234589f
...
@@ -31,17 +31,13 @@ logger = logging.getLogger("fluid")
...
@@ -31,17 +31,13 @@ logger = logging.getLogger("fluid")
logger
.
setLevel
(
logging
.
INFO
)
logger
.
setLevel
(
logging
.
INFO
)
def
need_save
(
epoch_id
,
epoch_interval
,
is_last
=
False
):
if
is_last
:
return
True
return
epoch_id
%
epoch_interval
==
0
class
SingleTrainer
(
Trainer
):
class
SingleTrainer
(
Trainer
):
def
__init__
(
self
,
config
=
None
):
def
__init__
(
self
,
config
=
None
):
Trainer
.
__init__
(
self
,
config
)
Trainer
.
__init__
(
self
,
config
)
self
.
inference_models
=
[]
self
.
increment_models
=
[]
self
.
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
self
.
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
self
.
regist_context_processor
(
'uninit'
,
self
.
instance
)
self
.
regist_context_processor
(
'uninit'
,
self
.
instance
)
...
@@ -116,44 +112,48 @@ class SingleTrainerWithDataset(SingleTrainer):
...
@@ -116,44 +112,48 @@ class SingleTrainerWithDataset(SingleTrainer):
dataset
.
set_filelist
(
file_list
)
dataset
.
set_filelist
(
file_list
)
return
dataset
return
dataset
def
save
(
self
,
epoch_id
):
def
save
(
self
,
epoch_id
,
namespace
):
def
need_save
(
epoch_id
,
epoch_interval
,
is_last
=
False
):
if
is_last
:
return
True
if
epoch_id
==
-
1
:
return
False
return
epoch_id
%
epoch_interval
==
0
def
save_inference_model
():
def
save_inference_model
():
is_save_inference
=
envs
.
get_global_env
(
"save.inference"
,
False
)
save_interval
=
envs
.
get_global_env
(
"save.inference.epoch_interval"
,
-
1
,
namespace
)
if
not
is_save_inference
:
return
save_interval
=
envs
.
get_global_env
(
"save.inference.epoch_interval"
,
1
)
if
not
need_save
(
epoch_id
,
save_interval
,
False
):
if
not
need_save
(
epoch_id
,
save_interval
,
False
):
return
return
feed_varnames
=
envs
.
get_global_env
(
"save.inference.feed_varnames"
,
None
)
print
(
"save inference model is not supported now."
)
fetch_varnames
=
envs
.
get_global_env
(
"save.inference.fetch_varnames"
,
None
)
return
feed_varnames
=
envs
.
get_global_env
(
"save.inference.feed_varnames"
,
None
,
namespace
)
fetch_varnames
=
envs
.
get_global_env
(
"save.inference.fetch_varnames"
,
None
,
namespace
)
fetch_vars
=
[
fluid
.
global_scope
().
vars
[
varname
]
for
varname
in
fetch_varnames
]
fetch_vars
=
[
fluid
.
global_scope
().
vars
[
varname
]
for
varname
in
fetch_varnames
]
dirname
=
envs
.
get_global_env
(
"save.inference.dirname"
,
None
)
dirname
=
envs
.
get_global_env
(
"save.inference.dirname"
,
None
,
namespace
)
assert
dirname
is
not
None
assert
dirname
is
not
None
dirname
=
os
.
path
.
join
(
dirname
,
str
(
epoch_id
))
dirname
=
os
.
path
.
join
(
dirname
,
str
(
epoch_id
))
fluid
.
io
.
save_inference_model
(
dirname
,
feed_varnames
,
fetch_vars
,
self
.
exe
)
fluid
.
io
.
save_inference_model
(
dirname
,
feed_varnames
,
fetch_vars
,
self
.
exe
)
self
.
inference_models
.
append
((
epoch_id
,
dirname
))
def
save_persistables
():
def
save_persistables
():
is_save_increment
=
envs
.
get_global_env
(
"save.increment"
,
False
)
save_interval
=
envs
.
get_global_env
(
"save.increment.epoch_interval"
,
-
1
,
namespace
)
if
not
is_save_increment
:
return
save_interval
=
envs
.
get_global_env
(
"save.increment.epoch_interval"
,
1
)
if
not
need_save
(
epoch_id
,
save_interval
,
False
):
if
not
need_save
(
epoch_id
,
save_interval
,
False
):
return
return
dirname
=
envs
.
get_global_env
(
"save.in
ference.dirname"
,
Non
e
)
dirname
=
envs
.
get_global_env
(
"save.in
crement.dirname"
,
None
,
namespac
e
)
assert
dirname
is
not
None
assert
dirname
is
not
None
dirname
=
os
.
path
.
join
(
dirname
,
str
(
epoch_id
))
dirname
=
os
.
path
.
join
(
dirname
,
str
(
epoch_id
))
fluid
.
io
.
save_persistables
(
self
.
exe
,
dirname
)
fluid
.
io
.
save_persistables
(
self
.
exe
,
dirname
)
self
.
increment_models
.
append
((
epoch_id
,
dirname
))
is_save
=
envs
.
get_global_env
(
"save"
,
False
)
if
not
is_save
:
return
save_persistables
()
save_persistables
()
save_inference_model
()
save_inference_model
()
...
@@ -169,8 +169,14 @@ class SingleTrainerWithDataset(SingleTrainer):
...
@@ -169,8 +169,14 @@ class SingleTrainerWithDataset(SingleTrainer):
fetch_list
=
self
.
metric_extras
[
0
],
fetch_list
=
self
.
metric_extras
[
0
],
fetch_info
=
self
.
metric_extras
[
1
],
fetch_info
=
self
.
metric_extras
[
1
],
print_period
=
self
.
metric_extras
[
2
])
print_period
=
self
.
metric_extras
[
2
])
self
.
save
(
i
,
"train"
)
context
[
'status'
]
=
'infer_pass'
context
[
'status'
]
=
'infer_pass'
def
infer
(
self
,
context
):
def
infer
(
self
,
context
):
context
[
'status'
]
=
'terminal_pass'
context
[
'status'
]
=
'terminal_pass'
def
terminal
(
self
,
context
):
for
model
in
self
.
increment_models
:
print
(
"epoch :{}, dir: {}"
.
format
(
model
[
0
],
model
[
1
]))
context
[
'is_exit'
]
=
True
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录