Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
refactor rewritten sklearn operators
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed May 18, 2020
1 parent a530fda commit 015eced
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 26 deletions.
2 changes: 1 addition & 1 deletion mlprodict/__init__.py
Expand Up @@ -4,7 +4,7 @@
@brief Ways to speed up predictions for a machine learned model.
"""

__version__ = "0.3.1097"
__version__ = "0.3.1099"
__author__ = "Xavier Dupré"


Expand Down
14 changes: 14 additions & 0 deletions mlprodict/onnx_conv/register_rewritten_converters.py
Expand Up @@ -19,10 +19,24 @@
new_convert_sklearn_random_forest_classifier,
new_convert_sklearn_random_forest_regressor,
)
from .sklconv.svm_converters import (
new_convert_sklearn_svm_classifier,
new_convert_sklearn_svm_regressor,
)


_overwritten_operators = {
#
'SklearnOneClassSVM': RegisteredConverter(
new_convert_sklearn_svm_regressor,
_converter_pool['SklearnOneClassSVM'].get_allowed_options()),
'SklearnSVR': RegisteredConverter(
new_convert_sklearn_svm_regressor,
_converter_pool['SklearnSVR'].get_allowed_options()),
'SklearnSVC': RegisteredConverter(
new_convert_sklearn_svm_classifier,
_converter_pool['SklearnSVC'].get_allowed_options()),
#
'SklearnDecisionTreeRegressor': RegisteredConverter(
new_convert_sklearn_decision_tree_regressor,
_converter_pool['SklearnDecisionTreeRegressor'].get_allowed_options()),
Expand Down
57 changes: 57 additions & 0 deletions mlprodict/onnx_conv/sklconv/svm_converters.py
@@ -0,0 +1,57 @@
"""
@file
@brief Rewrites some of the converters implemented in
:epkg:`sklearn-onnx`.
"""
import numpy
from skl2onnx.operator_converters.support_vector_machines import (
convert_sklearn_svm_regressor,
convert_sklearn_svm_classifier)


def _op_type_domain_regressor(container):
"""
Defines *op_type* and *op_domain* based on
`container.dtype`.
"""
if container.dtype == numpy.float32:
return 'SVMRegressor', 'ai.onnx.ml', 1
if container.dtype == numpy.float64:
return 'SVMRegressorDouble', 'mlprodict', 1
raise RuntimeError("Unsupported dtype {}.".format(container.dtype))


def _op_type_domain_classifier(container):
"""
Defines *op_type* and *op_domain* based on
`container.dtype`.
"""
if container.dtype == numpy.float32:
return 'SVMClassifier', 'ai.onnx.ml', 1
if container.dtype == numpy.float64:
return 'SVMClassifierDouble', 'mlprodict', 1
raise RuntimeError("Unsupported dtype {}.".format(container.dtype))


def new_convert_sklearn_svm_regressor(scope, operator, container):
"""
Rewrites the converters implemented in
:epkg:`sklearn-onnx` to support an operator supporting
doubles.
"""
op_type, op_domain, op_version = _op_type_domain_regressor(container)
convert_sklearn_svm_regressor(
scope, operator, container, op_type=op_type, op_domain=op_domain,
op_version=op_version)


def new_convert_sklearn_svm_classifier(scope, operator, container):
"""
Rewrites the converters implemented in
:epkg:`sklearn-onnx` to support an operator supporting
doubles.
"""
op_type, op_domain, op_version = _op_type_domain_classifier(container)
convert_sklearn_svm_classifier(
scope, operator, container, op_type=op_type, op_domain=op_domain,
op_version=op_version)
64 changes: 41 additions & 23 deletions mlprodict/onnx_conv/sklconv/tree_converters.py
Expand Up @@ -15,38 +15,52 @@
convert_sklearn_random_forest_regressor_converter)


def _op_type_domain(container):
def _op_type_domain_regressor(container):
"""
Defines *op_type* and *op_domain* based on
`container.dtype`.
"""
if container.dtype == numpy.float32:
return 'TreeEnsembleRegressor', 'ai.onnx.ml'
return 'TreeEnsembleRegressor', 'ai.onnx.ml', 1
if container.dtype == numpy.float64:
return 'TreeEnsembleRegressorDouble', 'mlprodict'
return 'TreeEnsembleRegressorDouble', 'mlprodict', 1
raise RuntimeError("Unsupported dtype {}.".format(container.dtype))


def new_convert_sklearn_decision_tree_regressor(scope, operator, container):
def _op_type_domain_classifier(container):
"""
Defines *op_type* and *op_domain* based on
`container.dtype`.
"""
if container.dtype == numpy.float32:
return 'TreeEnsembleClassifier', 'ai.onnx.ml', 1
if container.dtype == numpy.float64:
return 'TreeEnsembleClassifierDouble', 'mlprodict', 1
raise RuntimeError("Unsupported dtype {}.".format(container.dtype))


def new_convert_sklearn_decision_tree_classifier(scope, operator, container):
"""
Rewrites the converters implemented in
:epkg:`sklearn-onnx` to support an operator supporting
doubles.
"""
op_type, op_domain = _op_type_domain(container)
convert_sklearn_decision_tree_regressor(
scope, operator, container, op_type=op_type, op_domain=op_domain)
op_type, op_domain, op_version = _op_type_domain_classifier(container)
convert_sklearn_decision_tree_classifier(
scope, operator, container, op_type=op_type, op_domain=op_domain,
op_version=op_version)


def new_convert_sklearn_decision_tree_classifier(scope, operator, container):
def new_convert_sklearn_decision_tree_regressor(scope, operator, container):
"""
Rewrites the converters implemented in
:epkg:`sklearn-onnx` to support an operator supporting
doubles.
"""
op_type, op_domain = _op_type_domain(container)
convert_sklearn_decision_tree_classifier(
scope, operator, container, op_type=op_type, op_domain=op_domain)
op_type, op_domain, op_version = _op_type_domain_regressor(container)
convert_sklearn_decision_tree_regressor(
scope, operator, container, op_type=op_type, op_domain=op_domain,
op_version=op_version)


def new_convert_sklearn_gradient_boosting_classifier(scope, operator, container):
Expand All @@ -55,9 +69,10 @@ def new_convert_sklearn_gradient_boosting_classifier(scope, operator, container)
:epkg:`sklearn-onnx` to support an operator supporting
doubles.
"""
op_type, op_domain = _op_type_domain(container)
op_type, op_domain, op_version = _op_type_domain_classifier(container)
convert_sklearn_gradient_boosting_classifier(
scope, operator, container, op_type=op_type, op_domain=op_domain)
scope, operator, container, op_type=op_type, op_domain=op_domain,
op_version=op_version)


def new_convert_sklearn_gradient_boosting_regressor(scope, operator, container):
Expand All @@ -66,28 +81,31 @@ def new_convert_sklearn_gradient_boosting_regressor(scope, operator, container):
:epkg:`sklearn-onnx` to support an operator supporting
doubles.
"""
op_type, op_domain = _op_type_domain(container)
op_type, op_domain, op_version = _op_type_domain_regressor(container)
convert_sklearn_gradient_boosting_regressor(
scope, operator, container, op_type=op_type, op_domain=op_domain)
scope, operator, container, op_type=op_type, op_domain=op_domain,
op_version=op_version)


def new_convert_sklearn_random_forest_regressor(scope, operator, container):
def new_convert_sklearn_random_forest_classifier(scope, operator, container):
"""
Rewrites the converters implemented in
:epkg:`sklearn-onnx` to support an operator supporting
doubles.
"""
op_type, op_domain = _op_type_domain(container)
convert_sklearn_random_forest_regressor_converter(
scope, operator, container, op_type=op_type, op_domain=op_domain)
op_type, op_domain, op_version = _op_type_domain_classifier(container)
convert_sklearn_random_forest_classifier(
scope, operator, container, op_type=op_type, op_domain=op_domain,
op_version=op_version)


def new_convert_sklearn_random_forest_classifier(scope, operator, container):
def new_convert_sklearn_random_forest_regressor(scope, operator, container):
"""
Rewrites the converters implemented in
:epkg:`sklearn-onnx` to support an operator supporting
doubles.
"""
op_type, op_domain = _op_type_domain(container)
convert_sklearn_random_forest_classifier(
scope, operator, container, op_type=op_type, op_domain=op_domain)
op_type, op_domain, op_version = _op_type_domain_regressor(container)
convert_sklearn_random_forest_regressor_converter(
scope, operator, container, op_type=op_type, op_domain=op_domain,
op_version=op_version)
4 changes: 2 additions & 2 deletions mlprodict/onnxrt/validate/validate_scenarios.py
Expand Up @@ -232,11 +232,11 @@ def build_custom_scenarios():
('box-cox', {'method': 'box-cox'}),
],
RandomForestClassifier: [
('default', {'n_estimators': 10},
('default', {'n_estimators': 100},
{'conv_options': [{}, {RandomForestClassifier: {'zipmap': False}}]}),
],
RandomForestRegressor: [
('default', {'n_estimators': 10}),
('default', {'n_estimators': 100}),
],
RandomizedSearchCV: [
('cl', {
Expand Down

0 comments on commit 015eced

Please sign in to comment.