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
2ce7ec1c
You need to sign in or sign up before continuing.
Commit
2ce7ec1c
authored
Feb 10, 2018
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ScheduleHyperParamSetter based on steps (#633)
parent
0ffdcc44
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
10 deletions
+26
-10
tensorpack/callbacks/param.py
tensorpack/callbacks/param.py
+26
-10
No files found.
tensorpack/callbacks/param.py
View file @
2ce7ec1c
...
@@ -125,7 +125,8 @@ class HyperParamSetter(Callback):
...
@@ -125,7 +125,8 @@ class HyperParamSetter(Callback):
param
=
GraphVarParam
(
param
)
param
=
GraphVarParam
(
param
)
assert
isinstance
(
param
,
HyperParam
),
type
(
param
)
assert
isinstance
(
param
,
HyperParam
),
type
(
param
)
self
.
param
=
param
self
.
param
=
param
self
.
last_value
=
None
self
.
_last_value
=
None
self
.
_last_epoch_set
=
-
1
def
_setup_graph
(
self
):
def
_setup_graph
(
self
):
self
.
param
.
setup_graph
()
self
.
param
.
setup_graph
()
...
@@ -141,10 +142,13 @@ class HyperParamSetter(Callback):
...
@@ -141,10 +142,13 @@ class HyperParamSetter(Callback):
set, or return None to do nothing.
set, or return None to do nothing.
"""
"""
ret
=
self
.
_get_value_to_set
()
ret
=
self
.
_get_value_to_set
()
if
ret
is
not
None
and
ret
!=
self
.
last_value
:
if
ret
is
not
None
and
ret
!=
self
.
_last_value
:
logger
.
info
(
"After epoch {}, {} will change to {:.8f}"
.
format
(
if
self
.
epoch_num
!=
self
.
_last_epoch_set
:
self
.
epoch_num
,
self
.
param
.
readable_name
,
ret
))
# Print this message at most once every epoch
self
.
last_value
=
ret
logger
.
info
(
"[HyperParamSetter] At global_step={}, {} will change to {:.8f}"
.
format
(
self
.
global_step
,
self
.
param
.
readable_name
,
ret
))
self
.
_last_epoch_set
=
self
.
epoch_num
self
.
_last_value
=
ret
return
ret
return
ret
@
abstractmethod
@
abstractmethod
...
@@ -213,7 +217,7 @@ class ScheduledHyperParamSetter(HyperParamSetter):
...
@@ -213,7 +217,7 @@ 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
):
def
__init__
(
self
,
param
,
schedule
,
interp
=
None
,
step_based
=
False
):
"""
"""
Args:
Args:
param: same as in :class:`HyperParamSetter`.
param: same as in :class:`HyperParamSetter`.
...
@@ -223,6 +227,8 @@ class ScheduledHyperParamSetter(HyperParamSetter):
...
@@ -223,6 +227,8 @@ class ScheduledHyperParamSetter(HyperParamSetter):
If ep == 0, the value will be set before the first epoch
If ep == 0, the value will be set before the first epoch
(because by default the first is epoch 1).
(because by default the first is epoch 1).
interp: None: no interpolation. 'linear': linear interpolation
interp: None: no interpolation. 'linear': linear interpolation
step_based (bool): interpret ``schedule`` as (step, value) instead
of (epoch, value).
Example:
Example:
.. code-block:: python
.. code-block:: python
...
@@ -235,28 +241,38 @@ class ScheduledHyperParamSetter(HyperParamSetter):
...
@@ -235,28 +241,38 @@ class ScheduledHyperParamSetter(HyperParamSetter):
if
interp
is
not
None
:
if
interp
is
not
None
:
assert
interp
==
'linear'
assert
interp
==
'linear'
self
.
interp
=
interp
self
.
interp
=
interp
self
.
_step
=
step_based
super
(
ScheduledHyperParamSetter
,
self
)
.
__init__
(
param
)
super
(
ScheduledHyperParamSetter
,
self
)
.
__init__
(
param
)
def
_get_value_to_set
(
self
):
def
_get_value_to_set
(
self
):
refnum
=
self
.
global_step
if
self
.
_step
else
self
.
epoch_num
if
self
.
interp
is
None
:
if
self
.
interp
is
None
:
for
e
,
v
in
self
.
schedule
:
for
e
,
v
in
self
.
schedule
:
if
e
==
self
.
epoch_
num
:
if
e
==
ref
num
:
return
v
return
v
return
None
return
None
else
:
else
:
laste
,
lastv
=
None
,
None
laste
,
lastv
=
None
,
None
for
e
,
v
in
self
.
schedule
:
for
e
,
v
in
self
.
schedule
:
if
e
==
self
.
epoch_
num
:
if
e
==
ref
num
:
return
v
return
v
if
e
>
self
.
epoch_
num
:
if
e
>
ref
num
:
break
break
laste
,
lastv
=
e
,
v
laste
,
lastv
=
e
,
v
if
laste
is
None
or
laste
==
e
:
if
laste
is
None
or
laste
==
e
:
# hasn't reached the first scheduled point, or reached the end of all scheduled points
# hasn't reached the first scheduled point, or reached the end of all scheduled points
return
None
return
None
v
=
(
self
.
epoch_
num
-
laste
)
*
1.
/
(
e
-
laste
)
*
(
v
-
lastv
)
+
lastv
v
=
(
ref
num
-
laste
)
*
1.
/
(
e
-
laste
)
*
(
v
-
lastv
)
+
lastv
return
v
return
v
def
_trigger_epoch
(
self
):
if
not
self
.
_step
:
self
.
trigger
()
def
_trigger_step
(
self
):
if
self
.
_step
:
self
.
trigger
()
class
HyperParamSetterWithFunc
(
HyperParamSetter
):
class
HyperParamSetterWithFunc
(
HyperParamSetter
):
""" Set the parameter by a function of epoch num and old value. """
""" Set the parameter by a function of epoch num and old value. """
...
...
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