def train_and_evaluate(
    working_dir: str,
    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:
    working_dir: Directory to read transformed data and metadata from.
    output_dir: A directory where the output should be exported to.
    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
  """
  tf_transform_output = tft.TFTransformOutput(working_dir)
  train_data_path_pattern = os.path.join(
      working_dir, TRANSFORMED_TRAIN_DATA_FILEBASE + '*'
  )
  test_data_path_pattern = os.path.join(
      working_dir, TRANSFORMED_TEST_DATA_FILEBASE + '*'
  )

  train_dataset = _input_fn(
      tf_transform_output, train_data_path_pattern, batch_size=TRAIN_BATCH_SIZE
  )
  validation_dataset = _input_fn(
      tf_transform_output, test_data_path_pattern, batch_size=TRAIN_BATCH_SIZE
  )

  feature_spec = tf_transform_output.transformed_feature_spec().copy()
  feature_spec.pop(LABEL_KEY)

  review_input = tf_keras.layers.Input(
      shape=[None], name=REVIEW_KEY, dtype=tf.int64, sparse=True
  )
  review_weight_input = tf_keras.layers.Input(
      shape=[None], name=REVIEW_WEIGHT_KEY, dtype=tf.float32, sparse=True
  )
  count_layer = tf.keras.layers.CategoryEncoding(
      num_tokens=VOCAB_SIZE + 1, output_mode='count'
  )
  embedding_layer = tf.keras.layers.Dense(4, use_bias=False)
  embedding = embedding_layer(
      count_layer(review_input, count_weights=review_weight_input)
  )
  output = tf_keras.layers.Dense(100, activation='relu')(embedding)
  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={
          REVIEW_KEY: review_input,
          REVIEW_WEIGHT_KEY: review_weight_input,
      },
      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:
    working_dir: Directory to read transformed data and metadata from.
    output_dir: A directory where the output should be exported to.
    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/sentiment_example_v2.py