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
05ad529f
Commit
05ad529f
authored
Jul 09, 2020
by
Abdullah Rashwan
Committed by
A. Unique TensorFlower
Jul 09, 2020
Browse files
Internal change
PiperOrigin-RevId: 320495344
parent
4fd932ba
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
68 additions
and
28 deletions
+68
-28
official/modeling/optimization/configs/learning_rate_config.py
...ial/modeling/optimization/configs/learning_rate_config.py
+14
-0
official/modeling/optimization/configs/optimization_config.py
...cial/modeling/optimization/configs/optimization_config.py
+2
-0
official/modeling/optimization/configs/optimizer_config.py
official/modeling/optimization/configs/optimizer_config.py
+0
-10
official/modeling/optimization/optimizer_factory.py
official/modeling/optimization/optimizer_factory.py
+11
-11
official/modeling/optimization/optimizer_factory_test.py
official/modeling/optimization/optimizer_factory_test.py
+41
-7
No files found.
official/modeling/optimization/configs/learning_rate_config.py
View file @
05ad529f
...
@@ -20,6 +20,20 @@ import dataclasses
...
@@ -20,6 +20,20 @@ import dataclasses
from
official.modeling.hyperparams
import
base_config
from
official.modeling.hyperparams
import
base_config
@
dataclasses
.
dataclass
class
ConstantLrConfig
(
base_config
.
Config
):
"""Configuration for constant learning rate.
This class is a containers for the constant learning rate decay configs.
Attributes:
name: The name of the learning rate schedule. Defaults to Constant.
learning_rate: A float. The learning rate. Defaults to 0.1.
"""
name
:
str
=
'Constant'
learning_rate
:
float
=
0.1
@
dataclasses
.
dataclass
@
dataclasses
.
dataclass
class
StepwiseLrConfig
(
base_config
.
Config
):
class
StepwiseLrConfig
(
base_config
.
Config
):
"""Configuration for stepwise learning rate decay.
"""Configuration for stepwise learning rate decay.
...
...
official/modeling/optimization/configs/optimization_config.py
View file @
05ad529f
...
@@ -55,12 +55,14 @@ class LrConfig(oneof.OneOfConfig):
...
@@ -55,12 +55,14 @@ class LrConfig(oneof.OneOfConfig):
Attributes:
Attributes:
type: 'str', type of lr schedule to be used, on the of fields below.
type: 'str', type of lr schedule to be used, on the of fields below.
constant: constant learning rate config.
stepwise: stepwise learning rate config.
stepwise: stepwise learning rate config.
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.
"""
"""
type
:
Optional
[
str
]
=
None
type
:
Optional
[
str
]
=
None
constant
:
lr_cfg
.
ConstantLrConfig
=
lr_cfg
.
ConstantLrConfig
()
stepwise
:
lr_cfg
.
StepwiseLrConfig
=
lr_cfg
.
StepwiseLrConfig
()
stepwise
:
lr_cfg
.
StepwiseLrConfig
=
lr_cfg
.
StepwiseLrConfig
()
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
()
...
...
official/modeling/optimization/configs/optimizer_config.py
View file @
05ad529f
...
@@ -28,13 +28,11 @@ class SGDConfig(base_config.Config):
...
@@ -28,13 +28,11 @@ class SGDConfig(base_config.Config):
Attributes:
Attributes:
name: name of the optimizer.
name: name of the optimizer.
learning_rate: learning_rate for SGD optimizer.
decay: decay rate for SGD optimizer.
decay: decay rate for SGD optimizer.
nesterov: nesterov for SGD optimizer.
nesterov: nesterov for SGD optimizer.
momentum: momentum for SGD optimizer.
momentum: momentum for SGD optimizer.
"""
"""
name
:
str
=
"SGD"
name
:
str
=
"SGD"
learning_rate
:
float
=
0.01
decay
:
float
=
0.0
decay
:
float
=
0.0
nesterov
:
bool
=
False
nesterov
:
bool
=
False
momentum
:
float
=
0.0
momentum
:
float
=
0.0
...
@@ -49,14 +47,12 @@ class RMSPropConfig(base_config.Config):
...
@@ -49,14 +47,12 @@ class RMSPropConfig(base_config.Config):
Attributes:
Attributes:
name: name of the optimizer.
name: name of the optimizer.
learning_rate: learning_rate for RMSprop optimizer.
rho: discounting factor for RMSprop optimizer.
rho: discounting factor for RMSprop optimizer.
momentum: momentum for RMSprop optimizer.
momentum: momentum for RMSprop optimizer.
epsilon: epsilon value for RMSprop optimizer, help with numerical stability.
epsilon: epsilon value for RMSprop optimizer, help with numerical stability.
centered: Whether to normalize gradients or not.
centered: Whether to normalize gradients or not.
"""
"""
name
:
str
=
"RMSprop"
name
:
str
=
"RMSprop"
learning_rate
:
float
=
0.001
rho
:
float
=
0.9
rho
:
float
=
0.9
momentum
:
float
=
0.0
momentum
:
float
=
0.0
epsilon
:
float
=
1e-7
epsilon
:
float
=
1e-7
...
@@ -72,7 +68,6 @@ class AdamConfig(base_config.Config):
...
@@ -72,7 +68,6 @@ class AdamConfig(base_config.Config):
Attributes:
Attributes:
name: name of the optimizer.
name: name of the optimizer.
learning_rate: learning_rate for Adam optimizer.
beta_1: decay rate for 1st order moments.
beta_1: decay rate for 1st order moments.
beta_2: decay rate for 2st order moments.
beta_2: decay rate for 2st order moments.
epsilon: epsilon value used for numerical stability in Adam optimizer.
epsilon: epsilon value used for numerical stability in Adam optimizer.
...
@@ -80,7 +75,6 @@ class AdamConfig(base_config.Config):
...
@@ -80,7 +75,6 @@ class AdamConfig(base_config.Config):
the paper "On the Convergence of Adam and beyond".
the paper "On the Convergence of Adam and beyond".
"""
"""
name
:
str
=
"Adam"
name
:
str
=
"Adam"
learning_rate
:
float
=
0.001
beta_1
:
float
=
0.9
beta_1
:
float
=
0.9
beta_2
:
float
=
0.999
beta_2
:
float
=
0.999
epsilon
:
float
=
1e-07
epsilon
:
float
=
1e-07
...
@@ -93,7 +87,6 @@ class AdamWeightDecayConfig(base_config.Config):
...
@@ -93,7 +87,6 @@ class AdamWeightDecayConfig(base_config.Config):
Attributes:
Attributes:
name: name of the optimizer.
name: name of the optimizer.
learning_rate: learning_rate for the optimizer.
beta_1: decay rate for 1st order moments.
beta_1: decay rate for 1st order moments.
beta_2: decay rate for 2st order moments.
beta_2: decay rate for 2st order moments.
epsilon: epsilon value used for numerical stability in the optimizer.
epsilon: epsilon value used for numerical stability in the optimizer.
...
@@ -106,7 +99,6 @@ class AdamWeightDecayConfig(base_config.Config):
...
@@ -106,7 +99,6 @@ class AdamWeightDecayConfig(base_config.Config):
include in weight decay.
include in weight decay.
"""
"""
name
:
str
=
"AdamWeightDecay"
name
:
str
=
"AdamWeightDecay"
learning_rate
:
float
=
0.001
beta_1
:
float
=
0.9
beta_1
:
float
=
0.9
beta_2
:
float
=
0.999
beta_2
:
float
=
0.999
epsilon
:
float
=
1e-07
epsilon
:
float
=
1e-07
...
@@ -125,7 +117,6 @@ class LAMBConfig(base_config.Config):
...
@@ -125,7 +117,6 @@ class LAMBConfig(base_config.Config):
Attributes:
Attributes:
name: name of the optimizer.
name: name of the optimizer.
learning_rate: learning_rate for Adam optimizer.
beta_1: decay rate for 1st order moments.
beta_1: decay rate for 1st order moments.
beta_2: decay rate for 2st order moments.
beta_2: decay rate for 2st order moments.
epsilon: epsilon value used for numerical stability in LAMB optimizer.
epsilon: epsilon value used for numerical stability in LAMB optimizer.
...
@@ -139,7 +130,6 @@ class LAMBConfig(base_config.Config):
...
@@ -139,7 +130,6 @@ class LAMBConfig(base_config.Config):
be excluded.
be excluded.
"""
"""
name
:
str
=
"LAMB"
name
:
str
=
"LAMB"
learning_rate
:
float
=
0.001
beta_1
:
float
=
0.9
beta_1
:
float
=
0.9
beta_2
:
float
=
0.999
beta_2
:
float
=
0.999
epsilon
:
float
=
1e-6
epsilon
:
float
=
1e-6
...
...
official/modeling/optimization/optimizer_factory.py
View file @
05ad529f
...
@@ -60,7 +60,7 @@ class OptimizerFactory(object):
...
@@ -60,7 +60,7 @@ class OptimizerFactory(object):
params = {
params = {
'optimizer': {
'optimizer': {
'type': 'sgd',
'type': 'sgd',
'sgd': {
'learning_rate': 0.1,
'momentum': 0.9}
'sgd': {'momentum': 0.9}
},
},
'learning_rate': {
'learning_rate': {
'type': 'stepwise',
'type': 'stepwise',
...
@@ -88,12 +88,15 @@ class OptimizerFactory(object):
...
@@ -88,12 +88,15 @@ class OptimizerFactory(object):
self
.
_optimizer_config
=
config
.
optimizer
.
get
()
self
.
_optimizer_config
=
config
.
optimizer
.
get
()
self
.
_optimizer_type
=
config
.
optimizer
.
type
self
.
_optimizer_type
=
config
.
optimizer
.
type
if
self
.
_optimizer_
config
is
None
:
if
self
.
_optimizer_
type
is
None
:
raise
ValueError
(
'Optimizer type must be specified'
)
raise
ValueError
(
'Optimizer type must be specified'
)
self
.
_lr_config
=
config
.
learning_rate
.
get
()
self
.
_lr_config
=
config
.
learning_rate
.
get
()
self
.
_lr_type
=
config
.
learning_rate
.
type
self
.
_lr_type
=
config
.
learning_rate
.
type
if
self
.
_lr_type
is
None
:
raise
ValueError
(
'Learning rate type must be specified'
)
self
.
_warmup_config
=
config
.
warmup
.
get
()
self
.
_warmup_config
=
config
.
warmup
.
get
()
self
.
_warmup_type
=
config
.
warmup
.
type
self
.
_warmup_type
=
config
.
warmup
.
type
...
@@ -101,18 +104,15 @@ class OptimizerFactory(object):
...
@@ -101,18 +104,15 @@ class OptimizerFactory(object):
"""Build learning rate.
"""Build learning rate.
Builds learning rate from config. Learning rate schedule is built according
Builds learning rate from config. Learning rate schedule is built according
to the learning rate config. If
there is no
learning rate
config, optimizer
to the learning rate config. If learning rate
type is consant,
learning
rate is returned.
lr_config.
learning
_
rate is returned.
Returns:
Returns:
tf.keras.optimizers.schedules.LearningRateSchedule instance. If no
tf.keras.optimizers.schedules.LearningRateSchedule instance. If
learning rate schedule defined, optimizer_config.learning_rate is
learning rate type is consant, lr_config.learning_rate is returned.
returned.
"""
"""
if
self
.
_lr_type
==
'constant'
:
# TODO(arashwan): Explore if we want to only allow explicit const lr sched.
lr
=
self
.
_lr_config
.
learning_rate
if
not
self
.
_lr_config
:
lr
=
self
.
_optimizer_config
.
learning_rate
else
:
else
:
lr
=
LR_CLS
[
self
.
_lr_type
](
**
self
.
_lr_config
.
as_dict
())
lr
=
LR_CLS
[
self
.
_lr_type
](
**
self
.
_lr_config
.
as_dict
())
...
...
official/modeling/optimization/optimizer_factory_test.py
View file @
05ad529f
...
@@ -35,10 +35,17 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -35,10 +35,17 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
params
=
{
params
=
{
'optimizer'
:
{
'optimizer'
:
{
'type'
:
optimizer_type
'type'
:
optimizer_type
},
'learning_rate'
:
{
'type'
:
'constant'
,
'constant'
:
{
'learning_rate'
:
0.1
}
}
}
}
}
optimizer_cls
=
optimizer_factory
.
OPTIMIZERS_CLS
[
optimizer_type
]
optimizer_cls
=
optimizer_factory
.
OPTIMIZERS_CLS
[
optimizer_type
]
expected_optimizer_config
=
optimizer_cls
().
get_config
()
expected_optimizer_config
=
optimizer_cls
().
get_config
()
expected_optimizer_config
[
'learning_rate'
]
=
0.1
opt_config
=
optimization_config
.
OptimizationConfig
(
params
)
opt_config
=
optimization_config
.
OptimizationConfig
(
params
)
opt_factory
=
optimizer_factory
.
OptimizerFactory
(
opt_config
)
opt_factory
=
optimizer_factory
.
OptimizerFactory
(
opt_config
)
...
@@ -48,11 +55,32 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -48,11 +55,32 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
self
.
assertIsInstance
(
optimizer
,
optimizer_cls
)
self
.
assertIsInstance
(
optimizer
,
optimizer_cls
)
self
.
assertEqual
(
expected_optimizer_config
,
optimizer
.
get_config
())
self
.
assertEqual
(
expected_optimizer_config
,
optimizer
.
get_config
())
def
test_missing_types
(
self
):
params
=
{
'optimizer'
:
{
'type'
:
'sgd'
,
'sgd'
:
{
'momentum'
:
0.9
}
}
}
with
self
.
assertRaises
(
ValueError
):
optimizer_factory
.
OptimizerFactory
(
optimization_config
.
OptimizationConfig
(
params
))
params
=
{
'learning_rate'
:
{
'type'
:
'stepwise'
,
'stepwise'
:
{
'boundaries'
:
[
10000
,
20000
],
'values'
:
[
0.1
,
0.01
,
0.001
]}
}
}
with
self
.
assertRaises
(
ValueError
):
optimizer_factory
.
OptimizerFactory
(
optimization_config
.
OptimizationConfig
(
params
))
def
test_stepwise_lr_schedule
(
self
):
def
test_stepwise_lr_schedule
(
self
):
params
=
{
params
=
{
'optimizer'
:
{
'optimizer'
:
{
'type'
:
'sgd'
,
'type'
:
'sgd'
,
'sgd'
:
{
'learning_rate'
:
0.1
,
'momentum'
:
0.9
}
'sgd'
:
{
'momentum'
:
0.9
}
},
},
'learning_rate'
:
{
'learning_rate'
:
{
'type'
:
'stepwise'
,
'type'
:
'stepwise'
,
...
@@ -79,7 +107,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -79,7 +107,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
params
=
{
params
=
{
'optimizer'
:
{
'optimizer'
:
{
'type'
:
'sgd'
,
'type'
:
'sgd'
,
'sgd'
:
{
'learning_rate'
:
0.1
,
'momentum'
:
0.9
}
'sgd'
:
{
'momentum'
:
0.9
}
},
},
'learning_rate'
:
{
'learning_rate'
:
{
'type'
:
'stepwise'
,
'type'
:
'stepwise'
,
...
@@ -112,7 +140,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -112,7 +140,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
params
=
{
params
=
{
'optimizer'
:
{
'optimizer'
:
{
'type'
:
'sgd'
,
'type'
:
'sgd'
,
'sgd'
:
{
'learning_rate'
:
0.1
,
'momentum'
:
0.9
}
'sgd'
:
{
'momentum'
:
0.9
}
},
},
'learning_rate'
:
{
'learning_rate'
:
{
'type'
:
'exponential'
,
'type'
:
'exponential'
,
...
@@ -142,7 +170,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -142,7 +170,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
params
=
{
params
=
{
'optimizer'
:
{
'optimizer'
:
{
'type'
:
'sgd'
,
'type'
:
'sgd'
,
'sgd'
:
{
'learning_rate'
:
0.1
,
'momentum'
:
0.9
}
'sgd'
:
{
'momentum'
:
0.9
}
},
},
'learning_rate'
:
{
'learning_rate'
:
{
'type'
:
'polynomial'
,
'type'
:
'polynomial'
,
...
@@ -166,7 +194,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -166,7 +194,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
params
=
{
params
=
{
'optimizer'
:
{
'optimizer'
:
{
'type'
:
'sgd'
,
'type'
:
'sgd'
,
'sgd'
:
{
'learning_rate'
:
0.1
,
'momentum'
:
0.9
}
'sgd'
:
{
'momentum'
:
0.9
}
},
},
'learning_rate'
:
{
'learning_rate'
:
{
'type'
:
'cosine'
,
'type'
:
'cosine'
,
...
@@ -192,7 +220,13 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -192,7 +220,13 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
params
=
{
params
=
{
'optimizer'
:
{
'optimizer'
:
{
'type'
:
'sgd'
,
'type'
:
'sgd'
,
'sgd'
:
{
'learning_rate'
:
0.1
,
'momentum'
:
0.9
}
'sgd'
:
{
'momentum'
:
0.9
}
},
'learning_rate'
:
{
'type'
:
'constant'
,
'constant'
:
{
'learning_rate'
:
0.1
}
},
},
'warmup'
:
{
'warmup'
:
{
'type'
:
'linear'
,
'type'
:
'linear'
,
...
@@ -216,7 +250,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -216,7 +250,7 @@ class OptimizerFactoryTest(tf.test.TestCase, parameterized.TestCase):
params
=
{
params
=
{
'optimizer'
:
{
'optimizer'
:
{
'type'
:
'sgd'
,
'type'
:
'sgd'
,
'sgd'
:
{
'learning_rate'
:
0.1
,
'momentum'
:
0.9
}
'sgd'
:
{
'momentum'
:
0.9
}
},
},
'learning_rate'
:
{
'learning_rate'
:
{
'type'
:
'stepwise'
,
'type'
:
'stepwise'
,
...
...
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