def testTrainClassifierWithIntegerLabel(self): def _input_fn_with_integer_label(): return (FEATURES_DICT, constant_op.constant([[0], [1], [1], [0], [0]], dtypes.int32)) predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT, y=None, batch_size=1, num_epochs=1, shuffle=False) est = boosted_trees.BoostedTreesClassifier( feature_columns=self._feature_columns, n_batches_per_layer=1, n_trees=1, max_depth=5) est.train(input_fn=_input_fn_with_integer_label, steps=5) self._assert_checkpoint(est.model_dir, global_step=5, finalized_trees=1, attempted_layers=5) eval_res = est.evaluate(input_fn=_input_fn_with_integer_label, 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])
def testInferBinaryClassifier(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.BoostedTreesClassifier( feature_columns=self._feature_columns, n_batches_per_layer=1, n_trees=1, max_depth=5) # 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) predictions = list(est.predict(input_fn=predict_input_fn)) self.assertAllClose([[0], [1], [1], [0], [0]], [pred['class_ids'] for pred in predictions])
def testTrainClassifierWithLabelVocabulary(self): apple, banana = 'apple', 'banana' def _input_fn_with_label_vocab(): return FEATURES_DICT, [[apple], [banana], [banana], [apple], [apple]] predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT, y=None, batch_size=1, num_epochs=1, shuffle=False) est = boosted_trees.BoostedTreesClassifier( feature_columns=self._feature_columns, n_batches_per_layer=1, n_trees=1, max_depth=5, label_vocabulary=[apple, banana]) est.train(input_fn=_input_fn_with_label_vocab, steps=5) self._assert_checkpoint(est.model_dir, global_step=5, finalized_trees=1, attempted_layers=5) eval_res = est.evaluate(input_fn=_input_fn_with_label_vocab, 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])
def testInferBinaryClassifier(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.BoostedTreesClassifier( feature_columns=self._feature_columns, n_batches_per_layer=1, n_trees=1, max_depth=5) # 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) predictions = list(est.predict(input_fn=predict_input_fn)) self.assertEquals(5, len(predictions)) # All labels are correct. 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'])
def testTrainAndEvaluateBinaryClassifier(self): input_fn = _make_train_input_fn(is_classification=True) est = boosted_trees.BoostedTreesClassifier( feature_columns=self._feature_columns, n_batches_per_layer=1, n_trees=1, max_depth=5) # 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(input_fn, steps=num_steps) self._assert_checkpoint(est.model_dir, 6) eval_res = est.evaluate(input_fn=input_fn, steps=1) self.assertAllClose(eval_res['accuracy'], 1.0)
def testTrainClassifierWithDataset(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.BoostedTreesClassifier( feature_columns=self._feature_columns, n_batches_per_layer=1, n_trees=1, max_depth=5) est.train(train_input_fn, steps=100) # will stop after 5 steps anyway. self._assert_checkpoint( est.model_dir, global_step=5, finalized_trees=1, attempted_layers=5) 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])
def testTrainClassifierWithRankOneLabel(self): """Tests that label with rank-1 tensor is also accepted by classifier.""" def _input_fn_with_rank_one_label(): return FEATURES_DICT, [0., 1., 1., 0., 0.] est = boosted_trees.BoostedTreesClassifier( feature_columns=self._feature_columns, n_batches_per_layer=1, n_trees=1, max_depth=5) # 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(_input_fn_with_rank_one_label, steps=num_steps) self._assert_checkpoint(est.model_dir, global_step=5, finalized_trees=1, attempted_layers=5) eval_res = est.evaluate(input_fn=_input_fn_with_rank_one_label, steps=1) self.assertAllClose(eval_res['accuracy'], 1.0)
def testFirstCheckpointWorksFine(self): """Tests that eval/pred doesn't crash with the very first checkpoint. The step-0 checkpoint will have only an empty ensemble, and a separate eval job might read from it and crash. This test ensures that prediction/evaluation works fine with it. """ 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.BoostedTreesClassifier( feature_columns=self._feature_columns, n_batches_per_layer=1, n_trees=1, max_depth=5) class BailOutWithoutTraining(session_run_hook.SessionRunHook): def before_run(self, run_context): raise StopIteration('to bail out.') est.train( input_fn, steps=100, # must stop at 0 anyway. hooks=[BailOutWithoutTraining()]) self._assert_checkpoint(est.model_dir, global_step=0, finalized_trees=0, attempted_layers=0) # Empty ensemble returns 0 logits, so that all output labels are 0. eval_res = est.evaluate(input_fn=input_fn, steps=1) self.assertAllClose(eval_res['accuracy'], 0.6) predictions = list(est.predict(input_fn=predict_input_fn)) self.assertAllClose([[0], [0], [0], [0], [0]], [pred['class_ids'] for pred in predictions])