Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
曾经的那一瞬间
Models
提交
90dedf26
M
Models
项目概览
曾经的那一瞬间
/
Models
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
90dedf26
编写于
8月 09, 2022
作者:
R
Ruoxin Sang
提交者:
A. Unique TensorFlower
8月 09, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow `steps_per_loop` in Controller to be passed as a callable.
PiperOrigin-RevId: 466412169
上级
db19ab9b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
47 addition
and
11 deletion
+47
-11
orbit/controller.py
orbit/controller.py
+21
-11
orbit/controller_test.py
orbit/controller_test.py
+26
-0
未找到文件。
orbit/controller.py
浏览文件 @
90dedf26
...
...
@@ -94,7 +94,7 @@ class Controller:
train_actions
:
Optional
[
Iterable
[
Action
]]
=
None
,
eval_actions
:
Optional
[
Iterable
[
Action
]]
=
None
,
# Train related
steps_per_loop
:
Optional
[
int
]
=
None
,
steps_per_loop
:
Optional
[
Union
[
int
,
Callable
[[
int
],
int
]]
]
=
None
,
checkpoint_manager
:
Optional
[
tf
.
train
.
CheckpointManager
]
=
None
,
# Summary related
summary_interval
:
Optional
[
int
]
=
None
,
...
...
@@ -130,8 +130,11 @@ class Controller:
output of `trainer.train`.
eval_actions: Optional `orbit.Action`s to call after each evaluation.
These will be called with the output of `evaluator.evaluate`.
steps_per_loop: The number of steps to run in each inner loop of training
(passed as the `num_steps` parameter of `trainer.train`).
steps_per_loop: Optional integer to indicate the number of steps to run in
each inner loop of training (passed as the `num_steps` parameter of
`trainer.train`). It can be also a callable which takes the current
global step value as input and returns the number of steps to run as
output.
checkpoint_manager: An instance of `tf.train.CheckpointManager`. If
provided and there are checkpoints in the associated model directory,
the model will be restored from the most recent checkpoint inside this
...
...
@@ -152,7 +155,7 @@ class Controller:
Raises:
ValueError: If both `trainer` and `evaluator` are `None`.
ValueError: If `steps_per_loop` is not a positive integer.
ValueError: If `steps_per_loop` is not a positive integer
or a callable
.
ValueError: If `summary_interval` is not a positive integer or is not
divisible by `steps_per_loop`.
"""
...
...
@@ -163,15 +166,18 @@ class Controller:
if
steps_per_loop
is
None
:
raise
ValueError
(
"`steps_per_loop` is required when `trainer` is provided."
)
elif
not
isinstance
(
steps_per_loop
,
int
)
or
steps_per_loop
<
1
:
elif
not
callable
(
steps_per_loop
)
and
(
not
isinstance
(
steps_per_loop
,
int
)
or
steps_per_loop
<
1
):
raise
ValueError
(
f
"`steps_per_loop` (
{
steps_per_loop
}
) must be a positive integer."
)
f
"`steps_per_loop` (
{
steps_per_loop
}
) must be a positive integer "
"or a callable."
)
if
summary_interval
is
not
None
:
if
summary_interval
<=
0
:
raise
ValueError
(
f
"`summary_interval` (
{
summary_interval
}
) must be larger than 0."
)
elif
summary_interval
%
steps_per_loop
!=
0
:
elif
not
callable
(
steps_per_loop
)
and
(
summary_interval
%
steps_per_loop
!=
0
):
raise
ValueError
(
f
"`summary interval` (
{
summary_interval
}
) must be a multiple "
f
"of `steps_per_loop` (
{
steps_per_loop
}
)."
)
...
...
@@ -192,10 +198,10 @@ class Controller:
if
self
.
trainer
is
not
None
:
self
.
step_timer
=
None
self
.
steps_per_loop
=
steps_per_loop
self
.
summary_interval
=
summary_interval
self
.
summary_manager
=
utils
.
SummaryManager
(
summary_dir
,
tf
.
summary
.
scalar
,
global_step
=
self
.
global_step
)
self
.
_steps_per_loop
=
steps_per_loop
if
self
.
evaluator
is
not
None
:
eval_summary_dir
=
eval_summary_dir
or
summary_dir
...
...
@@ -316,9 +322,6 @@ class Controller:
results in a shorter inner loop than specified by `steps_per_loop`
setting. If None, evaluation will only be performed after training is
complete.
Raises:
ValueError: If eval_interval is not a multiple of self.steps_per_loop.
"""
self
.
_require
(
"trainer"
,
for_method
=
"train_and_evaluate"
)
self
.
_require
(
"evaluator"
,
for_method
=
"train_and_evaluate"
)
...
...
@@ -410,6 +413,13 @@ class Controller:
self
.
_require
(
"checkpoint_manager"
,
for_method
=
"save_checkpoint"
)
self
.
_maybe_save_checkpoint
(
check_interval
=
False
)
@
property
def
steps_per_loop
(
self
):
"""Returns current steps_per_loop value in a training loop."""
if
callable
(
self
.
_steps_per_loop
):
return
self
.
_steps_per_loop
(
self
.
global_step
.
numpy
())
return
self
.
_steps_per_loop
def
_train_n_steps
(
self
,
num_steps
:
int
):
"""Runs training for `num_steps` steps.
...
...
orbit/controller_test.py
浏览文件 @
90dedf26
...
...
@@ -770,6 +770,32 @@ class ControllerTest(tf.test.TestCase, parameterized.TestCase):
self
.
assertIn
(
"eval_loss"
,
output
)
self
.
assertGreaterEqual
(
output
[
"eval_loss"
],
0
)
def
test_step_per_loop_callable
(
self
):
test_runner
=
TestRunner
()
checkpoint
=
tf
.
train
.
Checkpoint
(
model
=
test_runner
.
model
,
optimizer
=
test_runner
.
optimizer
)
checkpoint_manager
=
tf
.
train
.
CheckpointManager
(
checkpoint
,
self
.
model_dir
,
max_to_keep
=
None
,
step_counter
=
test_runner
.
global_step
,
checkpoint_interval
=
10
)
def
steps_per_loop_fn
(
global_step
):
if
global_step
>
4
:
return
4
return
2
test_controller
=
controller
.
Controller
(
trainer
=
test_runner
,
global_step
=
test_runner
.
global_step
,
steps_per_loop
=
steps_per_loop_fn
,
checkpoint_manager
=
checkpoint_manager
,
)
test_controller
.
train
(
steps
=
10
)
self
.
assertEqual
(
test_runner
.
global_step
,
10
)
if
__name__
==
"__main__"
:
tf
.
test
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录