class_docstr: An abstract base class that represents a TensorFlow classifier.
docstr: Trains the classifier model.
  
  Compiles and fits the tf.keras `_model` and records the `_history`.
  
  Args:
    train_data: Training data.
    validation_data: Validation data.
    preprocessor: An optional data preprocessor that can be used when
      generating a tf.data.Dataset.
    checkpoint_path: An optional directory for the checkpoint file to support
      continual training. If provided, loads model weights from the latest
      checkpoint in the directory.
docstr_tok: ["Trains","the","classifier","model",".","Compiles","and","fits","the","tf.keras","`","_model","`","and","records","the","`","_history","`",".","Args",":","train_data",":","Training","data",".","validation_data",":","Validation","data",".","preprocessor",":","An","optional","data","preprocessor","that","can","be","used","when","generating","a","tf.data.Dataset",".","checkpoint_path",":","An","optional","directory","for","the","checkpoint","file","to","support","continual","training",".","If","provided",",","loads","model","weights","from","the","latest","checkpoint","in","the","directory","."]
code: def _train_model(
      self,
      train_data: classification_ds.ClassificationDataset,
      validation_data: classification_ds.ClassificationDataset,
      preprocessor: Optional[Callable[..., Any]] = None,
      checkpoint_path: Optional[str] = None,
  ):
      """Trains the classifier model.
  
      Compiles and fits the tf.keras `_model` and records the `_history`.
  
      Args:
        train_data: Training data.
        validation_data: Validation data.
        preprocessor: An optional data preprocessor that can be used when
          generating a tf.data.Dataset.
        checkpoint_path: An optional directory for the checkpoint file to support
          continual training. If provided, loads model weights from the latest
          checkpoint in the directory.
      """
      tf.compat.v1.logging.info("Training the models...")
      if len(train_data) < self._hparams.batch_size:
          raise ValueError(
              f"The size of the train_data {len(train_data)} can't be smaller than"
              f" batch_size {self._hparams.batch_size}. To solve this problem, set"
              " the batch_size smaller or increase the size of the train_data."
          )
  
      train_dataset = train_data.gen_tf_dataset(
          batch_size=self._hparams.batch_size,
          is_training=True,
          shuffle=self._shuffle,
          preprocess=preprocessor,
      )
      self._hparams.steps_per_epoch = model_util.get_steps_per_epoch(
          steps_per_epoch=self._hparams.steps_per_epoch,
          batch_size=self._hparams.batch_size,
          train_data=train_data,
      )
      train_dataset = train_dataset.take(
          count=self._hparams.steps_per_epoch
      )
      validation_dataset = validation_data.gen_tf_dataset(
          batch_size=self._hparams.batch_size,
          is_training=False,
          preprocess=preprocessor,
      )
      self._model.compile(
          optimizer=self._optimizer,
          loss=self._loss_function,
          metrics=self._metric_functions,
      )
  
      latest_checkpoint = (
          tf.train.latest_checkpoint(checkpoint_path)
          if checkpoint_path
          else None
      )
      if latest_checkpoint:
          print(f"Resuming from {latest_checkpoint}")
          self._model.load_weights(latest_checkpoint)
  
      self._history = self._model.fit(
          x=train_dataset,
          epochs=self._hparams.epochs,
          # `steps_per_epoch` is intentionally set to None in case the dataset
          # is not repeated. Otherwise, the training process will stop when the
          # dataset is exhausted even if there are epochs remaining.
          steps_per_epoch=None,
          validation_data=validation_dataset,
          callbacks=self._callbacks,
          class_weight=self._hparams.class_weights,
      )
  
file_path: mediapipe/mediapipe/model_maker/python/core/tasks/classifier.py
imports: import os
  from typing import Any, Callable, Optional, Sequence, Union
  import tensorflow
  from mediapipe.model_maker.python.core import hyperparameters
  from mediapipe.model_maker.python.core.data import classification_dataset
  from mediapipe.model_maker.python.core.data import dataset
  from mediapipe.model_maker.python.core.tasks import custom_model
  from mediapipe.model_maker.python.core.utils import model_util
label_desc: Use image node_id 2 for calling the Classifier obj's underlying member method code with example usage: obj._train_model(train_data, validation_data, preprocessor, checkpoint_path) without return types