Skip to content

Commit

Permalink
TF error message fixit: keras/engine/training (part 4).
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 389096851
  • Loading branch information
rchao authored and tensorflower-gardener committed Aug 6, 2021
1 parent 673c95d commit 6e84e99
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions keras/engine/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -2717,23 +2717,25 @@ def _validate_compile(self, optimizer, metrics, **kwargs):
f'Received: {target_tensor_arg}.')
invalid_kwargs = set(kwargs) - {'sample_weight_mode'}
if invalid_kwargs:
raise TypeError('Invalid keyword argument(s) in `compile()`: %s' %
(invalid_kwargs,))
raise TypeError('Invalid keyword argument(s) in `compile()`: '
f'{(invalid_kwargs,)}. Valid keyword arguments include '
'"cloning", "experimental_run_tf_function", "distribute",'
' "target_tensors", or "sample_weight_mode".')

# Model must be created and compiled with the same DistStrat.
if self.built and tf.distribute.has_strategy():
strategy = tf.distribute.get_strategy()
for v in self.variables:
if not strategy.extended.variable_created_in_scope(v):
raise ValueError(
'Variable (%s) was not created in the distribution strategy '
'scope of (%s). It is most likely due to not all layers or '
'the model or optimizer being created outside the distribution '
'strategy scope. Try to make sure your code looks similar '
'to the following.\n'
f'Variable ({v}) was not created in the distribution strategy '
f'scope of ({strategy}). It is most likely because some '
'layers, model, or optimizer was being created outside the '
'distribution strategy scope. Try to make sure your code looks '
'similar to the following.\n'
'with strategy.scope():\n'
' model=_create_model()\n'
' model.compile(...)' % (v, strategy))
' model.compile(...)')

# Model metrics must be created in the same distribution strategy scope
# as the model.
Expand All @@ -2742,13 +2744,13 @@ def _validate_compile(self, optimizer, metrics, **kwargs):
for v in getattr(metric, 'variables', []):
if not strategy.extended.variable_created_in_scope(v):
raise ValueError(
'Metric (%s) passed to model.compile was created inside of a '
'different distribution strategy scope than the model. All '
f'Metric ({metric}) passed to `model.compile` was created inside '
'a different distribution strategy scope than the model. All '
'metrics must be created in the same distribution strategy '
'scope as the model (in this case %s). If you pass in a string '
'identifier for a metric to compile the metric will '
f'scope as the model (in this case {strategy}). If you pass in a '
'string identifier for a metric to compile, the metric will '
'automatically be created in the correct distribution '
'strategy scope.' % (metric, strategy)
'strategy scope.'
)

# Model metrics must be created in the same distribution strategy scope
Expand All @@ -2757,13 +2759,14 @@ def _validate_compile(self, optimizer, metrics, **kwargs):
for v in getattr(opt, '_weights', []):
if not strategy.extended.variable_created_in_scope(v):
raise ValueError(
'Optimizer (%s) passed to model.compile was created inside of a '
'different distribution strategy scope than the model. All '
'optimizers must be created in the same distribution strategy '
'scope as the model (in this case %s). If you pass in a string '
'identifier for an optimizer to compile the optimizer will '
'automatically be created in the correct distribution '
'strategy scope.' % (opt, strategy))
f'Optimizer ({optimizer}) passed to `model.compile` was created '
'inside a different distribution strategy scope than the model. '
'All optimizers must be created in the same distribution '
f'strategy scope as the model (in this case {strategy}). If you '
'pass in a string identifier for an optimizer to compile, the '
'optimizer will automatically be created in the correct '
'distribution strategy scope.'
)

def _maybe_load_initial_epoch_from_ckpt(self, initial_epoch):
"""Maybe load initial epoch from ckpt considering possible worker recovery.
Expand Down Expand Up @@ -2827,7 +2830,9 @@ def _should_eval(self, epoch, validation_freq):
elif isinstance(validation_freq, list):
return epoch in validation_freq
else:
raise ValueError('Expected `validation_freq` to be a list or int.')
raise ValueError('Expected `validation_freq` to be a list or int. '
f'Received: validation_freq={validation_freq} of the '
f'type {type(validation_freq)}.')

######################################################################
# Functions below exist only as v1 / v2 compatibility shims.
Expand Down Expand Up @@ -2903,7 +2908,8 @@ def _reduce(v):
else:
return concat(strategy.unwrap(v))
else:
raise ValueError('`reduction` must be "first" or "concat".')
raise ValueError('`reduction` must be "first" or "concat". Received: '
f'reduction={reduction}.')

return tf.nest.map_structure(_reduce, values)

Expand Down Expand Up @@ -3075,8 +3081,10 @@ def disable_multi_worker(method):

def _method_wrapper(self, *args, **kwargs):
if self._in_multi_worker_mode(): # pylint: disable=protected-access
raise ValueError('{} is not supported in multi-worker mode.'.format(
method.__name__))
raise ValueError(f'{method.__name__} is not supported in multi-worker '
'mode. Please use a non-multi-worker '
'`tf.distribute.Strategy` such as '
'`tf.distribute.MirroredStrategy`.')
return method(self, *args, **kwargs)

return tf.__internal__.decorator.make_decorator(
Expand Down

0 comments on commit 6e84e99

Please sign in to comment.