code: def train_and_eval():
      """Train and Evaluate."""
      train_input_fn = make_input_fn(FLAGS.train_path, FLAGS.batch_size)
      eval_input_fn = make_input_fn(
          FLAGS.eval_path,
          FLAGS.batch_size,
          randomize_input=False,
          num_epochs=1,
      )
  
      optimizer = tf.compat.v1.train.AdagradOptimizer(
          learning_rate=FLAGS.learning_rate
      )
  
      def _train_op_fn(loss):
          """Defines train op used in ranking head."""
          update_ops = tf.compat.v1.get_collection(
              tf.compat.v1.GraphKeys.UPDATE_OPS
          )
          minimize_op = optimizer.minimize(
              loss=loss,
              global_step=tf.compat.v1.train.get_global_step(),
          )
          train_op = tf.group([minimize_op, update_ops])
          return train_op
  
      ranking_head = tfr.head.create_ranking_head(
          loss_fn=tfr.losses.make_loss_fn(
              FLAGS.loss,
              weights_feature_name=FLAGS.weights_feature_name,
          ),
          eval_metric_fns=eval_metric_fns(),
          train_op_fn=_train_op_fn,
      )
  
      estimator = tf_estimator.Estimator(
          model_fn=tfr.model.make_groupwise_ranking_fn(
              group_score_fn=make_score_fn(),
              group_size=FLAGS.group_size,
              transform_fn=make_transform_fn(),
              ranking_head=ranking_head,
          ),
          model_dir=FLAGS.model_dir,
          config=tf_estimator.RunConfig(save_checkpoints_steps=1000),
      )
  
      train_spec = tf_estimator.TrainSpec(
          input_fn=train_input_fn, max_steps=FLAGS.num_train_steps
      )
  
      exporters = tf_estimator.LatestExporter(
          "saved_model_exporter",
          serving_input_receiver_fn=make_serving_input_fn(),
      )
  
      eval_spec = tf_estimator.EvalSpec(
          name="eval",
          input_fn=eval_input_fn,
          steps=1,
          exporters=exporters,
          start_delay_secs=0,
          throttle_secs=15,
      )
  
      # Train and validate.
      tf_estimator.train_and_evaluate(estimator, train_spec, eval_spec)
  
file_path: ranking/tensorflow_ranking/examples/tf_ranking_tfrecord.py
imports: from absl import flags
  import tensorflow
  from tensorflow import estimator
  import tensorflow_ranking
label_desc: Use image node_id 8 for calling a global function with example usage: train_and_eval() and returns: train_op