Beispiel #1
0
    def testAdditionalOutputs(self):
        """Tests multi-class classification using matrix data as input."""
        hparams = tensor_forest.ForestHParams(num_trees=1,
                                              max_nodes=100,
                                              num_classes=3,
                                              num_features=4,
                                              split_after_samples=20,
                                              inference_tree_paths=True)
        classifier = random_forest.CoreTensorForestEstimator(
            hparams.fill(), keys_column='keys', include_all_in_serving=True)

        iris = base.load_iris()
        data = iris.data.astype(np.float32)
        labels = iris.target.astype(np.int32)

        input_fn = numpy_io.numpy_input_fn(x={
            'x':
            data,
            'keys':
            np.arange(len(iris.data)).reshape(150, 1)
        },
                                           y=labels,
                                           batch_size=10,
                                           num_epochs=1,
                                           shuffle=False)

        classifier.train(input_fn=input_fn, steps=100)
        predictions = list(classifier.predict(input_fn=input_fn))
        # Check that there is a key column, tree paths and var.
        for pred in predictions:
            self.assertTrue('keys' in pred)
            self.assertTrue('tree_paths' in pred)
            self.assertTrue('prediction_variance' in pred)
Beispiel #2
0
    def testWithFeatureColumns(self):
        head_fn = head_lib._multi_class_head_with_softmax_cross_entropy_loss(
            n_classes=3,
            loss_reduction=losses.Reduction.SUM_OVER_NONZERO_WEIGHTS)

        hparams = tensor_forest.ForestHParams(num_trees=3,
                                              max_nodes=1000,
                                              num_classes=3,
                                              num_features=4,
                                              split_after_samples=20,
                                              inference_tree_paths=True)

        est = random_forest.CoreTensorForestEstimator(
            hparams.fill(),
            head=head_fn,
            feature_columns=[core_feature_column.numeric_column('x')])

        iris = base.load_iris()
        data = {'x': iris.data.astype(np.float32)}
        labels = iris.target.astype(np.int32)

        input_fn = numpy_io.numpy_input_fn(x=data,
                                           y=labels,
                                           batch_size=150,
                                           num_epochs=None,
                                           shuffle=False)

        est.train(input_fn=input_fn, steps=100)
        res = est.evaluate(input_fn=input_fn, steps=1)

        self.assertEqual(1.0, res['accuracy'])
        self.assertAllClose(0.55144483, res['loss'])
Beispiel #3
0
    def testRegression(self):
        """Tests regression using matrix data as input."""
        head_fn = head_lib._regression_head(
            label_dimension=1,
            loss_reduction=losses.Reduction.SUM_OVER_NONZERO_WEIGHTS)

        hparams = tensor_forest.ForestHParams(num_trees=5,
                                              max_nodes=1000,
                                              num_classes=1,
                                              num_features=13,
                                              regression=True,
                                              split_after_samples=20)

        regressor = random_forest.CoreTensorForestEstimator(hparams.fill(),
                                                            head=head_fn)

        input_fn, predict_input_fn = _get_regression_input_fns()

        regressor.train(input_fn=input_fn, steps=100)
        res = regressor.evaluate(input_fn=input_fn, steps=10)
        self.assertGreaterEqual(0.1, res['loss'])

        predictions = list(regressor.predict(input_fn=predict_input_fn))
        self.assertAllClose([[24.]],
                            [pred['predictions'] for pred in predictions],
                            atol=1)
Beispiel #4
0
    def testTrainEvaluateInferDoesNotThrowErrorForClassifier(self):
        head_fn = head_lib._multi_class_head_with_softmax_cross_entropy_loss(
            n_classes=3,
            loss_reduction=losses.Reduction.SUM_OVER_NONZERO_WEIGHTS)

        hparams = tensor_forest.ForestHParams(num_trees=3,
                                              max_nodes=1000,
                                              num_classes=3,
                                              num_features=4,
                                              split_after_samples=20,
                                              inference_tree_paths=True)

        est = random_forest.CoreTensorForestEstimator(hparams.fill(),
                                                      head=head_fn)

        input_fn, predict_input_fn = _get_classification_input_fns()

        est.train(input_fn=input_fn, steps=100)
        res = est.evaluate(input_fn=input_fn, steps=1)

        self.assertEqual(1.0, res['accuracy'])
        self.assertAllClose(0.55144483, res['loss'])

        predictions = list(est.predict(input_fn=predict_input_fn))
        self.assertAllClose([[0.576117, 0.211942, 0.211942]],
                            [pred['probabilities'] for pred in predictions])
Beispiel #5
0
    def testAutofillsClassificationHead(self):
        hparams = tensor_forest.ForestHParams(num_trees=3,
                                              max_nodes=1000,
                                              num_classes=3,
                                              num_features=4,
                                              split_after_samples=20,
                                              inference_tree_paths=True)

        est = random_forest.CoreTensorForestEstimator(hparams.fill())

        input_fn, _ = _get_classification_input_fns()

        est.train(input_fn=input_fn, steps=100)
        res = est.evaluate(input_fn=input_fn, steps=1)

        self.assertEqual(1.0, res['accuracy'])
        self.assertAllClose(0.55144483, res['loss'])
Beispiel #6
0
    def testAutofillsRegressionHead(self):
        hparams = tensor_forest.ForestHParams(num_trees=5,
                                              max_nodes=1000,
                                              num_classes=1,
                                              num_features=13,
                                              regression=True,
                                              split_after_samples=20)

        regressor = random_forest.CoreTensorForestEstimator(hparams.fill())

        input_fn, predict_input_fn = _get_regression_input_fns()

        regressor.train(input_fn=input_fn, steps=100)
        res = regressor.evaluate(input_fn=input_fn, steps=10)
        self.assertGreaterEqual(0.1, res['loss'])

        predictions = list(regressor.predict(input_fn=predict_input_fn))
        self.assertAllClose([[24.]],
                            [pred['predictions'] for pred in predictions],
                            atol=1)
Beispiel #7
0
    def testEarlyStopping(self):
        head_fn = head_lib._multi_class_head_with_softmax_cross_entropy_loss(
            n_classes=3,
            loss_reduction=losses.Reduction.SUM_OVER_NONZERO_WEIGHTS)

        hparams = tensor_forest.ForestHParams(num_trees=3,
                                              max_nodes=1000,
                                              num_classes=3,
                                              num_features=4,
                                              split_after_samples=20,
                                              inference_tree_paths=True)

        est = random_forest.CoreTensorForestEstimator(
            hparams.fill(),
            head=head_fn,
            # Set a crazy threshold - 30% loss change.
            early_stopping_loss_threshold=0.3,
            early_stopping_rounds=2)

        input_fn, _ = _get_classification_input_fns()
        est.train(input_fn=input_fn, steps=100)
        # We stopped early.
        self._assert_checkpoint(est.model_dir, global_step=8)