code: def train_and_eval():
      """Train and evaluate ranking model."""
      optimizer = tf.keras.optimizers.Adagrad(
          learning_rate=FLAGS.learning_rate
      )
      loss = tfr.keras.losses.get(loss=FLAGS.loss)
      eval_metrics = tfr.keras.metrics.default_keras_metrics()
  
      model = create_ranking_model()
      model.compile(
          optimizer=optimizer, loss=loss, metrics=eval_metrics
      )
      model.summary()
  
      tensorboard_callback = tf.keras.callbacks.TensorBoard(
          FLAGS.model_dir
      )
  
      train_dataset = _create_dataset(
          file_pattern=FLAGS.train_input_pattern,
          batch_size=FLAGS.train_batch_size,
      )
  
      eval_dataset = _create_dataset(
          file_pattern=FLAGS.eval_input_pattern,
          batch_size=FLAGS.eval_batch_size,
          num_epochs=1,
          randomize_input=True,
      )
  
      # Train ranker.
      steps_per_epoch = FLAGS.num_train_steps // FLAGS.num_epochs
      logging.info("Training the model...")
      model.fit(
          train_dataset,
          steps_per_epoch=steps_per_epoch,
          epochs=FLAGS.num_epochs,
          validation_data=eval_dataset,
          callbacks=[tensorboard_callback],
      )
      logging.info("Finished training the model.")
  
      # Evaluate over test data.
      if FLAGS.test_input_pattern is not None:
          logging.info("Evaluating the model...")
          test_dataset = _create_dataset(
              file_pattern=FLAGS.test_input_pattern,
              batch_size=FLAGS.eval_batch_size,
              randomize_input=False,
              num_epochs=1,
          )
          model.evaluate(test_dataset)
          logging.info("Finished evaluating the model.")
  
      # Export SavedModel.
      context_feature_spec, example_feature_spec = _create_feature_spec(
          with_label=False
      )
      saved_model_path = os.path.join(FLAGS.model_dir, "export/")
      logging.info("Exporting to SavedModel...")
      model.save(
          filepath=saved_model_path,
          signatures=tfr.keras.saved_model.Signatures(
              model,
              context_feature_spec=context_feature_spec,
              example_feature_spec=example_feature_spec,
              mask_feature_name=_MASK,
          )(),
      )
      logging.info(
          "SavedModel exported successfully to: %s", saved_model_path
      )
      return model
  
file_path: ranking/tensorflow_ranking/examples/keras/antique_kpl_din.py
imports: import os
  from typing import Dict, Optional, Tuple, Union
  from absl import app
  from absl import flags
  from absl import logging
  import tensorflow
  import tensorflow_ranking
label_desc: Use image node_id 6 for calling a global function with example usage: train_and_eval() and returns: model