Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
PaddleRec
提交
5234589f
P
PaddleRec
项目概览
BaiXuePrincess
/
PaddleRec
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleRec
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleRec
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
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")
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
):
def
__init__
(
self
,
config
=
None
):
Trainer
.
__init__
(
self
,
config
)
self
.
inference_models
=
[]
self
.
increment_models
=
[]
self
.
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
self
.
regist_context_processor
(
'uninit'
,
self
.
instance
)
...
...
@@ -116,44 +112,48 @@ class SingleTrainerWithDataset(SingleTrainer):
dataset
.
set_filelist
(
file_list
)
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
():
is_save_inference
=
envs
.
get_global_env
(
"save.inference"
,
False
)
if
not
is_save_inference
:
return
save_interval
=
envs
.
get_global_env
(
"save.inference.epoch_interval"
,
-
1
,
namespace
)
save_interval
=
envs
.
get_global_env
(
"save.inference.epoch_interval"
,
1
)
if
not
need_save
(
epoch_id
,
save_interval
,
False
):
return
feed_varnames
=
envs
.
get_global_env
(
"save.inference.feed_varnames"
,
None
)
fetch_varnames
=
envs
.
get_global_env
(
"save.inference.fetch_varnames"
,
None
)
print
(
"save inference model is not supported now."
)
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
]
dirname
=
envs
.
get_global_env
(
"save.inference.dirname"
,
None
)
dirname
=
envs
.
get_global_env
(
"save.inference.dirname"
,
None
,
namespace
)
assert
dirname
is
not
None
dirname
=
os
.
path
.
join
(
dirname
,
str
(
epoch_id
))
fluid
.
io
.
save_inference_model
(
dirname
,
feed_varnames
,
fetch_vars
,
self
.
exe
)
self
.
inference_models
.
append
((
epoch_id
,
dirname
))
def
save_persistables
():
is_save_increment
=
envs
.
get_global_env
(
"save.increment"
,
False
)
if
not
is_save_increment
:
return
save_interval
=
envs
.
get_global_env
(
"save.increment.epoch_interval"
,
-
1
,
namespace
)
save_interval
=
envs
.
get_global_env
(
"save.increment.epoch_interval"
,
1
)
if
not
need_save
(
epoch_id
,
save_interval
,
False
):
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
dirname
=
os
.
path
.
join
(
dirname
,
str
(
epoch_id
))
fluid
.
io
.
save_persistables
(
self
.
exe
,
dirname
)
is_save
=
envs
.
get_global_env
(
"save"
,
False
)
if
not
is_save
:
return
self
.
increment_models
.
append
((
epoch_id
,
dirname
))
save_persistables
()
save_inference_model
()
...
...
@@ -169,8 +169,14 @@ class SingleTrainerWithDataset(SingleTrainer):
fetch_list
=
self
.
metric_extras
[
0
],
fetch_info
=
self
.
metric_extras
[
1
],
print_period
=
self
.
metric_extras
[
2
])
self
.
save
(
i
,
"train"
)
context
[
'status'
]
=
'infer_pass'
def
infer
(
self
,
context
):
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录