tensorflow/models · error · ValueError
User should set optimizer attribute to model inside…
Error message
User should set optimizer attribute to model inside `model_fn`.
What it means
Guard inside DistributedExecutor.train after model_fn runs in the strategy scope: the produced model has no optimizer attribute, so no gradients can be applied. The model_fn contract requires attaching the optimizer to the returned model (created inside the strategy scope) before training.
Solutions
- Inside model_fn, set model.optimizer = tf_keras.optimizers.SGD(learning_rate=..., ...) within strategy.scope()
- Ensure model_fn returns the model object with the optimizer attached, not a bare network
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at official/legacy/detection/executor/distributed_executor.py:421 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24).
Data as JSON: /api/errors/364e3ccde99ddf26.
Report an issue: GitHub.
Appendix: source
Thrown at official/legacy/detection/executor/distributed_executor.py:421
save_freq = FLAGS.save_checkpoint_freq
else:
save_freq = iterations_per_loop
params = self._params
strategy = self._strategy
# To reduce unnecessary send/receive input pipeline operation, we place
# input pipeline ops in worker task.
train_iterator = self._get_input_iterator(train_input_fn, strategy)
train_loss = None
train_metric_result = None
eval_metric_result = None
tf_keras.backend.set_learning_phase(1)
with strategy.scope():
# To correctly place the model weights on accelerators,
# model and optimizer should be created in scope.
model = self.model_fn(params.as_dict())
if not hasattr(model, 'optimizer'):
raise ValueError('User should set optimizer attribute to model '
'inside `model_fn`.')
optimizer = model.optimizer
# Training loop starts here.
checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer)
latest_checkpoint_file = tf.train.latest_checkpoint(model_dir)
initial_step = 0
if latest_checkpoint_file:
logging.info(
'Checkpoint file %s found and restoring from '
'checkpoint', latest_checkpoint_file)
checkpoint.restore(latest_checkpoint_file)
initial_step = optimizer.iterations.numpy()
logging.info('Loading from checkpoint file completed. Init step %d',
initial_step)
elif init_checkpoint:
logging.info('Restoring from init checkpoint function')
init_checkpoint(model)View on GitHub (pinned to e006f5f0d5)