|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | + |
| 7 | +from ...common._registration import register_converter |
| 8 | + |
| 9 | +_LINK_FUNCTION_TO_POST_TRANSFORM = { |
| 10 | + 'identity': 'NONE', |
| 11 | + 'logit': 'LOGISTIC', |
| 12 | + 'ologit': 'LOGISTIC' |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +def _get_post_transform(params): |
| 17 | + link_function = params["link_function"] |
| 18 | + family = params["family"] |
| 19 | + if family == "multinomial": |
| 20 | + return 'SOFTMAX' |
| 21 | + elif link_function not in _LINK_FUNCTION_TO_POST_TRANSFORM.keys(): |
| 22 | + raise ValueError("Link function %s not supported." % link_function) |
| 23 | + else: |
| 24 | + return _LINK_FUNCTION_TO_POST_TRANSFORM[link_function] |
| 25 | + |
| 26 | + |
| 27 | +def _get_default_tree_attribute_pairs(is_classifier, params): |
| 28 | + attrs = { |
| 29 | + 'post_transform': _get_post_transform(params) |
| 30 | + } |
| 31 | + nclasses = params["nclasses"] |
| 32 | + if is_classifier: |
| 33 | + predicted_classes = nclasses if nclasses > 2 else 1 |
| 34 | + attrs['base_values'] = [params["base_score"] for _ in range(0, predicted_classes)] |
| 35 | + else: |
| 36 | + attrs['n_targets'] = 1 |
| 37 | + attrs['base_values'] = [params["base_score"]] |
| 38 | + for k in {'nodes_treeids', 'nodes_nodeids', |
| 39 | + 'nodes_featureids', 'nodes_modes', 'nodes_values', |
| 40 | + 'nodes_truenodeids', 'nodes_falsenodeids', 'nodes_missing_value_tracks_true'}: |
| 41 | + attrs[k] = [] |
| 42 | + node_attr_prefix = _node_attr_prefix(is_classifier) |
| 43 | + for k in {'_treeids', '_nodeids', '_ids', '_weights'}: |
| 44 | + attrs[node_attr_prefix + k] = [] |
| 45 | + return attrs |
| 46 | + |
| 47 | + |
| 48 | +def _add_node( |
| 49 | + attr_pairs, is_classifier, tree_id, node_id, |
| 50 | + feature_id, mode, value, true_child_id, false_child_id, weights, |
| 51 | + missing |
| 52 | +): |
| 53 | + attr_pairs['nodes_treeids'].append(tree_id) |
| 54 | + attr_pairs['nodes_nodeids'].append(node_id) |
| 55 | + attr_pairs['nodes_featureids'].append(feature_id) |
| 56 | + attr_pairs['nodes_modes'].append(mode) |
| 57 | + attr_pairs['nodes_values'].append(float(value)) |
| 58 | + attr_pairs['nodes_truenodeids'].append(true_child_id) |
| 59 | + attr_pairs['nodes_falsenodeids'].append(false_child_id) |
| 60 | + attr_pairs['nodes_missing_value_tracks_true'].append(missing) |
| 61 | + if mode == 'LEAF': |
| 62 | + node_attr_prefix = _node_attr_prefix(is_classifier) |
| 63 | + for i, w in enumerate(weights): |
| 64 | + attr_pairs[node_attr_prefix + '_treeids'].append(tree_id) |
| 65 | + attr_pairs[node_attr_prefix + '_nodeids'].append(node_id) |
| 66 | + attr_pairs[node_attr_prefix + '_ids'].append(i) |
| 67 | + attr_pairs[node_attr_prefix + '_weights'].append(float(w)) |
| 68 | + |
| 69 | + |
| 70 | +def _node_attr_prefix(is_classifier): |
| 71 | + return "class" if is_classifier else "target" |
| 72 | + |
| 73 | + |
| 74 | +def _fill_node_attributes(tree_id, node, attr_pairs, is_classifier): |
| 75 | + if 'leftChild' in node: |
| 76 | + if node["isCategorical"]: |
| 77 | + raise ValueError("categorical splits not supported, use one_hot_explicit") |
| 78 | + else: |
| 79 | + operator = 'BRANCH_GTE' |
| 80 | + value = node['splitValue'] |
| 81 | + _add_node( |
| 82 | + attr_pairs=attr_pairs, |
| 83 | + is_classifier=is_classifier, |
| 84 | + tree_id=tree_id, |
| 85 | + mode=operator, |
| 86 | + value=value, |
| 87 | + node_id=node['id'], |
| 88 | + feature_id=node['colId'], |
| 89 | + true_child_id=node['rightChild']['id'], |
| 90 | + false_child_id=node['leftChild']['id'], |
| 91 | + weights=None, |
| 92 | + missing=(0 if node["leftward"] else 1), |
| 93 | + ) |
| 94 | + _fill_node_attributes(tree_id, node["leftChild"], attr_pairs, is_classifier) |
| 95 | + _fill_node_attributes(tree_id, node["rightChild"], attr_pairs, is_classifier) |
| 96 | + else: # leaf |
| 97 | + weights = [node['predValue']] |
| 98 | + _add_node( |
| 99 | + attr_pairs=attr_pairs, |
| 100 | + is_classifier=is_classifier, |
| 101 | + tree_id=tree_id, |
| 102 | + value=0., |
| 103 | + node_id=node['id'], |
| 104 | + feature_id=0, mode='LEAF', |
| 105 | + true_child_id=0, false_child_id=0, |
| 106 | + weights=weights, |
| 107 | + missing=False |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +def assign_node_ids(node, next_id): |
| 112 | + if node is None: |
| 113 | + return next_id |
| 114 | + node["id"] = next_id |
| 115 | + next_id += 1 |
| 116 | + next_id = assign_node_ids(node.get("leftChild", None), next_id) |
| 117 | + return assign_node_ids(node.get("rightChild", None), next_id) |
| 118 | + |
| 119 | + |
| 120 | +def fill_tree_attributes(model, attr_pairs, node_attr_prefix): |
| 121 | + for tree in model["trees"]: |
| 122 | + assign_node_ids(tree["root"], 0) |
| 123 | + _fill_node_attributes(tree["index"], tree["root"], attr_pairs, node_attr_prefix) |
| 124 | + |
| 125 | + |
| 126 | +def convert_regression(scope, operator, container, params): |
| 127 | + model = operator.raw_operator |
| 128 | + |
| 129 | + attr_pairs = _get_default_tree_attribute_pairs(False, params) |
| 130 | + fill_tree_attributes(model, attr_pairs, False) |
| 131 | + |
| 132 | + # add nodes |
| 133 | + container.add_node('TreeEnsembleRegressor', operator.input_full_names, |
| 134 | + operator.output_full_names, op_domain='ai.onnx.ml', |
| 135 | + name=scope.get_unique_operator_name('TreeEnsembleRegressor'), **attr_pairs) |
| 136 | + |
| 137 | + |
| 138 | +def convert_classifier(scope, operator, container, params): |
| 139 | + if params["family"] == "multinomial" and params["nclasses"] == 2: |
| 140 | + raise ValueError("Multinomial distribution with two classes not supported, use binomial distribution.") |
| 141 | + model = operator.raw_operator |
| 142 | + |
| 143 | + attr_pairs = _get_default_tree_attribute_pairs(True, params) |
| 144 | + fill_tree_attributes(model, attr_pairs, True) |
| 145 | + |
| 146 | + n_trees_in_group = params["n_trees_in_group"] |
| 147 | + attr_pairs['class_ids'] = [v % n_trees_in_group for v in attr_pairs['class_treeids']] |
| 148 | + attr_pairs['classlabels_strings'] = params["class_labels"] |
| 149 | + |
| 150 | + container.add_node('TreeEnsembleClassifier', operator.input_full_names, |
| 151 | + operator.output_full_names, |
| 152 | + op_domain='ai.onnx.ml', |
| 153 | + name=scope.get_unique_operator_name('TreeEnsembleClassifier'), |
| 154 | + **attr_pairs) |
| 155 | + |
| 156 | + |
| 157 | +def convert_h2o(scope, operator, container): |
| 158 | + params = operator.raw_operator["params"] |
| 159 | + is_classifier = params["classifier"] |
| 160 | + if is_classifier: |
| 161 | + convert_classifier(scope, operator, container, params) |
| 162 | + else: |
| 163 | + convert_regression(scope, operator, container, params) |
| 164 | + |
| 165 | + |
| 166 | +register_converter('H2OTreeMojo', convert_h2o) |
0 commit comments