Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
seminar-breakout
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
seminar-breakout
Commits
11ca8b2c
Commit
11ca8b2c
authored
Oct 31, 2020
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ScheduleParam: set at beginning
parent
36bdc187
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
7 deletions
+29
-7
tensorpack/callbacks/param.py
tensorpack/callbacks/param.py
+21
-6
tensorpack/callbacks/param_test.py
tensorpack/callbacks/param_test.py
+8
-1
No files found.
tensorpack/callbacks/param.py
View file @
11ca8b2c
...
@@ -234,7 +234,8 @@ class ScheduledHyperParamSetter(HyperParamSetter):
...
@@ -234,7 +234,8 @@ class ScheduledHyperParamSetter(HyperParamSetter):
Set hyperparameters by a predefined epoch-based schedule.
Set hyperparameters by a predefined epoch-based schedule.
"""
"""
def
__init__
(
self
,
param
,
schedule
,
interp
=
None
,
step_based
=
False
):
def
__init__
(
self
,
param
,
schedule
,
interp
=
None
,
step_based
=
False
,
set_at_beginning
=
True
):
"""
"""
Args:
Args:
param: same as in :class:`HyperParamSetter`.
param: same as in :class:`HyperParamSetter`.
...
@@ -250,6 +251,14 @@ class ScheduledHyperParamSetter(HyperParamSetter):
...
@@ -250,6 +251,14 @@ class ScheduledHyperParamSetter(HyperParamSetter):
every time this callback is triggered.
every time this callback is triggered.
step_based (bool): interpret ``schedule`` as (step, value) instead
step_based (bool): interpret ``schedule`` as (step, value) instead
of (epoch, value).
of (epoch, value).
set_at_beginning (bool): at the start of training, the current value
may be different from the expected value according to the
schedule.
If this option is True, set the value anyway even though the current
epoch/step is not at the scheduled time.
If False, the value will only be set according to the
schedule, i.e. it will only be set if the current epoch/step
is at the scheduled time.
Example:
Example:
.. code-block:: python
.. code-block:: python
...
@@ -263,6 +272,7 @@ class ScheduledHyperParamSetter(HyperParamSetter):
...
@@ -263,6 +272,7 @@ class ScheduledHyperParamSetter(HyperParamSetter):
assert
interp
==
'linear'
assert
interp
==
'linear'
self
.
interp
=
interp
self
.
interp
=
interp
self
.
_step
=
step_based
self
.
_step
=
step_based
self
.
_set_at_beginning
=
set_at_beginning
super
(
ScheduledHyperParamSetter
,
self
)
.
__init__
(
param
)
super
(
ScheduledHyperParamSetter
,
self
)
.
__init__
(
param
)
def
_get_value_to_set
(
self
):
# override parent
def
_get_value_to_set
(
self
):
# override parent
...
@@ -277,12 +287,17 @@ class ScheduledHyperParamSetter(HyperParamSetter):
...
@@ -277,12 +287,17 @@ class ScheduledHyperParamSetter(HyperParamSetter):
for
p
in
range
(
0
,
self
.
_current_point
()
+
1
):
for
p
in
range
(
0
,
self
.
_current_point
()
+
1
):
v
=
self
.
_get_value_to_set_at_point
(
p
)
or
v
v
=
self
.
_get_value_to_set_at_point
(
p
)
or
v
actual_value
=
self
.
param
.
get_value
()
actual_value
=
self
.
param
.
get_value
()
current_point
=
"step"
if
self
.
_step
else
"epoch"
+
str
(
self
.
_current_point
())
if
v
is
not
None
and
not
np
.
isclose
(
v
,
actual_value
):
if
v
is
not
None
and
not
np
.
isclose
(
v
,
actual_value
):
logger
.
warn
(
"According to scheduler {}, parameter '{}' should become {} at the current point. "
logger
.
warn
(
"According to scheduler {}, parameter '{}' should become {:.7g} at the current point ({}). "
"However its current value is {}. "
"However its current value is {:.7g}. "
.
format
(
"If this is the only scheduler being used, you may want to check whether your "
self
,
self
.
param
.
readable_name
,
v
,
current_point
,
actual_value
))
"initialization of the parameter is as expected"
.
format
(
if
self
.
_set_at_beginning
:
self
,
self
.
param
.
readable_name
,
v
,
actual_value
))
logger
.
info
(
"Setting '{}' to {:.7g}."
.
format
(
self
.
param
.
readable_name
,
v
))
self
.
param
.
set_value
(
v
)
else
:
logger
.
warn
(
"If there is no other scheduler being used, you may want to check whether your "
"initialization of the parameter is as expected"
)
def
_get_value_to_set_at_point
(
self
,
point
):
def
_get_value_to_set_at_point
(
self
,
point
):
"""
"""
...
...
tensorpack/callbacks/param_test.py
View file @
11ca8b2c
...
@@ -69,10 +69,17 @@ class ScheduledHyperParamSetterTest(unittest.TestCase):
...
@@ -69,10 +69,17 @@ class ScheduledHyperParamSetterTest(unittest.TestCase):
def
testStartAfterSchedule
(
self
):
def
testStartAfterSchedule
(
self
):
scheduler
=
ScheduledHyperParamSetter
(
scheduler
=
ScheduledHyperParamSetter
(
ObjAttrParam
(
self
.
_param_obj
,
ParamObject
.
PARAM_NAME
),
ObjAttrParam
(
self
.
_param_obj
,
ParamObject
.
PARAM_NAME
),
[(
10
,
0.3
),
(
20
,
0.4
),
(
30
,
0.5
)])
[(
10
,
0.3
),
(
20
,
0.4
),
(
30
,
0.5
)]
,
set_at_beginning
=
False
)
history
=
self
.
_create_trainer_with_scheduler
(
scheduler
,
1
,
92
,
starting_epoch
=
90
)
history
=
self
.
_create_trainer_with_scheduler
(
scheduler
,
1
,
92
,
starting_epoch
=
90
)
self
.
assertEqual
(
len
(
history
),
0
)
self
.
assertEqual
(
len
(
history
),
0
)
def
testStartAfterSchedule_SetAtBeginning
(
self
):
scheduler
=
ScheduledHyperParamSetter
(
ObjAttrParam
(
self
.
_param_obj
,
ParamObject
.
PARAM_NAME
),
[(
10
,
0.3
),
(
20
,
0.4
),
(
30
,
0.5
)],
set_at_beginning
=
True
)
history
=
self
.
_create_trainer_with_scheduler
(
scheduler
,
1
,
92
,
starting_epoch
=
90
)
self
.
assertEqual
(
history
,
{
0
:
0.5
})
def
testWarningStartInTheMiddle
(
self
):
def
testWarningStartInTheMiddle
(
self
):
scheduler
=
ScheduledHyperParamSetter
(
scheduler
=
ScheduledHyperParamSetter
(
ObjAttrParam
(
self
.
_param_obj
,
ParamObject
.
PARAM_NAME
),
ObjAttrParam
(
self
.
_param_obj
,
ParamObject
.
PARAM_NAME
),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment