Skip to content

Commit

Permalink
Fixed Adaboost converter's incorrect results with non-default learnin…
Browse files Browse the repository at this point in the history
…g rate (#223)
  • Loading branch information
Prabhat committed Jul 18, 2019
1 parent 5a065b7 commit 2a9399f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions skl2onnx/operator_converters/ada_boost.py
Expand Up @@ -160,7 +160,7 @@ def convert_sklearn_ada_boost_classifier(scope, operator, container):
attrs['classlabels_strings'] = classes

add_tree_to_attribute_pairs(attrs, True, op.estimators_[tree_id].tree_,
0, op.learning_rate, 0, True)
0, 1, 0, True)
container.add_node(
op_type, operator.input_full_names,
[label_name, proba_name],
Expand Down Expand Up @@ -234,7 +234,7 @@ def _get_estimators_label(scope, operator, container, model):
attrs['n_targets'] = int(model.estimators_[tree_id].n_outputs_)
add_tree_to_attribute_pairs(attrs, False,
model.estimators_[tree_id].tree_,
0, model.learning_rate, 0, False)
0, 1, 0, False)

container.add_node(op_type, input_name,
estimator_label_name, op_domain='ai.onnx.ml',
Expand Down
47 changes: 47 additions & 0 deletions tests/test_sklearn_adaboost_converter.py
Expand Up @@ -69,6 +69,34 @@ def test_ada_boost_classifier_samme(self):
"<= StrictVersion('0.2.1')",
)

@unittest.skipIf(not onnx_built_with_ml(),
reason="Requires ONNX-ML extension.")
def test_ada_boost_classifier_lr(self):
data = load_digits()
X, y = data.data, data.target
X = X.astype('int64')
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.2,
random_state=42)
model = AdaBoostClassifier(learning_rate=0.3, random_state=42)
model.fit(X_train, y_train)
model_onnx = convert_sklearn(
model,
"AdaBoost classification",
[("input", Int64TensorType(X_test.shape))],
)
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X_test,
model,
model_onnx,
basename="SklearnAdaBoostClassifierLR",
allow_failure="StrictVersion("
"onnxruntime.__version__)"
"<= StrictVersion('0.2.1')",
)

def test_ada_boost_regressor(self):
model, X = fit_regression_model(
AdaBoostRegressor(n_estimators=5))
Expand Down Expand Up @@ -107,6 +135,25 @@ def test_ada_boost_regressor_int(self):
"== StrictVersion('1.4.1')",
)

def test_ada_boost_regressor_lr(self):
model, X = fit_regression_model(
AdaBoostRegressor(learning_rate=0.5, random_state=42))
model_onnx = convert_sklearn(
model, "AdaBoost regression",
[("input", FloatTensorType([1, X.shape[1]]))])
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X,
model,
model_onnx,
basename="SklearnAdaBoostRegressorLR-OneOffArray-Dec4",
allow_failure="StrictVersion("
"onnxruntime.__version__) "
"<= StrictVersion('0.2.1') or "
"StrictVersion(onnx.__version__) "
"== StrictVersion('1.4.1')",
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 2a9399f

Please sign in to comment.