Esempio n. 1
0
  def test_toy_rf_classification_weighted(self, add_boolean_features):

    with tf.Graph().as_default():

      # Create toy model.
      model_path = os.path.join(
          tempfile.mkdtemp(dir=self.get_temp_dir()), "test_basic_rf_weighted")
      test_utils.build_toy_random_forest(
          model_path,
          winner_take_all_inference=False,
          add_boolean_features=add_boolean_features)
      features = test_utils.build_toy_input_features()

      # Prepare model.
      model = inference.Model(model_path)
      predictions = model.apply(features)

      # Run model on toy dataset.
      with self.session() as sess:
        sess.run(model.init_op())

        dense_predictions_values, dense_col_representation_values = sess.run([
            predictions.dense_predictions, predictions.dense_col_representation
        ], test_utils.build_toy_input_feature_values(features))
        logging.info("dense_predictions_values: %s", dense_predictions_values)
        logging.info("dense_col_representation_values: %s",
                     dense_col_representation_values)

        expected_proba, expected_classes = test_utils.expected_toy_predictions_rf_weighted(
            add_boolean_features=add_boolean_features)
        self.assertAllEqual(dense_col_representation_values, expected_classes)
        self.assertAllClose(dense_predictions_values, expected_proba)
Esempio n. 2
0
  def test_toy_gbdt_multiclass_classification(self):

    with tf.Graph().as_default():
      # Create toy model.
      model_path = os.path.join(
          tempfile.mkdtemp(dir=self.get_temp_dir()),
          "test_basic_gbdt_multiclass")
      test_utils.build_toy_gbdt(model_path, num_classes=3)
      features = test_utils.build_toy_input_features()

      # Prepare model.
      model = inference.Model(model_path)
      predictions = model.apply(features)

      # Run model on toy dataset.
      with self.session() as sess:
        sess.run(model.init_op())

        dense_predictions_values, dense_col_representation_values = sess.run([
            predictions.dense_predictions, predictions.dense_col_representation
        ], test_utils.build_toy_input_feature_values(features))
        logging.info("dense_predictions_values: %s", dense_predictions_values)
        logging.info("dense_col_representation_values: %s",
                     dense_col_representation_values)

        expected_proba, expected_classes = test_utils.expected_toy_predictions_gbdt_multiclass(
        )
        self.assertAllEqual(dense_col_representation_values, expected_classes)
        self.assertAllClose(dense_predictions_values, expected_proba)
Esempio n. 3
0
  def test_get_leaves(self):
    """Access the active leaves of the model."""

    model_path = os.path.join(
        tempfile.mkdtemp(dir=self.get_temp_dir()), "test_get_leaves")
    test_utils.build_toy_random_forest(
        model_path, winner_take_all_inference=True, num_trees=6)
    features = test_utils.build_toy_input_feature_values(features=None)

    model = inference.ModelV2(model_path, output_types=["LEAVES"])

    leaves = model.apply_get_leaves(features)
    logging.info("Leaves: %s", leaves)

    self.assertAllEqual(leaves, [[3] * 6, [2] * 6, [1] * 6, [0] * 6])
Esempio n. 4
0
  def test_toy_rf_classification_winner_takes_all_v2(self, add_boolean_features,
                                                     has_catset):

    # Create toy model.
    model_path = os.path.join(
        tempfile.mkdtemp(dir=self.get_temp_dir()), "test_basic_rf_wta")
    test_utils.build_toy_random_forest(
        model_path,
        winner_take_all_inference=True,
        add_boolean_features=add_boolean_features,
        has_catset=has_catset)
    features = test_utils.build_toy_input_feature_values(
        features=None, has_catset=has_catset)

    # Prepare model.
    tf.print("Loading model")
    model = inference.ModelV2(model_path)

    @tf.function
    def apply_non_eager(x):
      return model.apply(x)

    predictions_non_eager = apply_non_eager(features)
    predictions_eager = model.apply(features)

    def check_predictions(predictions):
      print("Predictions: %s", predictions)

      logging.info("dense_predictions_values: %s",
                   predictions.dense_predictions)
      logging.info("dense_col_representation_values: %s",
                   predictions.dense_col_representation)

      (expected_proba,
       expected_classes) = test_utils.expected_toy_predictions_rf_wta(
           add_boolean_features=add_boolean_features, has_catset=has_catset)
      self.assertAllEqual(predictions.dense_col_representation,
                          expected_classes)
      self.assertAllClose(predictions.dense_predictions, expected_proba)

    check_predictions(predictions_non_eager)
    check_predictions(predictions_eager)
Esempio n. 5
0
  def test_multi_thread(self):

    # Create toy model with a lot of trees i.e. slow model.
    model_path = os.path.join(
        tempfile.mkdtemp(dir=self.get_temp_dir()), "test_multi_thread")
    test_utils.build_toy_random_forest(
        model_path, winner_take_all_inference=True, num_trees=100000)
    features = test_utils.build_toy_input_feature_values(features=None)

    model = inference.ModelV2(model_path)

    @tf.function
    def apply_non_eager(x):
      return model.apply(x)

    with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor:
      predictions = executor.map(apply_non_eager, [features] * 1000)

    (expected_proba,
     expected_classes) = test_utils.expected_toy_predictions_rf_wta()

    for prediction in predictions:
      self.assertAllEqual(prediction.dense_col_representation, expected_classes)
      self.assertAllClose(prediction.dense_predictions, expected_proba)
Esempio n. 6
0
 def good_feature_values():
   return test_utils.build_toy_input_feature_values(
       features, has_catset=True)