Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
89921bf1
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 2 年 前同步成功
通知
285
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
89921bf1
编写于
9月 21, 2019
作者:
Z
zhangxuefei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update autofinetune
1. add the options of the finetuned task related 2. move the checkpoint_dir to saved_params_dir
上级
82847247
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
74 addition
and
44 deletion
+74
-44
paddlehub/autofinetune/autoft.py
paddlehub/autofinetune/autoft.py
+5
-5
paddlehub/autofinetune/evaluator.py
paddlehub/autofinetune/evaluator.py
+30
-27
paddlehub/commands/autofinetune.py
paddlehub/commands/autofinetune.py
+39
-12
未找到文件。
paddlehub/autofinetune/autoft.py
浏览文件 @
89921bf1
...
...
@@ -164,15 +164,15 @@ class BaseTuningStrategy(object):
params_cudas_dirs
=
[]
solution_results
=
[]
cnt
=
0
solutions_
ckpt
dirs
=
{}
solutions_
model
dirs
=
{}
mkdir
(
output_dir
)
for
idx
,
solution
in
enumerate
(
solutions
):
cuda
=
self
.
is_cuda_free
[
"free"
][
0
]
ckptdir
=
output_dir
+
"/ckpt-"
+
str
(
idx
)
modeldir
=
output_dir
+
"/model-"
+
str
(
idx
)
+
"/"
log_file
=
output_dir
+
"/log-"
+
str
(
idx
)
+
".info"
params_cudas_dirs
.
append
([
solution
,
cuda
,
ckpt
dir
,
log_file
])
solutions_
ckptdirs
[
tuple
(
solution
)]
=
ckpt
dir
params_cudas_dirs
.
append
([
solution
,
cuda
,
model
dir
,
log_file
])
solutions_
modeldirs
[
tuple
(
solution
)]
=
model
dir
self
.
is_cuda_free
[
"free"
].
remove
(
cuda
)
self
.
is_cuda_free
[
"busy"
].
append
(
cuda
)
if
len
(
params_cudas_dirs
...
...
@@ -190,7 +190,7 @@ class BaseTuningStrategy(object):
self
.
feedback
(
solutions
,
solution_results
)
return
solutions_
ckpt
dirs
return
solutions_
model
dirs
class
HAZero
(
BaseTuningStrategy
):
...
...
paddlehub/autofinetune/evaluator.py
浏览文件 @
89921bf1
...
...
@@ -36,11 +36,12 @@ else:
class
BaseEvaluator
(
object
):
def
__init__
(
self
,
params_file
,
finetunee_script
):
def
__init__
(
self
,
params_file
,
finetunee_script
,
options_str
=
""
):
with
io
.
open
(
params_file
,
'r'
,
encoding
=
'utf8'
)
as
f
:
self
.
params
=
yaml
.
safe_load
(
f
)
self
.
finetunee_script
=
finetunee_script
self
.
model_rewards
=
{}
self
.
options_str
=
options_str
def
get_init_params
(
self
):
init_params
=
[]
...
...
@@ -108,13 +109,14 @@ class BaseEvaluator(object):
class
FullTrailEvaluator
(
BaseEvaluator
):
def
__init__
(
self
,
params_file
,
finetunee_script
):
super
(
FullTrailEvaluator
,
self
).
__init__
(
params_file
,
finetunee_script
)
def
__init__
(
self
,
params_file
,
finetunee_script
,
options_str
=
""
):
super
(
FullTrailEvaluator
,
self
).
__init__
(
params_file
,
finetunee_script
,
options_str
=
options_str
)
def
run
(
self
,
*
args
):
params
=
args
[
0
][
0
]
num_cuda
=
args
[
0
][
1
]
ckpt
_dir
=
args
[
0
][
2
]
saved_params
_dir
=
args
[
0
][
2
]
log_file
=
args
[
0
][
3
]
params
=
self
.
convert_params
(
params
)
if
not
self
.
is_valid_params
(
params
):
...
...
@@ -125,12 +127,11 @@ class FullTrailEvaluator(BaseEvaluator):
f
.
close
()
if
is_windows
():
run_cmd
=
"set FLAGS_eager_delete_tensor_gb=0.0&set CUDA_VISIBLE_DEVICES=%s&python -u %s --
checkpoint_dir=
%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
ckpt_dir
,
param
_str
,
log_file
)
run_cmd
=
"set FLAGS_eager_delete_tensor_gb=0.0&set CUDA_VISIBLE_DEVICES=%s&python -u %s --
saved_params_dir=%s
%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
saved_params_dir
,
param_str
,
self
.
options
_str
,
log_file
)
else
:
run_cmd
=
"export FLAGS_eager_delete_tensor_gb=0.0; export CUDA_VISIBLE_DEVICES=%s; python -u %s --checkpoint_dir=%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
ckpt_dir
,
param_str
,
log_file
)
run_cmd
=
"export FLAGS_eager_delete_tensor_gb=0.0; export CUDA_VISIBLE_DEVICES=%s; python -u %s --saved_params_dir=%s %s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
saved_params_dir
,
param_str
,
self
.
options_str
,
log_file
)
try
:
os
.
system
(
run_cmd
)
with
open
(
log_file
,
"r"
)
as
f
:
...
...
@@ -142,20 +143,21 @@ class FullTrailEvaluator(BaseEvaluator):
%
param_str
.
replace
(
"--"
,
""
))
eval_result
=
0.0
reward
=
self
.
get_reward
(
eval_result
)
self
.
model_rewards
[
ckpt
_dir
]
=
reward
self
.
model_rewards
[
saved_params
_dir
]
=
reward
return
reward
class
ModelBasedEvaluator
(
BaseEvaluator
):
def
__init__
(
self
,
params_file
,
finetunee_script
):
super
(
ModelBasedEvaluator
,
self
).
__init__
(
params_file
,
finetunee_script
)
self
.
half_best_model_ckpt
=
[]
def
__init__
(
self
,
params_file
,
finetunee_script
,
options_str
=
""
):
super
(
ModelBasedEvaluator
,
self
).
__init__
(
params_file
,
finetunee_script
,
options_str
=
options_str
)
self
.
half_best_model_path
=
[]
self
.
run_count
=
0
def
run
(
self
,
*
args
):
params
=
args
[
0
][
0
]
num_cuda
=
args
[
0
][
1
]
ckpt
_dir
=
args
[
0
][
2
]
saved_params
_dir
=
args
[
0
][
2
]
log_file
=
args
[
0
][
3
]
params
=
self
.
convert_params
(
params
)
if
not
self
.
is_valid_params
(
params
):
...
...
@@ -165,22 +167,23 @@ class ModelBasedEvaluator(BaseEvaluator):
f
=
open
(
log_file
,
"w"
)
f
.
close
()
if
len
(
self
.
half_best_model_
ckpt
)
>
0
:
model_path
=
self
.
half_best_model_
ckpt
[
self
.
run_count
%
len
(
self
.
half_best_model_
ckpt
)]
+
"/best_model"
if
len
(
self
.
half_best_model_
path
)
>
0
:
model_path
=
self
.
half_best_model_
path
[
self
.
run_count
%
len
(
self
.
half_best_model_
path
)]
if
is_windows
():
run_cmd
=
"set FLAGS_eager_delete_tensor_gb=0.0&set CUDA_VISIBLE_DEVICES=%s&python -u %s --epochs=1 --model_path %s --
checkpoint_dir=
%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
model_path
,
ckpt_dir
,
param
_str
,
log_file
)
run_cmd
=
"set FLAGS_eager_delete_tensor_gb=0.0&set CUDA_VISIBLE_DEVICES=%s&python -u %s --epochs=1 --model_path %s --
saved_params_dir=%s
%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
model_path
,
saved_params_dir
,
param_str
,
self
.
options
_str
,
log_file
)
else
:
run_cmd
=
"export FLAGS_eager_delete_tensor_gb=0.0; export CUDA_VISIBLE_DEVICES=%s; python -u %s --epochs=1 --model_path %s --checkpoint_dir=%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
model_path
,
ckpt_dir
,
param_str
,
log_file
)
run_cmd
=
"export FLAGS_eager_delete_tensor_gb=0.0; export CUDA_VISIBLE_DEVICES=%s; python -u %s --epochs=1 --model_path %s --saved_params_dir=%s %s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
model_path
,
saved_params_dir
,
param_str
,
self
.
options_str
,
log_file
)
else
:
if
is_windows
():
run_cmd
=
"set FLAGS_eager_delete_tensor_gb=0.0&set CUDA_VISIBLE_DEVICES=%s&python -u %s --
checkpoint_dir=
%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
ckpt_dir
,
param
_str
,
log_file
)
run_cmd
=
"set FLAGS_eager_delete_tensor_gb=0.0&set CUDA_VISIBLE_DEVICES=%s&python -u %s --
saved_params_dir=%s
%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
saved_params_dir
,
param_str
,
self
.
options
_str
,
log_file
)
else
:
run_cmd
=
"export FLAGS_eager_delete_tensor_gb=0.0; export CUDA_VISIBLE_DEVICES=%s; python -u %s --
checkpoint_dir=
%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
ckpt_dir
,
param
_str
,
log_file
)
run_cmd
=
"export FLAGS_eager_delete_tensor_gb=0.0; export CUDA_VISIBLE_DEVICES=%s; python -u %s --
saved_params_dir=%s
%s %s >%s 2>&1"
%
\
(
num_cuda
,
self
.
finetunee_script
,
saved_params_dir
,
param_str
,
self
.
options
_str
,
log_file
)
self
.
run_count
+=
1
try
:
...
...
@@ -194,7 +197,7 @@ class ModelBasedEvaluator(BaseEvaluator):
%
param_str
.
replace
(
"--"
,
""
))
eval_result
=
0.0
reward
=
self
.
get_reward
(
eval_result
)
self
.
model_rewards
[
ckpt
_dir
]
=
reward
self
.
model_rewards
[
saved_params
_dir
]
=
reward
return
reward
def
new_round
(
self
):
...
...
@@ -202,7 +205,7 @@ class ModelBasedEvaluator(BaseEvaluator):
half_size
=
int
(
len
(
self
.
model_rewards
)
/
2
)
if
half_size
<
1
:
half_size
=
1
self
.
half_best_model_
ckpt
=
list
({
self
.
half_best_model_
path
=
list
({
key
for
key
in
sorted
(
self
.
model_rewards
,
key
=
self
.
model_rewards
.
get
,
reverse
=
False
)
...
...
paddlehub/commands/autofinetune.py
浏览文件 @
89921bf1
...
...
@@ -91,6 +91,22 @@ class AutoFineTuneCommand(BaseCommand):
type
=
str
,
default
=
"HAZero"
,
help
=
"Choices: HAZero or PSHE2."
)
self
.
arg_config_group
.
add_argument
(
'opts'
,
help
=
'See utils/config.py for all options'
,
default
=
None
,
nargs
=
argparse
.
REMAINDER
)
def
convert_to_other_options
(
self
,
config_list
):
if
len
(
config_list
)
%
2
!=
0
:
raise
ValueError
(
"Command for finetuned task options config format error! Please check it: {}"
.
format
(
config_list
))
options_str
=
""
for
key
,
value
in
zip
(
config_list
[
0
::
2
],
config_list
[
1
::
2
]):
options_str
+=
"--"
+
key
+
"="
+
value
+
" "
print
(
options_str
)
return
options_str
def
execute
(
self
,
argv
):
if
not
argv
:
...
...
@@ -109,6 +125,11 @@ class AutoFineTuneCommand(BaseCommand):
description
=
"Autofintune configuration for controlling autofinetune behavior, not required"
)
self
.
arg_finetuned_task_group
=
self
.
parser
.
add_argument_group
(
title
=
"Finetuned task config options"
,
description
=
"Finetuned task configuration for controlling finetuned task behavior, not required"
)
self
.
add_params_file_arg
()
self
.
add_autoft_config_arg
()
...
...
@@ -118,12 +139,20 @@ class AutoFineTuneCommand(BaseCommand):
return
False
self
.
args
=
self
.
parser
.
parse_args
(
argv
[
1
:])
options_str
=
""
if
self
.
args
.
opts
is
not
None
:
options_str
=
self
.
convert_to_other_options
(
self
.
args
.
opts
)
if
self
.
args
.
evaluate_choice
.
lower
()
==
"fulltrail"
:
evaluator
=
FullTrailEvaluator
(
self
.
args
.
param_file
,
self
.
fintunee_script
)
evaluator
=
FullTrailEvaluator
(
self
.
args
.
param_file
,
self
.
fintunee_script
,
options_str
=
options_str
)
elif
self
.
args
.
evaluate_choice
.
lower
()
==
"modelbased"
:
evaluator
=
ModelBasedEvaluator
(
self
.
args
.
param_file
,
self
.
fintunee_script
)
evaluator
=
ModelBasedEvaluator
(
self
.
args
.
param_file
,
self
.
fintunee_script
,
options_str
=
options_str
)
else
:
raise
ValueError
(
"The evaluate %s is not defined!"
%
self
.
args
.
evaluate_choice
)
...
...
@@ -145,13 +174,13 @@ class AutoFineTuneCommand(BaseCommand):
self
.
args
.
tuning_strategy
)
run_round_cnt
=
0
solutions_
ckpt
dirs
=
{}
solutions_
model
dirs
=
{}
print
(
"PaddleHub Autofinetune starts."
)
while
(
not
autoft
.
is_stop
())
and
run_round_cnt
<
self
.
args
.
round
:
print
(
"PaddleHub Autofinetune starts round at %s."
%
run_round_cnt
)
output_dir
=
autoft
.
_output_dir
+
"/round"
+
str
(
run_round_cnt
)
res
=
autoft
.
step
(
output_dir
)
solutions_
ckpt
dirs
.
update
(
res
)
solutions_
model
dirs
.
update
(
res
)
evaluator
.
new_round
()
run_round_cnt
=
run_round_cnt
+
1
print
(
"PaddleHub Autofinetune ends."
)
...
...
@@ -164,17 +193,15 @@ class AutoFineTuneCommand(BaseCommand):
print
(
"%s=%s"
%
(
hparam_name
,
best_hparams
[
index
]))
f
.
write
(
hparam_name
+
"
\t
:
\t
"
+
str
(
best_hparams
[
index
])
+
"
\n
"
)
f
.
write
(
"
\n\n\n
"
)
f
.
write
(
"
\t
"
.
join
(
autoft
.
hparams_name_list
)
+
"
\t
output_dir
\n\n
"
)
logger
.
info
(
"The checkpont directory of programs ran with hyperparamemters searched are saved as log_file.txt ."
)
f
.
write
(
"
\t
"
.
join
(
autoft
.
hparams_name_list
)
+
"
\t
saved_params_dir
\n\n
"
)
print
(
"The checkpont directory of programs ran with hyperparamemters searched are saved as log_file.txt ."
)
for
solution
,
ckptdir
in
solutions_ckpt
dirs
.
items
():
for
solution
,
modeldir
in
solutions_model
dirs
.
items
():
param
=
evaluator
.
convert_params
(
solution
)
param
=
[
str
(
p
)
for
p
in
param
]
f
.
write
(
"
\t
"
.
join
(
param
)
+
"
\t
"
+
ckpt
dir
+
"
\n\n
"
)
f
.
write
(
"
\t
"
.
join
(
param
)
+
"
\t
"
+
model
dir
+
"
\n\n
"
)
return
True
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录