class_docstr: Pipeline using `model.fit` to train a ranking `tf.keras.Model`.
  
  The `ModelFitPipeline` class is an abstract class inherit from
  `AbstractPipeline` to train and validate a ranking `model` with `model.fit`
  in a distributed strategy specified in hparams.
  
  To be implemented by subclasses:
  
    * `build_loss()`: Contains the logic to build a `tf.keras.losses.Loss` or a
    dict or list of `tf.keras.losses.Loss`s to be optimized in training.
    * `build_metrics()`: Contains the logic to build a list or dict of
    `tf.keras.metrics.Metric`s to monitor and evaluate the training.
    * `build_weighted_metrics()`: Contains the logic to build a list or dict of
    `tf.keras.metrics.Metric`s which will take the weights.
  
  Example subclass implementation:
  
  ```python
  class BasicModelFitPipeline(ModelFitPipeline):
  
    def build_loss(self):
      return tfr.keras.losses.get('softmax_loss')
  
    def build_metrics(self):
      return [
          tfr.keras.metrics.get(
              'ndcg', topn=topn, name='ndcg_{}'.format(topn)
          ) for topn in [1, 5, 10]
      ]
  
    def build_weighted_metrics(self):
      return [
          tfr.keras.metrics.get(
              'ndcg', topn=topn, name='weighted_ndcg_{}'.format(topn)
          ) for topn in [1, 5, 10]
      ]
  ```
class_docstr_tok: ["Pipeline","using","`","model.fit","`","to","train","a","ranking","`","tf.keras.Model","`",".","The","`","ModelFitPipeline","`","class","is","an","abstract","class","inherit","from","`","AbstractPipeline","`","to","train","and","validate","a","ranking","`","model","`","with","`","model.fit","`","in","a","distributed","strategy","specified","in","hparams",".","To","be","implemented","by","subclasses",":","*","`","build_loss","(",")","`",":","Contains","the","logic","to","build","a","`","tf.keras.losses.Loss","`","or","a","dict","or","list","of","`","tf.keras.losses.Loss","`","s","to","be","optimized","in","training",".","*","`","build_metrics","(",")","`",":","Contains","the","logic","to","build","a","list","or","dict","of","`","tf.keras.metrics.Metric","`","s","to","monitor","and","evaluate","the","training",".","*","`","build_weighted_metrics","(",")","`",":","Contains","the","logic","to","build","a","list","or","dict","of","`","tf.keras.metrics.Metric","`","s","which","will","take","the","weights",".","Example","subclass","implementation",":","``","`","python","class","BasicModelFitPipeline","(","ModelFitPipeline",")",":","def","build_loss","(","self",")",":","return","tfr.keras.losses.get","(","'softmax_loss","'",")","def","build_metrics","(","self",")",":","return","[","tfr.keras.metrics.get","(","'ndcg","'",",","topn=topn",",","name='ndcg_","{","}","'.format","(","topn",")",")","for","topn","in","[","1",",","5",",","10","]","]","def","build_weighted_metrics","(","self",")",":","return","[","tfr.keras.metrics.get","(","'ndcg","'",",","topn=topn",",","name='weighted_ndcg_","{","}","'.format","(","topn",")",")","for","topn","in","[","1",",","5",",","10","]","]","``","`"]
docstr: Sets up Callbacks.
  
  Example usage:
  
  ```python
  model_builder = ModelBuilder(...)
  dataset_builder = DatasetBuilder(...)
  hparams = PipelineHparams(...)
  pipeline = BasicModelFitPipeline(model_builder, dataset_builder, hparams)
  callbacks = pipeline.build_callbacks()
  ```
  
  Returns:
    A list of `tf.keras.callbacks.Callback` or a
    `tf.keras.callbacks.CallbackList` for tensorboard and checkpoint.
code: def build_callbacks(self) -> List[tf.keras.callbacks.Callback]:
      """Sets up Callbacks.
  
      Example usage:
  
      ```python
      model_builder = ModelBuilder(...)
      dataset_builder = DatasetBuilder(...)
      hparams = PipelineHparams(...)
      pipeline = BasicModelFitPipeline(model_builder, dataset_builder, hparams)
      callbacks = pipeline.build_callbacks()
      ```
  
      Returns:
        A list of `tf.keras.callbacks.Callback` or a
        `tf.keras.callbacks.CallbackList` for tensorboard and checkpoint.
      """
      # Writing summary logs to file may have performance impact. Therefore, we
      # only write summary events every epoch.
      callbacks = [
          tf.keras.callbacks.TensorBoard(self._hparams.model_dir),
          tf.keras.callbacks.experimental.BackupAndRestore(
              backup_dir=self._hparams.model_dir
          ),
      ]
  
      if self._hparams.export_best_model:
          # default to be min of loss metric.
          best_export_metric = self._hparams.best_exporter_metric
          if best_export_metric != "loss":
              best_export_metric = "metric/" + best_export_metric
          callbacks.append(
              tf.keras.callbacks.ModelCheckpoint(
                  os.path.join(
                      self._hparams.model_dir,
                      "best_checkpoint/ckpt-{epoch:04d}",
                  ),
                  monitor="val_" + best_export_metric,
                  mode=(
                      "max"
                      if self._hparams.best_exporter_metric_higher_better
                      else "min"
                  ),
                  save_weights_only=True,
                  save_best_only=True,
              )
          )
  
      if self._hparams.automatic_reduce_lr:
          callbacks.append(
              tf.keras.callbacks.ReduceLROnPlateau(
                  monitor="val_loss",
                  min_delta=0.01 * self._hparams.learning_rate,
              )
          )
  
      if self._hparams.early_stopping_patience:
          best_export_metric = self._hparams.best_exporter_metric
          if best_export_metric != "loss":
              best_export_metric = "metric/" + best_export_metric
          callbacks.append(
              tf.keras.callbacks.EarlyStopping(
                  monitor="val_" + best_export_metric,
                  min_delta=self._hparams.early_stopping_min_delta,
                  patience=self._hparams.early_stopping_patience,
                  mode=(
                      "max"
                      if self._hparams.best_exporter_metric_higher_better
                      else "min"
                  ),
              )
          )
  
      return callbacks
  
imports: import abc
  import dataclasses
  import os
  from typing import Any, Dict, List, Optional, Tuple, Union
  import tensorflow
  from tensorflow_ranking.python import data
  from tensorflow_ranking.python.keras import losses
  from tensorflow_ranking.python.keras import metrics
  from tensorflow_ranking.python.keras import model
  from tensorflow_ranking.python.keras import saved_model
  from tensorflow_ranking.python.keras import strategy_utils
label_desc: Use image node_id 3 for calling the ModelFitPipeline obj's underlying member method code with example usage: obj.build_callbacks() and returns: callbacks