def train_and_evaluate(
    raw_train_eval_data_path_pattern: tuple[str, str],
    transformed_train_eval_data_path_pattern: tuple[str, str],
    output_dir: str,
    transform_output_dir: str,
    num_train_instances: int = NUM_TRAIN_INSTANCES,
    num_test_instances: int = 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=TRAIN_BATCH_SIZE
  )

  # Evaluate model on test dataset.
  validation_dataset = selected_input_fn(
      tf_transform_output, eval_data_path_pattern, batch_size=TRAIN_BATCH_SIZE
  )

  feature_spec = tf_transform_output.transformed_feature_spec().copy()
  feature_spec.pop(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=TRAIN_NUM_EPOCHS,
      steps_per_epoch=math.ceil(num_train_instances / TRAIN_BATCH_SIZE),
      validation_steps=math.ceil(num_test_instances / TRAIN_BATCH_SIZE),
  )

  # Export the model.
  export_serving_model(tf_transform_output, model, output_dir)

  return model.evaluate(validation_dataset, steps=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
  
examples/census_example_v2.py