def main(args): learner_config = learner_pb2.LearnerConfig() learner_config.learning_rate_tuner.fixed.learning_rate = args.learning_rate learner_config.num_classes = args.num_classes learner_config.regularization.l1 = 0.0 learner_config.regularization.l2 = args.l2 / args.examples_per_layer learner_config.constraints.max_tree_depth = args.depth learner_config.growing_mode = learner_pb2.LearnerConfig.LAYER_BY_LAYER learner_config.multi_class_strategy = learner_pb2.LearnerConfig.DIAGONAL_HESSIAN head = custom_loss_head.CustomLossHead( loss_fn=functools.partial(losses.per_example_maxent_loss, num_classes=args.num_classes), link_fn=tf.identity, logit_dimension=args.num_classes, metrics_fn=_multiclass_metrics) estimator = GradientBoostedDecisionTreeEstimator( learner_config=learner_config, head=head, examples_per_layer=args.examples_per_layer, num_trees=args.num_trees, center_bias=False) (X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data() X_train = (X_train/255.).reshape(-1, 28*28).astype(np.float32) X_test = (X_test/255.).reshape(-1, 28*28).astype(np.float32) y_train = y_train.astype(np.int32) y_test = y_test.astype(np.int32) estimator.fit(input_fn=tf.estimator.inputs.numpy_input_fn( x={'_':X_train}, y=y_train, batch_size=args.batch_size, num_epochs=10, shuffle=True)) estimator.evaluate(input_fn=tf.estimator.inputs.numpy_input_fn( x={'_':X_test}, y=y_test, batch_size=args.batch_size, shuffle=False))
def _quantile_regression_head(quantile): # Use quantile regression. head = custom_loss_head.CustomLossHead( loss_fn=functools.partial( losses.per_example_quantile_regression_loss, quantile=quantile), link_fn=array_ops.identity, logit_dimension=label_dimension) return head
def _get_tfbt(output_dir): """Configures TF Boosted Trees estimator based on flags.""" learner_config = learner_pb2.LearnerConfig() num_classes = 10 learner_config.learning_rate_tuner.fixed.learning_rate = FLAGS.learning_rate learner_config.num_classes = num_classes learner_config.regularization.l1 = 0.0 learner_config.regularization.l2 = FLAGS.l2 / FLAGS.examples_per_layer learner_config.constraints.max_tree_depth = FLAGS.depth growing_mode = learner_pb2.LearnerConfig.LAYER_BY_LAYER learner_config.growing_mode = growing_mode run_config = tf.contrib.learn.RunConfig(save_checkpoints_secs=300) # Use Cross Entropy loss (the impl in losses is twice differentiable). loss_fn = functools.partial(losses.per_example_maxent_loss, num_classes=num_classes) logit_dim = num_classes learner_config.multi_class_strategy = ( learner_pb2.LearnerConfig.DIAGONAL_HESSIAN) # Since we use custom head, we need to tell how accuracy is calculated. def _multiclass_metrics(predictions, labels, weights): """Prepares eval metrics for multiclass eval.""" metrics = dict() logits = predictions["scores"] classes = math_ops.argmax(logits, 1) metrics["accuracy"] = metrics_lib.streaming_accuracy( classes, labels, weights) return metrics metrics_fn = _multiclass_metrics # Use custom loss head so we can provide our loss (cross entropy for # multiclass). head = custom_loss_head.CustomLossHead(loss_fn=loss_fn, link_fn=tf.identity, logit_dimension=logit_dim, metrics_fn=metrics_fn) # Create a TF Boosted trees estimator that can take in custom loss. estimator = GradientBoostedDecisionTreeEstimator( learner_config=learner_config, head=head, examples_per_layer=FLAGS.examples_per_layer, model_dir=output_dir, num_trees=FLAGS.num_trees, center_bias=False, config=run_config) return estimator
parser.add_argument("--num_trees", type=int, default=100) parser.add_argument("--num_classes", type=int, default=10) args = parser.parse_args() learner_config = learner_pb2.LearnerConfig() learner_config.learning_rate_tuner.fixed.learning_rate = args.learning_rate learner_config.num_classes = args.num_classes learner_config.regularization.l1 = 0.0 learner_config.regularization.l2 = args.l2 / args.examples_per_layer learner_config.constraints.max_tree_depth = args.depth learner_config.growing_mode = learner_pb2.LearnerConfig.LAYER_BY_LAYER learner_config.multi_class_strategy = learner_pb2.LearnerConfig.DIAGONAL_HESSIAN head = custom_loss_head.CustomLossHead(loss_fn=functools.partial( losses.per_example_maxent_loss, num_classes=args.num_classes), link_fn=tf.identity, logit_dimension=args.num_classes, metrics_fn=_multiclass_metrics) estimator = GradientBoostedDecisionTreeEstimator( learner_config=learner_config, head=head, examples_per_layer=args.examples_per_layer, num_trees=args.num_trees, center_bias=False) (X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data() X_train = (X_train / 255.).reshape(-1, 28 * 28).astype(np.float32) X_test = (X_test / 255.).reshape(-1, 28 * 28).astype(np.float32) y_train = y_train.astype(np.int32) y_test = y_test.astype(np.int32)