{"record":{"id":"364e3ccde99ddf26","repo":"tensorflow/models","slug":"user-should-set-optimizer-attribute-to-model-insid-364e3c","errorCode":null,"errorMessage":"User should set optimizer attribute to model inside `model_fn`.","messagePattern":"User should set optimizer attribute to model inside `model_fn`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/legacy/detection/executor/distributed_executor.py","lineNumber":421,"sourceCode":"      save_freq = FLAGS.save_checkpoint_freq\n    else:\n      save_freq = iterations_per_loop\n\n    params = self._params\n    strategy = self._strategy\n    # To reduce unnecessary send/receive input pipeline operation, we place\n    # input pipeline ops in worker task.\n    train_iterator = self._get_input_iterator(train_input_fn, strategy)\n    train_loss = None\n    train_metric_result = None\n    eval_metric_result = None\n    tf_keras.backend.set_learning_phase(1)\n    with strategy.scope():\n      # To correctly place the model weights on accelerators,\n      # model and optimizer should be created in scope.\n      model = self.model_fn(params.as_dict())\n      if not hasattr(model, 'optimizer'):\n        raise ValueError('User should set optimizer attribute to model '\n                         'inside `model_fn`.')\n      optimizer = model.optimizer\n\n      # Training loop starts here.\n      checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer)\n      latest_checkpoint_file = tf.train.latest_checkpoint(model_dir)\n      initial_step = 0\n      if latest_checkpoint_file:\n        logging.info(\n            'Checkpoint file %s found and restoring from '\n            'checkpoint', latest_checkpoint_file)\n        checkpoint.restore(latest_checkpoint_file)\n        initial_step = optimizer.iterations.numpy()\n        logging.info('Loading from checkpoint file completed. Init step %d',\n                     initial_step)\n      elif init_checkpoint:\n        logging.info('Restoring from init checkpoint function')\n        init_checkpoint(model)","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/legacy/detection/executor/distributed_executor.py#L403-L439","documentation":"Error \"User should set optimizer attribute to model inside `model_fn`.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/legacy/detection/executor/distributed_executor.py:421 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}