tensorflow/models · error · TypeError

AverageModelCheckpoint is only used when trainingwith…

Error message

AverageModelCheckpoint is only used when trainingwith MovingAverage

What it means

Configuration guard in AverageModelCheckpoint.set_model: the callback's update_weights feature requires the model's optimizer to be a MovingAverage optimizer, but the attached model uses an ordinary optimizer. The callback was attached to a training run without MovingAverage.

Solutions

  1. Enable moving_average_decay > 0 in the optimizer factory so the model uses MovingAverage
  2. Set update_weights=False and keep the averaged weights in the checkpoint instead
  3. Remove the AverageModelCheckpoint callback if not using MovingAverage
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/legacy/image_classification/callbacks.py:235 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/aae07f9899b672ba. Report an issue: GitHub.

Appendix: source

Thrown at official/legacy/image_classification/callbacks.py:235

  """

  def __init__(self,
               update_weights: bool,
               filepath: str,
               monitor: str = 'val_loss',
               verbose: int = 0,
               save_best_only: bool = False,
               save_weights_only: bool = False,
               mode: str = 'auto',
               save_freq: str = 'epoch',
               **kwargs):
    self.update_weights = update_weights
    super().__init__(filepath, monitor, verbose, save_best_only,
                     save_weights_only, mode, save_freq, **kwargs)

  def set_model(self, model):
    if not isinstance(model.optimizer, optimization.ExponentialMovingAverage):
      raise TypeError('AverageModelCheckpoint is only used when training'
                      'with MovingAverage')
    return super().set_model(model)

  def _save_model(self, epoch, logs):
    assert isinstance(self.model.optimizer,
                      optimization.ExponentialMovingAverage)

    if self.update_weights:
      self.model.optimizer.assign_average_vars(self.model.variables)
      return super()._save_model(epoch, logs)  # pytype: disable=attribute-error  # typed-keras
    else:
      # Note: `model.get_weights()` gives us the weights (non-ref)
      # whereas `model.variables` returns references to the variables.
      non_avg_weights = self.model.get_weights()
      self.model.optimizer.assign_average_vars(self.model.variables)
      # result is currently None, since `super._save_model` doesn't
      # return anything, but this may change in the future.
      result = super()._save_model(epoch, logs)  # pytype: disable=attribute-error  # typed-keras

View on GitHub (pinned to e006f5f0d5)