[go: nahoru, domu]

Skip to content

Commit

Permalink
Allow legacy optimizer in MG's optimizer factory.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 467067824
  • Loading branch information
chenmoneygithub authored and tensorflower-gardener committed Aug 11, 2022
1 parent 433a87e commit 03c41a1
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions official/modeling/optimization/optimizer_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,15 @@ def build_optimizer(
optimizer, **self._ema_config.as_dict())
if postprocessor:
optimizer = postprocessor(optimizer)
if not isinstance(optimizer, tf.keras.optimizers.Optimizer):
# tf.keras.optimizers.experimental only exist in tf-nightly.
# The following check makes sure the function wont' break in older TF
# version because of missing the experimental package.
if hasattr(tf.keras.optimizers, 'experimental'):
if not isinstance(optimizer,
tf.keras.optimizers.experimental.Optimizer):
raise TypeError('OptimizerFactory.build_optimizer returning a '
'non-optimizer object: {}'.format(optimizer))
else:
raise TypeError('OptimizerFactory.build_optimizer returning a '
'non-optimizer object: {}'.format(optimizer))

return optimizer
if isinstance(optimizer, tf.keras.optimizers.Optimizer):
return optimizer
# The following check makes sure the function won't break in older TF
# version because of missing the experimental/legacy package.
if hasattr(tf.keras.optimizers, 'experimental'):
if isinstance(optimizer, tf.keras.optimizers.experimental.Optimizer):
return optimizer
if hasattr(tf.keras.optimizers, 'legacy'):
if isinstance(optimizer, tf.keras.optimizers.legacy.Optimizer):
return optimizer
raise TypeError('OptimizerFactory.build_optimizer returning a '
'non-optimizer object: {}'.format(optimizer))

0 comments on commit 03c41a1

Please sign in to comment.