Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
b044724d
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
b044724d
编写于
5月 22, 2018
作者:
T
tangwei12
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update fluid Train API param_path to checkpoint_config
上级
e901de66
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
40 addition
and
10 deletion
+40
-10
python/paddle/fluid/trainer.py
python/paddle/fluid/trainer.py
+40
-10
未找到文件。
python/paddle/fluid/trainer.py
浏览文件 @
b044724d
...
...
@@ -27,11 +27,8 @@ import parallel_executor
from
transpiler
import
distribute_transpiler
__all__
=
[
'Trainer'
,
'BeginEpochEvent'
,
'EndEpochEvent'
,
'BeginStepEvent'
,
'EndStepEvent'
,
'Trainer'
,
'BeginEpochEvent'
,
'EndEpochEvent'
,
'BeginStepEvent'
,
'EndStepEvent'
,
'CheckpointConfig'
]
...
...
@@ -59,6 +56,17 @@ class EndStepEvent(object):
self
.
metrics
=
metrics
class
CheckpointConfig
(
object
):
def
__init__
(
self
,
checkpoint_dir
=
None
,
max_num_checkpoints
=
3
,
save_interval_secs
=
600
):
if
checkpoint_dir
is
None
:
self
.
checkpoint_dir
=
os
.
getcwd
()
self
.
max_num_checkpoints
=
max_num_checkpoints
self
.
save_interval_secs
=
save_interval_secs
def
check_and_get_place
(
place
):
"""
Check the type of place or get the default place
...
...
@@ -97,9 +105,9 @@ class Trainer(object):
def
__init__
(
self
,
train_func
,
optimizer
,
param_path
=
None
,
place
=
None
,
parallel
=
False
):
parallel
=
False
,
checkpoint_config
=
None
):
self
.
__stop
=
False
self
.
parallel
=
parallel
# 1. we need to generate a framework.Program by calling
...
...
@@ -108,6 +116,16 @@ class Trainer(object):
if
not
isinstance
(
optimizer
,
opt_module
.
Optimizer
):
raise
TypeError
(
"The optimizer should be an instance of Optimizer"
)
# config for checkpoint
# only chief worker will save variables
self
.
chief
=
True
self
.
checkpoint
=
checkpoint_config
if
self
.
checkpoint
and
not
isinstance
(
self
.
checkpoint
,
CheckpointConfig
):
raise
TypeError
(
"The checkpoint_config shoule be an instance of CheckpointConfig"
)
self
.
scope
=
core
.
Scope
()
self
.
startup_program
=
framework
.
Program
()
...
...
@@ -136,9 +154,10 @@ class Trainer(object):
exe
=
executor
.
Executor
(
place
)
exe
.
run
(
self
.
startup_program
)
if
param_path
:
# load params from param_path into scope
io
.
load_persistables
(
exe
,
dirname
=
param_path
)
if
self
.
checkpoint
:
exe
=
executor
.
Executor
(
place
)
io
.
load_checkpoint
(
exe
,
self
.
checkpoint
.
checkpoint_dir
,
self
.
startup_program
)
def
_transpile_nccl2_dist
(
self
):
# PADDLE_TRAINER_IPS
...
...
@@ -146,6 +165,7 @@ class Trainer(object):
self
.
nccl_id_var
=
None
else
:
self
.
trainer_id
=
int
(
os
.
getenv
(
"PADDLE_TRAINER_ID"
))
self
.
chief
=
self
.
trainer_id
==
0
port
=
os
.
getenv
(
"PADDLE_PSERVER_PORT"
)
worker_ips
=
os
.
getenv
(
"PADDLE_TRAINER_IPS"
)
worker_endpoints
=
[]
...
...
@@ -194,6 +214,7 @@ class Trainer(object):
# the unique trainer id, starting from 0, needed by trainer
# only
trainer_id
=
int
(
os
.
getenv
(
"PADDLE_TRAINER_ID"
,
"0"
))
self
.
chief
=
self
.
trainer_id
==
0
# the role, should be either PSERVER or TRAINER
training_role
=
os
.
getenv
(
"PADDLE_TRAINING_ROLE"
)
with
self
.
_prog_and_scope_guard
():
...
...
@@ -263,6 +284,14 @@ class Trainer(object):
exe
=
executor
.
Executor
(
self
.
place
)
io
.
save_persistables
(
exe
,
dirname
=
param_path
)
def
_save_checkpoint
(
self
):
if
self
.
checkpoint
and
self
.
chief
:
exe
=
executor
.
Executor
(
self
.
place
)
io
.
save_checkpoint
(
exe
,
self
.
checkpoint
.
checkpoint_dir
,
self
.
checkpoint
.
max_num_checkpoints
,
self
.
checkpoint
.
save_interval_secs
,
self
.
train_program
)
@
contextlib
.
contextmanager
def
_prog_and_scope_guard
(
self
):
with
framework
.
program_guard
(
...
...
@@ -309,6 +338,7 @@ class Trainer(object):
else
:
metrics
=
exe
.
run
(
feed
=
data
,
fetch_list
=
[])
event_handler
(
EndStepEvent
(
epoch_id
,
step_id
,
metrics
))
self
.
_save_checkpoint
()
event_handler
(
EndEpochEvent
(
epoch_id
))
def
_test_by_executor
(
self
,
reader
,
feed_order
,
fetch_list
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录