code: def train_and_evaluate(
      raw_train_eval_data_path_pattern,
      transformed_train_eval_data_path_pattern,
      output_dir,
      transform_output_dir,
      num_train_instances=common.NUM_TRAIN_INSTANCES,
      num_test_instances=common.NUM_TEST_INSTANCES,
  ):
      """Train the model on training data and evaluate on test data.
  
      Args:
        raw_train_eval_data_path_pattern: A pair of patterns of raw
          (train data file paths, eval data file paths) in CSV format.
        transformed_train_eval_data_path_pattern: A pair of patterns of transformed
          (train data file paths, eval data file paths) in TFRecord format.
        output_dir: A directory where the output should be exported to.
        transform_output_dir: The location of the Transform output.
        num_train_instances: Number of instances in train set
        num_test_instances: Number of instances in test set
  
      Returns:
        The results from the estimator's 'evaluate' method
      """
      if not (
          (raw_train_eval_data_path_pattern is None)
          ^ (transformed_train_eval_data_path_pattern is None)
      ):
          raise ValueError(
              "Exactly one of raw_train_eval_data_path_pattern and "
              "transformed_train_eval_data_path_pattern should be provided"
          )
      tf_transform_output = tft.TFTransformOutput(transform_output_dir)
  
      if raw_train_eval_data_path_pattern is not None:
          selected_input_fn = input_fn_raw
          (
              train_data_path_pattern,
              eval_data_path_pattern,
          ) = raw_train_eval_data_path_pattern
      else:
          selected_input_fn = input_fn
          (
              train_data_path_pattern,
              eval_data_path_pattern,
          ) = transformed_train_eval_data_path_pattern
  
      train_dataset = selected_input_fn(
          tf_transform_output,
          train_data_path_pattern,
          batch_size=common.TRAIN_BATCH_SIZE,
      )
  
      # Evaluate model on test dataset.
      validation_dataset = selected_input_fn(
          tf_transform_output,
          eval_data_path_pattern,
          batch_size=common.TRAIN_BATCH_SIZE,
      )
  
      feature_spec = (
          tf_transform_output.transformed_feature_spec().copy()
      )
      feature_spec.pop(common.LABEL_KEY)
  
      inputs = {}
      sparse_inputs = {}
      dense_inputs = {}
      for key, spec in feature_spec.items():
          if isinstance(spec, tf.io.FixedLenFeature):
              # TODO(b/208879020): Move into schema such that spec.shape is [1] and not
              # [] for scalars.
              inputs[key] = tf_keras.layers.Input(
                  shape=spec.shape or [1], name=key, dtype=spec.dtype
              )
              dense_inputs[key] = inputs[key]
          elif isinstance(spec, tf.io.SparseFeature):
              inputs[key] = tf_keras.layers.Input(
                  shape=spec.size,
                  name=key,
                  dtype=spec.dtype,
                  sparse=True,
              )
              sparse_inputs[key] = inputs[key]
          else:
              raise ValueError(
                  "Spec type is not supported: ", key, spec
              )
  
      outputs = [
          tf_keras.layers.Dense(10, activation="relu")(x)
          for x in tf.nest.flatten(sparse_inputs)
      ]
      stacked_inputs = tf.concat(
          tf.nest.flatten(dense_inputs) + outputs, axis=1
      )
      output = tf_keras.layers.Dense(100, activation="relu")(
          stacked_inputs
      )
      output = tf_keras.layers.Dense(70, activation="relu")(output)
      output = tf_keras.layers.Dense(50, activation="relu")(output)
      output = tf_keras.layers.Dense(20, activation="relu")(output)
      output = tf_keras.layers.Dense(2, activation="sigmoid")(output)
      model = tf_keras.Model(inputs=inputs, outputs=output)
  
      model.compile(
          optimizer="adam",
          loss="binary_crossentropy",
          metrics=["accuracy"],
      )
      logging.info(model.summary())
  
      model.fit(
          train_dataset,
          validation_data=validation_dataset,
          epochs=common.TRAIN_NUM_EPOCHS,
          steps_per_epoch=math.ceil(
              num_train_instances / common.TRAIN_BATCH_SIZE
          ),
          validation_steps=math.ceil(
              num_test_instances / common.TRAIN_BATCH_SIZE
          ),
      )
  
      # Export the model.
      export_serving_model(tf_transform_output, model, output_dir)
  
      return model.evaluate(
          validation_dataset, steps=num_test_instances
      )
  
file_path: transform/examples/census_example_v2.py
imports: import math
  import os
  import pprint
  import tempfile
  from absl import logging
  import tensorflow
  import tensorflow_transform
  import census_example_common
  from tensorflow_transform.keras_lib import tf_keras
label_desc: Use image node_id 4 for calling a global function with example usage: train_and_evaluate(raw_train_eval_data_path_pattern, transformed_train_eval_data_path_pattern, output_dir, transform_output_dir, num_train_instances, num_test_instances) and returns: model