def testBinaryClassifierTrainInMemoryAndEvalAndInfer(self):
    train_input_fn = _make_train_input_fn(is_classification=True)
    predict_input_fn = numpy_io.numpy_input_fn(
        x=FEATURES_DICT, y=None, batch_size=1, num_epochs=1, shuffle=False)

    est = boosted_trees.boosted_trees_classifier_train_in_memory(
        train_input_fn=train_input_fn,
        feature_columns=self._feature_columns,
        n_trees=1,
        max_depth=5)
    # It will stop after 5 steps because of the max depth and num trees.
    self._assert_checkpoint(est.model_dir, 6)

    # Check eval.
    eval_res = est.evaluate(input_fn=train_input_fn, steps=1)
    self.assertAllClose(eval_res['accuracy'], 1.0)

    # Check predict that all labels are correct.
    predictions = list(est.predict(input_fn=predict_input_fn))
    self.assertEquals(5, len(predictions))
    self.assertAllClose([0], predictions[0]['class_ids'])
    self.assertAllClose([1], predictions[1]['class_ids'])
    self.assertAllClose([1], predictions[2]['class_ids'])
    self.assertAllClose([0], predictions[3]['class_ids'])
    self.assertAllClose([0], predictions[4]['class_ids'])
Esempio n. 2
0
    def testBinaryClassifierTrainInMemoryAndEvalAndInfer(self):
        train_input_fn = _make_train_input_fn(is_classification=True)
        predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT,
                                                   y=None,
                                                   batch_size=1,
                                                   num_epochs=1,
                                                   shuffle=False)

        est = boosted_trees.boosted_trees_classifier_train_in_memory(
            train_input_fn=train_input_fn,
            feature_columns=self._feature_columns,
            n_trees=1,
            max_depth=5)
        # It will stop after 5 steps because of the max depth and num trees.
        self._assert_checkpoint(est.model_dir, 6)

        # Check eval.
        eval_res = est.evaluate(input_fn=train_input_fn, steps=1)
        self.assertAllClose(eval_res['accuracy'], 1.0)

        # Check predict that all labels are correct.
        predictions = list(est.predict(input_fn=predict_input_fn))
        self.assertEquals(5, len(predictions))
        self.assertAllClose([0], predictions[0]['class_ids'])
        self.assertAllClose([1], predictions[1]['class_ids'])
        self.assertAllClose([1], predictions[2]['class_ids'])
        self.assertAllClose([0], predictions[3]['class_ids'])
        self.assertAllClose([0], predictions[4]['class_ids'])
Esempio n. 3
0
    def testBinaryClassifierTrainInMemoryWithDataset(self):
        train_input_fn = _make_train_input_fn_dataset(is_classification=True)
        predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT,
                                                   y=None,
                                                   batch_size=1,
                                                   num_epochs=1,
                                                   shuffle=False)

        est = boosted_trees.boosted_trees_classifier_train_in_memory(
            train_input_fn=train_input_fn,
            feature_columns=self._feature_columns,
            n_trees=1,
            max_depth=5)
        # It will stop after 5 steps because of the max depth and num trees.
        self._assert_checkpoint(est.model_dir,
                                global_step=5,
                                finalized_trees=1,
                                attempted_layers=5)

        # Check evaluate and predict.
        eval_res = est.evaluate(input_fn=train_input_fn, steps=1)
        self.assertAllClose(eval_res['accuracy'], 1.0)
        predictions = list(est.predict(input_fn=predict_input_fn))
        self.assertAllClose([[0], [1], [1], [0], [0]],
                            [pred['class_ids'] for pred in predictions])
Esempio n. 4
0
    def testBinaryClassifierTrainInMemoryAndEvalAndInferWithPrePruning(self):
        train_input_fn = _make_train_input_fn(is_classification=True)
        predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT,
                                                   y=None,
                                                   batch_size=1,
                                                   num_epochs=1,
                                                   shuffle=False)

        est = boosted_trees.boosted_trees_classifier_train_in_memory(
            train_input_fn=train_input_fn,
            feature_columns=self._feature_columns,
            n_trees=1,
            max_depth=5,
            pruning_mode='pre',
            tree_complexity=0.01)
        # We stop actually after 2*depth*n_trees steps (via a hook) because we still
        # could not grow 1 trees of depth 5 (due to pre-pruning).
        self._assert_checkpoint(est.model_dir,
                                global_step=11,
                                finalized_trees=0,
                                attempted_layers=11)

        # Check evaluate and predict.
        eval_res = est.evaluate(input_fn=train_input_fn, steps=1)
        self.assertAllClose(eval_res['accuracy'], 1.0)
        # Validate predictions.
        predictions = list(est.predict(input_fn=predict_input_fn))
        self.assertAllClose([[0], [1], [1], [0], [0]],
                            [pred['class_ids'] for pred in predictions])
  def testBinaryClassifierTrainInMemoryWithDataset(self):
    train_input_fn = _make_train_input_fn_dataset(is_classification=True)
    predict_input_fn = numpy_io.numpy_input_fn(
        x=FEATURES_DICT, y=None, batch_size=1, num_epochs=1, shuffle=False)

    est = boosted_trees.boosted_trees_classifier_train_in_memory(
        train_input_fn=train_input_fn, feature_columns=self._feature_columns,
        n_trees=1, max_depth=5)
    # It will stop after 5 steps because of the max depth and num trees.
    self._assert_checkpoint(
        est.model_dir, global_step=5, finalized_trees=1, attempted_layers=5)

    # Check evaluate and predict.
    eval_res = est.evaluate(input_fn=train_input_fn, steps=1)
    self.assertAllClose(eval_res['accuracy'], 1.0)
    predictions = list(est.predict(input_fn=predict_input_fn))
    self.assertAllClose([[0], [1], [1], [0], [0]],
                        [pred['class_ids'] for pred in predictions])
Esempio n. 6
0
  def testBinaryClassifierTrainInMemoryAndEvalAndInferWithPrePruning(self):
    train_input_fn = _make_train_input_fn(is_classification=True)
    predict_input_fn = numpy_io.numpy_input_fn(
        x=FEATURES_DICT, y=None, batch_size=1, num_epochs=1, shuffle=False)

    est = boosted_trees.boosted_trees_classifier_train_in_memory(
        train_input_fn=train_input_fn,
        feature_columns=self._feature_columns,
        n_trees=1,
        max_depth=5,
        pruning_mode='pre',
        tree_complexity=0.01)
    # We stop actually after 2*depth*n_trees steps (via a hook) because we still
    # could not grow 1 trees of depth 5 (due to pre-pruning).
    self._assert_checkpoint(
        est.model_dir, global_step=11, finalized_trees=0, attempted_layers=11)

    # Check evaluate and predict.
    eval_res = est.evaluate(input_fn=train_input_fn, steps=1)
    self.assertAllClose(eval_res['accuracy'], 1.0)
    # Validate predictions.
    predictions = list(est.predict(input_fn=predict_input_fn))
    self.assertAllClose([[0], [1], [1], [0], [0]],
                        [pred['class_ids'] for pred in predictions])