Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
1ebff962
Commit
1ebff962
authored
Aug 26, 2020
by
Hongkun Yu
Committed by
A. Unique TensorFlower
Aug 26, 2020
Browse files
Add new lr schedule: lr * (step)^power
PiperOrigin-RevId: 328677732
parent
ed38358f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
79 additions
and
1 deletion
+79
-1
official/modeling/optimization/configs/learning_rate_config.py
...ial/modeling/optimization/configs/learning_rate_config.py
+16
-0
official/modeling/optimization/configs/optimization_config.py
...cial/modeling/optimization/configs/optimization_config.py
+2
-0
official/modeling/optimization/lr_schedule.py
official/modeling/optimization/lr_schedule.py
+35
-0
official/modeling/optimization/optimizer_factory.py
official/modeling/optimization/optimizer_factory.py
+2
-1
official/modeling/optimization/optimizer_factory_test.py
official/modeling/optimization/optimizer_factory_test.py
+24
-0
No files found.
official/modeling/optimization/configs/learning_rate_config.py
View file @
1ebff962
...
@@ -130,6 +130,22 @@ class CosineLrConfig(base_config.Config):
...
@@ -130,6 +130,22 @@ class CosineLrConfig(base_config.Config):
alpha
:
float
=
0.0
alpha
:
float
=
0.0
@
dataclasses
.
dataclass
class
DirectPowerLrConfig
(
base_config
.
Config
):
"""Configuration for DirectPower learning rate decay.
This class configures a schedule following follows lr * (step)^power.
Attributes:
name: The name of the learning rate schedule. Defaults to DirectPowerDecay.
initial_learning_rate: A float. The initial learning rate. Defaults to None.
power: A float. Defaults to -0.5, for sqrt decay.
"""
name
:
str
=
'DirectPowerDecay'
initial_learning_rate
:
Optional
[
float
]
=
None
power
:
float
=
-
0.5
@
dataclasses
.
dataclass
@
dataclasses
.
dataclass
class
LinearWarmupConfig
(
base_config
.
Config
):
class
LinearWarmupConfig
(
base_config
.
Config
):
"""Configuration for linear warmup schedule config.
"""Configuration for linear warmup schedule config.
...
...
official/modeling/optimization/configs/optimization_config.py
View file @
1ebff962
...
@@ -60,6 +60,7 @@ class LrConfig(oneof.OneOfConfig):
...
@@ -60,6 +60,7 @@ class LrConfig(oneof.OneOfConfig):
exponential: exponential learning rate config.
exponential: exponential learning rate config.
polynomial: polynomial learning rate config.
polynomial: polynomial learning rate config.
cosine: cosine learning rate config.
cosine: cosine learning rate config.
power: step^power learning rate config.
"""
"""
type
:
Optional
[
str
]
=
None
type
:
Optional
[
str
]
=
None
constant
:
lr_cfg
.
ConstantLrConfig
=
lr_cfg
.
ConstantLrConfig
()
constant
:
lr_cfg
.
ConstantLrConfig
=
lr_cfg
.
ConstantLrConfig
()
...
@@ -67,6 +68,7 @@ class LrConfig(oneof.OneOfConfig):
...
@@ -67,6 +68,7 @@ class LrConfig(oneof.OneOfConfig):
exponential
:
lr_cfg
.
ExponentialLrConfig
=
lr_cfg
.
ExponentialLrConfig
()
exponential
:
lr_cfg
.
ExponentialLrConfig
=
lr_cfg
.
ExponentialLrConfig
()
polynomial
:
lr_cfg
.
PolynomialLrConfig
=
lr_cfg
.
PolynomialLrConfig
()
polynomial
:
lr_cfg
.
PolynomialLrConfig
=
lr_cfg
.
PolynomialLrConfig
()
cosine
:
lr_cfg
.
CosineLrConfig
=
lr_cfg
.
CosineLrConfig
()
cosine
:
lr_cfg
.
CosineLrConfig
=
lr_cfg
.
CosineLrConfig
()
power
:
lr_cfg
.
DirectPowerLrConfig
=
lr_cfg
.
DirectPowerLrConfig
()
@
dataclasses
.
dataclass
@
dataclasses
.
dataclass
...
...
official/modeling/optimization/lr_schedule.py
View file @
1ebff962
...
@@ -153,3 +153,38 @@ class PolynomialWarmUp(tf.keras.optimizers.schedules.LearningRateSchedule):
...
@@ -153,3 +153,38 @@ class PolynomialWarmUp(tf.keras.optimizers.schedules.LearningRateSchedule):
"name"
:
self
.
_name
"name"
:
self
.
_name
})
})
return
config
return
config
class
DirectPowerDecay
(
tf
.
keras
.
optimizers
.
schedules
.
LearningRateSchedule
):
"""Learning rate schedule follows lr * (step)^power."""
def
__init__
(
self
,
initial_learning_rate
:
float
,
power
:
float
=
1.0
,
name
:
str
=
"DirectPowerDecay"
):
"""Initialize configuration of the learning rate schedule.
Args:
initial_learning_rate: A float, the initial learning rate.
power: A float, the number of steps required for linear warmup.
name: Optional, name of warmup schedule.
"""
super
(
DirectPowerDecay
,
self
).
__init__
()
self
.
_initial_learning_rate
=
initial_learning_rate
self
.
_power
=
power
self
.
_name
=
name
def
__call__
(
self
,
step
):
with
tf
.
name_scope
(
self
.
_name
or
"DirectPowerDecay"
):
step
=
tf
.
cast
(
step
,
tf
.
float32
)
learning_rate
=
self
.
_initial_learning_rate
learning_rate
*=
tf
.
math
.
pow
(
step
,
self
.
_power
)
return
learning_rate
def
get_config
(
self
):
"""Get the configuration of the learning rate schedule."""
return
{
"initial_learning_rate"
:
self
.
_initial_learning_rate
,
"power"
:
self
.
_power
,
"name"
:
self
.
_name
,
}
official/modeling/optimization/optimizer_factory.py
View file @
1ebff962
...
@@ -36,7 +36,8 @@ LR_CLS = {
...
@@ -36,7 +36,8 @@ LR_CLS = {
'stepwise'
:
tf
.
keras
.
optimizers
.
schedules
.
PiecewiseConstantDecay
,
'stepwise'
:
tf
.
keras
.
optimizers
.
schedules
.
PiecewiseConstantDecay
,
'polynomial'
:
tf
.
keras
.
optimizers
.
schedules
.
PolynomialDecay
,
'polynomial'
:
tf
.
keras
.
optimizers
.
schedules
.
PolynomialDecay
,
'exponential'
:
tf
.
keras
.
optimizers
.
schedules
.
ExponentialDecay
,
'exponential'
:
tf
.
keras
.
optimizers
.
schedules
.
ExponentialDecay
,
'cosine'
:
tf
.
keras
.
experimental
.
CosineDecay
'cosine'
:
tf
.
keras
.
experimental
.
CosineDecay
,
'power'
:
lr_schedule
.
DirectPowerDecay
,
}
}
WARMUP_CLS
=
{
WARMUP_CLS
=
{
...
...
official/modeling/optimization/optimizer_factory_test.py
View file @
1ebff962
...
@@ -274,6 +274,30 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -274,6 +274,30 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
for
step
,
value
in
expected_lr_step_values
:
for
step
,
value
in
expected_lr_step_values
:
self
.
assertAlmostEqual
(
lr
(
step
).
numpy
(),
value
)
self
.
assertAlmostEqual
(
lr
(
step
).
numpy
(),
value
)
def
test_power_lr_schedule
(
self
):
params
=
{
'optimizer'
:
{
'type'
:
'sgd'
,
'sgd'
:
{
'momentum'
:
0.9
}
},
'learning_rate'
:
{
'type'
:
'power'
,
'power'
:
{
'initial_learning_rate'
:
1.0
,
'power'
:
-
1.0
}
}
}
expected_lr_step_values
=
[[
1
,
1.0
],
[
250
,
1.
/
250.
]]
opt_config
=
optimization_config
.
OptimizationConfig
(
params
)
opt_factory
=
optimizer_factory
.
OptimizerFactory
(
opt_config
)
lr
=
opt_factory
.
build_learning_rate
()
for
step
,
value
in
expected_lr_step_values
:
self
.
assertAlmostEqual
(
lr
(
step
).
numpy
(),
value
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
tf
.
test
.
main
()
tf
.
test
.
main
()
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