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

    est = boosted_trees._BoostedTreesEstimator(
        feature_columns=self._feature_columns,
        n_batches_per_layer=1,
        n_trees=1,
        max_depth=5,
        head=self._head)

    # It will stop after 5 steps because of the max depth and num trees.
    num_steps = 100
    # Train for a few steps, and validate final checkpoint.
    est.train(train_input_fn, steps=num_steps)
    self._assert_checkpoint(est.model_dir, 6)

    predictions = list(est.predict(input_fn=predict_input_fn))
    self.assertEquals(5, len(predictions))
    self.assertAllClose([0.703549], predictions[0]['predictions'])
    self.assertAllClose([0.266539], predictions[1]['predictions'])
    self.assertAllClose([0.256479], predictions[2]['predictions'])
    self.assertAllClose([1.088732], predictions[3]['predictions'])
    self.assertAllClose([1.901732], predictions[4]['predictions'])
Esempio n. 2
0
    def testInferEstimator(self):
        train_input_fn = _make_train_input_fn(is_classification=False)
        predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT,
                                                   y=None,
                                                   batch_size=1,
                                                   num_epochs=1,
                                                   shuffle=False)

        est = boosted_trees._BoostedTreesEstimator(
            feature_columns=self._feature_columns,
            n_batches_per_layer=1,
            n_trees=1,
            max_depth=5,
            head=self._head)

        # It will stop after 5 steps because of the max depth and num trees.
        num_steps = 100
        # Train for a few steps, and validate final checkpoint.
        est.train(train_input_fn, steps=num_steps)
        self._assert_checkpoint(est.model_dir, 6)

        predictions = list(est.predict(input_fn=predict_input_fn))
        self.assertEquals(5, len(predictions))
        self.assertAllClose([0.703549], predictions[0]['predictions'])
        self.assertAllClose([0.266539], predictions[1]['predictions'])
        self.assertAllClose([0.256479], predictions[2]['predictions'])
        self.assertAllClose([1.088732], predictions[3]['predictions'])
        self.assertAllClose([1.901732], predictions[4]['predictions'])
Esempio n. 3
0
    def testInferEstimatorWithCenterBias(self):
        train_input_fn = _make_train_input_fn(is_classification=False)
        predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT,
                                                   y=None,
                                                   batch_size=1,
                                                   num_epochs=1,
                                                   shuffle=False)

        est = boosted_trees._BoostedTreesEstimator(
            feature_columns=self._feature_columns,
            n_batches_per_layer=1,
            n_trees=1,
            max_depth=5,
            center_bias=True,
            head=self._head)

        # It will stop after 6 steps because of the max depth and num trees (5 for
        # training and 2 for bias centering).
        num_steps = 100
        # Train for a few steps, and validate final checkpoint.
        est.train(train_input_fn, steps=num_steps)
        self._assert_checkpoint(est.model_dir,
                                global_step=7,
                                finalized_trees=1,
                                attempted_layers=5)
        # Validate predictions.
        predictions = list(est.predict(input_fn=predict_input_fn))

        self.assertAllClose(
            [[1.634501], [1.325703], [1.187431], [2.019683], [2.832683]],
            [pred['predictions'] for pred in predictions])
  def testInferEstimatorWithCenterBias(self):
    train_input_fn = _make_train_input_fn(is_classification=False)
    predict_input_fn = numpy_io.numpy_input_fn(
        x=FEATURES_DICT, y=None, batch_size=1, num_epochs=1, shuffle=False)

    est = boosted_trees._BoostedTreesEstimator(
        feature_columns=self._feature_columns,
        n_batches_per_layer=1,
        n_trees=1,
        max_depth=5,
        center_bias=True,
        head=self._head)

    # It will stop after 6 steps because of the max depth and num trees (5 for
    # training and 2 for bias centering).
    num_steps = 100
    # Train for a few steps, and validate final checkpoint.
    est.train(train_input_fn, steps=num_steps)
    self._assert_checkpoint(
        est.model_dir, global_step=7, finalized_trees=1, attempted_layers=5)
    # Validate predictions.
    predictions = list(est.predict(input_fn=predict_input_fn))

    self.assertAllClose(
        [[1.634501], [1.325703], [1.187431], [2.019683], [2.832683]],
        [pred['predictions'] for pred in predictions])
Esempio n. 5
0
    def testInferEstimator(self):
        train_input_fn = _make_train_input_fn(is_classification=False)
        predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT,
                                                   y=None,
                                                   batch_size=1,
                                                   num_epochs=1,
                                                   shuffle=False)

        est = boosted_trees._BoostedTreesEstimator(
            feature_columns=self._feature_columns,
            n_batches_per_layer=1,
            n_trees=1,
            max_depth=5,
            head=self._head)

        # It will stop after 5 steps because of the max depth and num trees.
        num_steps = 100
        # Train for a few steps, and validate final checkpoint.
        est.train(train_input_fn, steps=num_steps)
        self._assert_checkpoint(est.model_dir,
                                global_step=5,
                                finalized_trees=1,
                                attempted_layers=5)
        # Validate predictions.
        predictions = list(est.predict(input_fn=predict_input_fn))
        self.assertAllClose(
            [[0.571619], [0.262821], [0.124549], [0.956801], [1.769801]],
            [pred['predictions'] for pred in predictions])
  def testTrainAndEvaluateEstimator(self):
    input_fn = _make_train_input_fn(is_classification=False)

    est = boosted_trees._BoostedTreesEstimator(
        feature_columns=self._feature_columns,
        n_batches_per_layer=1,
        n_trees=2,
        head=self._head,
        max_depth=5)

    # It will stop after 10 steps because of the max depth and num trees.
    num_steps = 100
    # Train for a few steps, and validate final checkpoint.
    est.train(input_fn, steps=num_steps)
    self._assert_checkpoint(est.model_dir, 11)
    eval_res = est.evaluate(input_fn=input_fn, steps=1)
    self.assertAllClose(eval_res['average_loss'], 0.913176)
Esempio n. 7
0
    def testTrainAndEvaluateEstimator(self):
        input_fn = _make_train_input_fn(is_classification=False)

        est = boosted_trees._BoostedTreesEstimator(
            feature_columns=self._feature_columns,
            n_batches_per_layer=1,
            n_trees=2,
            head=self._head,
            max_depth=5)

        # It will stop after 10 steps because of the max depth and num trees.
        num_steps = 100
        # Train for a few steps, and validate final checkpoint.
        est.train(input_fn, steps=num_steps)
        self._assert_checkpoint(est.model_dir, 11)
        eval_res = est.evaluate(input_fn=input_fn, steps=1)
        self.assertAllClose(eval_res['average_loss'], 0.913176)
  def testTrainAndEvaluateEstimatorWithCenterBias(self):
    input_fn = _make_train_input_fn(is_classification=False)

    est = boosted_trees._BoostedTreesEstimator(
        feature_columns=self._feature_columns,
        n_batches_per_layer=1,
        n_trees=2,
        head=self._head,
        max_depth=5,
        center_bias=True)

    # It will stop after 11 steps because of the max depth and num trees.
    num_steps = 100
    # Train for a few steps, and validate final checkpoint.
    est.train(input_fn, steps=num_steps)
    # 10 steps for training and 2 step for bias centering.
    self._assert_checkpoint(
        est.model_dir, global_step=12, finalized_trees=2, attempted_layers=10)
    eval_res = est.evaluate(input_fn=input_fn, steps=1)
    self.assertAllClose(eval_res['average_loss'], 0.614642)
Esempio n. 9
0
  def testTrainAndEvaluateEstimatorWithPostPruning(self):
    input_fn = _make_train_input_fn(is_classification=False)

    est = boosted_trees._BoostedTreesEstimator(
        feature_columns=self._feature_columns,
        n_batches_per_layer=1,
        n_trees=2,
        head=self._head,
        max_depth=5,
        tree_complexity=0.001,
        pruning_mode='post')

    # It will stop after 10 steps because of the max depth and num trees.
    num_steps = 100
    # Train for a few steps, and validate final checkpoint.
    est.train(input_fn, steps=num_steps)
    self._assert_checkpoint(
        est.model_dir, global_step=10, finalized_trees=2, attempted_layers=10)
    eval_res = est.evaluate(input_fn=input_fn, steps=1)
    self.assertAllClose(eval_res['average_loss'], 2.37652)
Esempio n. 10
0
  def testTrainAndEvaluateEstimatorWithPrePruning(self):
    input_fn = _make_train_input_fn(is_classification=False)

    est = boosted_trees._BoostedTreesEstimator(
        feature_columns=self._feature_columns,
        n_batches_per_layer=1,
        n_trees=2,
        head=self._head,
        max_depth=5,
        tree_complexity=0.001,
        pruning_mode='pre')

    num_steps = 100
    # Train for a few steps, and validate final checkpoint.
    est.train(input_fn, steps=num_steps)
    # We stop actually after 2*depth*n_trees steps (via a hook) because we still
    # could not grow 2 trees of depth 5 (due to pre-pruning).
    self._assert_checkpoint(
        est.model_dir, global_step=21, finalized_trees=0, attempted_layers=21)
    eval_res = est.evaluate(input_fn=input_fn, steps=1)
    self.assertAllClose(eval_res['average_loss'], 3.83943)
Esempio n. 11
0
    def testTrainAndEvaluateEstimatorWithCenterBias(self):
        input_fn = _make_train_input_fn(is_classification=False)

        est = boosted_trees._BoostedTreesEstimator(
            feature_columns=self._feature_columns,
            n_batches_per_layer=1,
            n_trees=2,
            head=self._head,
            max_depth=5,
            center_bias=True)

        # It will stop after 11 steps because of the max depth and num trees.
        num_steps = 100
        # Train for a few steps, and validate final checkpoint.
        est.train(input_fn, steps=num_steps)
        # 10 steps for training and 2 step for bias centering.
        self._assert_checkpoint(est.model_dir,
                                global_step=12,
                                finalized_trees=2,
                                attempted_layers=10)
        eval_res = est.evaluate(input_fn=input_fn, steps=1)
        self.assertAllClose(eval_res['average_loss'], 0.614642)
Esempio n. 12
0
    def testTrainAndEvaluateEstimatorWithPostPruning(self):
        input_fn = _make_train_input_fn(is_classification=False)

        est = boosted_trees._BoostedTreesEstimator(
            feature_columns=self._feature_columns,
            n_batches_per_layer=1,
            n_trees=2,
            head=self._head,
            max_depth=5,
            tree_complexity=0.001,
            pruning_mode='post')

        # It will stop after 10 steps because of the max depth and num trees.
        num_steps = 100
        # Train for a few steps, and validate final checkpoint.
        est.train(input_fn, steps=num_steps)
        self._assert_checkpoint(est.model_dir,
                                global_step=10,
                                finalized_trees=2,
                                attempted_layers=10)
        eval_res = est.evaluate(input_fn=input_fn, steps=1)
        self.assertAllClose(eval_res['average_loss'], 2.37652)
Esempio n. 13
0
    def testTrainAndEvaluateEstimatorWithPrePruning(self):
        input_fn = _make_train_input_fn(is_classification=False)

        est = boosted_trees._BoostedTreesEstimator(
            feature_columns=self._feature_columns,
            n_batches_per_layer=1,
            n_trees=2,
            head=self._head,
            max_depth=5,
            tree_complexity=0.001,
            pruning_mode='pre')

        num_steps = 100
        # Train for a few steps, and validate final checkpoint.
        est.train(input_fn, steps=num_steps)
        # We stop actually after 2*depth*n_trees steps (via a hook) because we still
        # could not grow 2 trees of depth 5 (due to pre-pruning).
        self._assert_checkpoint(est.model_dir,
                                global_step=21,
                                finalized_trees=0,
                                attempted_layers=21)
        eval_res = est.evaluate(input_fn=input_fn, steps=1)
        self.assertAllClose(eval_res['average_loss'], 3.83943)
  def testInferEstimator(self):
    train_input_fn = _make_train_input_fn(is_classification=False)
    predict_input_fn = numpy_io.numpy_input_fn(
        x=FEATURES_DICT, y=None, batch_size=1, num_epochs=1, shuffle=False)

    est = boosted_trees._BoostedTreesEstimator(
        feature_columns=self._feature_columns,
        n_batches_per_layer=1,
        n_trees=1,
        max_depth=5,
        head=self._head)

    # It will stop after 5 steps because of the max depth and num trees.
    num_steps = 100
    # Train for a few steps, and validate final checkpoint.
    est.train(train_input_fn, steps=num_steps)
    self._assert_checkpoint(
        est.model_dir, global_step=5, finalized_trees=1, attempted_layers=5)
    # Validate predictions.
    predictions = list(est.predict(input_fn=predict_input_fn))
    self.assertAllClose(
        [[0.571619], [0.262821], [0.124549], [0.956801], [1.769801]],
        [pred['predictions'] for pred in predictions])
    def testContribEstimatorThatDFCIsInPredictions(self):
        # pylint:disable=protected-access
        head = canned_boosted_trees._create_regression_head(label_dimension=1)
        train_input_fn = _make_train_input_fn(is_classification=False)
        predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT,
                                                   y=None,
                                                   batch_size=1,
                                                   num_epochs=1,
                                                   shuffle=False)

        est = boosted_trees._BoostedTreesEstimator(
            feature_columns=self._feature_columns,
            n_batches_per_layer=1,
            head=head,
            n_trees=1,
            max_depth=5,
            center_bias=True)
        # pylint:enable=protected-access

        num_steps = 100
        # Train for a few steps. Validate debug outputs in prediction dicts.
        est.train(train_input_fn, steps=num_steps)
        debug_predictions = est.experimental_predict_with_explanations(
            predict_input_fn)
        biases, dfcs = zip(*[(pred['bias'], pred['dfc'])
                             for pred in debug_predictions])
        self.assertAllClose([1.8] * 5, biases)
        self.assertAllClose(({
            0: -0.070499420166015625,
            1: -0.095000028610229492,
            2: 0.0
        }, {
            0: -0.53763031959533691,
            1: 0.063333392143249512,
            2: 0.0
        }, {
            0: -0.51756942272186279,
            1: -0.095000028610229492,
            2: 0.0
        }, {
            0: 0.1563495397567749,
            1: 0.063333392143249512,
            2: 0.0
        }, {
            0: 0.96934974193572998,
            1: 0.063333392143249512,
            2: 0.0
        }), dfcs)

        # Assert sum(dfcs) + bias == predictions.
        expected_predictions = [[1.6345005], [1.32570302], [1.1874305],
                                [2.01968288], [2.83268309]]
        predictions = [[sum(dfc.values()) + bias]
                       for (dfc, bias) in zip(dfcs, biases)]
        self.assertAllClose(expected_predictions, predictions)

        # Test when user doesn't include bias or dfc in predict_keys.
        debug_predictions = est.experimental_predict_with_explanations(
            predict_input_fn, predict_keys=['predictions'])
        for prediction_dict in debug_predictions:
            self.assertTrue('bias' in prediction_dict)
            self.assertTrue('dfc' in prediction_dict)
            self.assertTrue('predictions' in prediction_dict)
            self.assertEqual(len(prediction_dict), 3)
Esempio n. 16
0
  def testContribEstimatorThatDFCIsInPredictions(self):
    # pylint:disable=protected-access
    head = canned_boosted_trees._create_regression_head(label_dimension=1)
    train_input_fn = _make_train_input_fn(is_classification=False)
    predict_input_fn = numpy_io.numpy_input_fn(
        x=FEATURES_DICT, y=None, batch_size=1, num_epochs=1, shuffle=False)

    est = boosted_trees._BoostedTreesEstimator(
        feature_columns=self._feature_columns,
        n_batches_per_layer=1,
        head=head,
        n_trees=1,
        max_depth=5,
        center_bias=True)
    # pylint:enable=protected-access

    num_steps = 100
    # Train for a few steps. Validate debug outputs in prediction dicts.
    est.train(train_input_fn, steps=num_steps)
    debug_predictions = est.experimental_predict_with_explanations(
        predict_input_fn)
    biases, dfcs = zip(*[(pred['bias'], pred['dfc'])
                         for pred in debug_predictions])
    self.assertAllClose([1.8] * 5, biases)
    self.assertAllClose(({
        0: -0.070499420166015625,
        1: -0.095000028610229492,
        2: 0.0
    }, {
        0: -0.53763031959533691,
        1: 0.063333392143249512,
        2: 0.0
    }, {
        0: -0.51756942272186279,
        1: -0.095000028610229492,
        2: 0.0
    }, {
        0: 0.1563495397567749,
        1: 0.063333392143249512,
        2: 0.0
    }, {
        0: 0.96934974193572998,
        1: 0.063333392143249512,
        2: 0.0
    }), dfcs)

    # Assert sum(dfcs) + bias == predictions.
    expected_predictions = [[1.6345005], [1.32570302], [1.1874305],
                            [2.01968288], [2.83268309]]
    predictions = [
        [sum(dfc.values()) + bias] for (dfc, bias) in zip(dfcs, biases)
    ]
    self.assertAllClose(expected_predictions, predictions)

    # Test when user doesn't include bias or dfc in predict_keys.
    debug_predictions = est.experimental_predict_with_explanations(
        predict_input_fn, predict_keys=['predictions'])
    for prediction_dict in debug_predictions:
      self.assertTrue('bias' in prediction_dict)
      self.assertTrue('dfc' in prediction_dict)
      self.assertTrue('predictions' in prediction_dict)
      self.assertEqual(len(prediction_dict), 3)