tensorflow/models · error · ValueError

User should set optimizer attribute to model inside `model_f

Error message

User should set optimizer attribute to model inside `model_fn`.

What it means

Error "User should set optimizer attribute to model inside `model_fn`." thrown in tensorflow/models.

Source

Thrown at official/legacy/bert/model_training_utils.py:271

          ' optimization for the distributed system.')

  if eval_input_fn and eval_steps is None:
    raise ValueError(
        '`eval_step` is required when `eval_input_fn ` is not none.')
  if metric_fn and not callable(metric_fn):
    raise ValueError(
        'if `metric_fn` is specified, metric_fn must be a callable.')

  total_training_steps = steps_per_epoch * epochs  # pyrefly: ignore[unsupported-operation]
  train_iterator = _get_input_iterator(train_input_fn, strategy)
  eval_loss_metric = tf_keras.metrics.Mean('training_loss', dtype=tf.float32)

  with distribute_utils.get_strategy_scope(strategy):
    # To correctly place the model weights on accelerators,
    # model and optimizer should be created in scope.
    model, sub_model = model_fn()  # pyrefly: ignore[not-callable]
    if not hasattr(model, 'optimizer'):
      raise ValueError('User should set optimizer attribute to model '
                       'inside `model_fn`.')
    if sub_model_export_name and sub_model is None:
      raise ValueError('sub_model_export_name is specified as %s, but '
                       'sub_model is None.' % sub_model_export_name)

    callback_list = tf_keras.callbacks.CallbackList(
        callbacks=custom_callbacks, model=model)

    optimizer = model.optimizer

    if init_checkpoint:
      logging.info(
          'Checkpoint file %s found and restoring from '
          'initial checkpoint for core model.', init_checkpoint)
      checkpoint = tf.train.Checkpoint(model=sub_model, encoder=sub_model)
      checkpoint.read(init_checkpoint).assert_existing_objects_matched()
      logging.info('Loading from checkpoint file completed')

View on GitHub (pinned to e006f5f0d5)

When it happens

Trigger: Thrown at official/legacy/bert/model_training_utils.py:271 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/bf591dd24c6145c2. Report an issue: GitHub.