refactoring of operators convert folder (#216)

This commit is contained in:
Matteo Interlandi 2020-08-03 11:22:34 -07:00 коммит произвёл GitHub
Родитель a6bedbaf2f
Коммит 58496ede96
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
22 изменённых файлов: 138 добавлений и 103 удалений

Просмотреть файл

@ -17,24 +17,24 @@ from .._utils import _Constants
constants = _Constants(converter_constants, hummingbird_constants)
# To register a converter for scikit-learn API operators, import associated modules here.
from . import lightgbm # noqa: E402
from . import onnx_operator # noqa: E402
from . import onnxml_array_feature_extractor # noqa: E402
from . import onnxml_linear # noqa: E402
from . import onnxml_normalizer # noqa: E402
from . import onnxml_one_hot_encoder # noqa: E402
from . import onnxml_scaler # noqa: E402
from . import onnxml_tree_ensemble # noqa: E402
from . import skl_array_feature_extractor # noqa: E402
from . import skl_decision_tree # noqa: E402
from . import skl_gbdt # noqa: E402
from . import skl_iforest # noqa: E402
from . import skl_linear # noqa: E402
from . import skl_normalizer # noqa: E402
from . import skl_one_hot_encoder # noqa: E402
from . import skl_scaler # noqa: E402
from . import skl_sv # noqa: E402
from . import xgb # noqa: E402
from .onnx import onnx_operator # noqa: E402
from .onnx import onnxml_array_feature_extractor # noqa: E402
from .onnx import onnxml_linear # noqa: E402
from .onnx import onnxml_normalizer # noqa: E402
from .onnx import onnxml_one_hot_encoder # noqa: E402
from .onnx import onnxml_scaler # noqa: E402
from .onnx import onnxml_tree_ensemble # noqa: E402
from .sklearn import lightgbm # noqa: E402
from .sklearn import skl_array_feature_extractor # noqa: E402
from .sklearn import skl_decision_tree # noqa: E402
from .sklearn import skl_gbdt # noqa: E402
from .sklearn import skl_iforest # noqa: E402
from .sklearn import skl_linear # noqa: E402
from .sklearn import skl_normalizer # noqa: E402
from .sklearn import skl_one_hot_encoder # noqa: E402
from .sklearn import skl_scaler # noqa: E402
from .sklearn import skl_sv # noqa: E402
from .sklearn import xgb # noqa: E402
__pdoc__ = {}

Просмотреть файл

@ -0,0 +1,9 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
All onnx operators converters are stored under this package.
"""

Просмотреть файл

@ -9,12 +9,11 @@ Converters for ONNX operators.
"""
import numpy as np
from onnxconverter_common.registration import register_converter
import torch
from onnxconverter_common.registration import register_converter
from . import constants
from ._base_operator import BaseOperator
from .. import constants
from .._base_operator import BaseOperator
class Cast(BaseOperator, torch.nn.Module):

Просмотреть файл

@ -3,12 +3,15 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import torch
"""
Converter for ONNX-ML Array Feature Extractor.
"""
from onnxconverter_common.registration import register_converter
from ._base_operator import BaseOperator
from . import constants
from ._array_feature_extractor_implementations import ArrayFeatureExtractor
from .. import constants
from .._array_feature_extractor_implementations import ArrayFeatureExtractor
def convert_onnx_array_feature_extractor(operator, device, extra_config):

Просмотреть файл

@ -11,7 +11,7 @@ Converters for ONNX-ML linear models.
import numpy as np
from onnxconverter_common.registration import register_converter
from ._linear_implementations import LinearModel
from .._linear_implementations import LinearModel
def convert_onnx_linear_model(operator, device=None, extra_config={}):

Просмотреть файл

@ -3,11 +3,14 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import torch
from onnxconverter_common.registration import register_converter
from ._base_operator import BaseOperator
from ._normalizer_implementations import Normalizer
from .._normalizer_implementations import Normalizer
"""
Converter for ONNX-ML Normalizer.
"""
def convert_onnx_normalizer(operator, device=None, extra_config={}):

Просмотреть файл

@ -3,12 +3,16 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
Converter for ONNX-ML One Hot Encoder.
"""
import numpy as np
from onnxconverter_common.registration import register_converter
import torch
from ._base_operator import BaseOperator
from ._one_hot_encoder_implementations import OneHotEncoderString, OneHotEncoder
from .._one_hot_encoder_implementations import OneHotEncoderString, OneHotEncoder
def convert_onnx_one_hot_encoder(operator, device=None, extra_config={}):

Просмотреть файл

@ -3,12 +3,16 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
Converter for ONNX-ML Scaler.
"""
import numpy as np
from onnxconverter_common.registration import register_converter
import torch
from ._base_operator import BaseOperator
from ._scaler_implementations import Scaler
from .._base_operator import BaseOperator
from .._scaler_implementations import Scaler
def convert_onnx_scaler(operator, device=None, extra_config={}):

Просмотреть файл

@ -11,9 +11,9 @@ Converters for ONNX-ML tree-ensemble models.
import numpy as np
from onnxconverter_common.registration import register_converter
from . import constants
from ._gbdt_commons import convert_gbdt_classifier_common, convert_gbdt_common
from ._tree_commons import TreeParameters
from .. import constants
from .._gbdt_commons import convert_gbdt_classifier_common, convert_gbdt_common
from .._tree_commons import TreeParameters
def _get_tree_infos_from_onnx_ml_operator(model):

Просмотреть файл

@ -0,0 +1,9 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
All scikit-learn operators converters are stored under this package.
"""

Просмотреть файл

@ -11,8 +11,8 @@ Converters for LightGBM models.
import numpy as np
from onnxconverter_common.registration import register_converter
from ._gbdt_commons import convert_gbdt_classifier_common, convert_gbdt_common
from ._tree_commons import TreeParameters
from .._gbdt_commons import convert_gbdt_classifier_common, convert_gbdt_common
from .._tree_commons import TreeParameters
def _tree_traversal(node, lefts, rights, features, thresholds, values, count):

Просмотреть файл

@ -5,15 +5,13 @@
# --------------------------------------------------------------------------
"""
Converters for scikit-learn feature selectors: SelectKBest, SelectPercentile, VarianceThreshold
Converters for scikit-learn feature selectors: SelectKBest, SelectPercentile, VarianceThreshold.
"""
import numpy as np
from onnxconverter_common.registration import register_converter
import torch
from ._array_feature_extractor_implementations import ArrayFeatureExtractor
from .._array_feature_extractor_implementations import ArrayFeatureExtractor
def convert_sklearn_select_k_best(operator, device, extra_config):

Просмотреть файл

@ -9,12 +9,10 @@ Converters for scikit-learn decision-tree-based models: DecisionTree, RandomFore
"""
import copy
import torch
from onnxconverter_common.registration import register_converter
from ._tree_commons import get_parameters_for_sklearn_common, get_parameters_for_tree_trav_sklearn
from ._tree_commons import convert_decision_ensemble_tree_common
from .._tree_commons import get_parameters_for_sklearn_common, get_parameters_for_tree_trav_sklearn
from .._tree_commons import convert_decision_ensemble_tree_common
def convert_sklearn_random_forest_classifier(operator, device, extra_config):

Просмотреть файл

@ -11,9 +11,9 @@ Converters for Sklearn's GradientBoosting models.
import numpy as np
from onnxconverter_common.registration import register_converter
from . import constants
from ._gbdt_commons import convert_gbdt_common, convert_gbdt_classifier_common
from ._tree_commons import get_parameters_for_sklearn_common, get_parameters_for_tree_trav_sklearn, TreeParameters
from .. import constants
from .._gbdt_commons import convert_gbdt_common, convert_gbdt_classifier_common
from .._tree_commons import get_parameters_for_sklearn_common, get_parameters_for_tree_trav_sklearn, TreeParameters
def _get_parameters_hist_gbdt(trees):

Просмотреть файл

@ -7,13 +7,18 @@
"""
Converters for scikit-learn isolation forest.
"""
import numpy as np
from onnxconverter_common.registration import register_converter
from . import constants
from ._tree_commons import get_parameters_for_sklearn_common, \
get_parameters_for_tree_trav_sklearn, get_tree_params_and_type, get_parameters_for_gemm_common
from ._tree_implementations import TreeImpl, GEMMTreeImpl, TreeTraversalTreeImpl, PerfectTreeTraversalTreeImpl
from .. import constants
from .._tree_commons import (
get_parameters_for_sklearn_common,
get_parameters_for_tree_trav_sklearn,
get_tree_params_and_type,
get_parameters_for_gemm_common,
)
from .._tree_implementations import TreeImpl, GEMMTreeImpl, TreeTraversalTreeImpl, PerfectTreeTraversalTreeImpl
def _average_path_length(n_samples_leaf):
@ -37,8 +42,8 @@ def _average_path_length(n_samples_leaf):
mask_2 = n_samples_leaf == 2
not_mask = ~np.logical_or(mask_1, mask_2)
average_path_length[mask_1] = 0.
average_path_length[mask_2] = 1.
average_path_length[mask_1] = 0.0
average_path_length[mask_2] = 1.0
average_path_length[not_mask] = (
2.0 * (np.log(n_samples_leaf[not_mask] - 1.0) + np.euler_gamma)
- 2.0 * (n_samples_leaf[not_mask] - 1.0) / n_samples_leaf[not_mask]
@ -81,12 +86,9 @@ def _get_parameters_for_sklearn_iforest(tree_infos):
The tree parameters wrapped into an instance of `operator_converters._tree_commons_TreeParameters`
"""
tree_parameters = get_parameters_for_sklearn_common(tree_infos)
tree_parameters.values = (
_get_iforest_anomaly_score_per_node(tree_parameters.lefts,
tree_parameters.rights,
tree_infos.tree_.n_node_samples)
.reshape(tree_parameters.values.shape)
)
tree_parameters.values = _get_iforest_anomaly_score_per_node(
tree_parameters.lefts, tree_parameters.rights, tree_infos.tree_.n_node_samples
).reshape(tree_parameters.values.shape)
return tree_parameters
@ -119,7 +121,7 @@ class GEMMIsolationForestImpl(GEMMTreeImpl):
if self.final_probability_divider > 1:
output = output / self.final_probability_divider
# Further normalize to match "decision_function" in sklearn implementation.
output = -1.0 * 2**(-output / self.average_path_length) - self.offset
output = -1.0 * 2 ** (-output / self.average_path_length) - self.offset
return output
@ -154,7 +156,7 @@ class TreeTraversalIsolationForestImpl(TreeTraversalTreeImpl):
if self.final_probability_divider > 1:
output = output / self.final_probability_divider
# Further normalize to match "decision_function" in sklearn implementation.
output = -1.0 * 2**(-output / self.average_path_length) - self.offset
output = -1.0 * 2 ** (-output / self.average_path_length) - self.offset
return output
@ -189,7 +191,7 @@ class PerfectTreeTraversalIsolationForestImpl(PerfectTreeTraversalTreeImpl):
if self.final_probability_divider > 1:
output = output / self.final_probability_divider
# Further normalize to match "decision_function" in sklearn implementation.
output = -1.0 * 2**(-output / self.average_path_length) - self.offset
output = -1.0 * 2 ** (-output / self.average_path_length) - self.offset
return output
@ -217,15 +219,15 @@ def convert_sklearn_isolation_forest(operator, device, extra_config):
# Predict in isolation forest sklearn implementation produce 2 classes: -1 (normal) & 1 (anomaly).
classes = [-1, 1]
tree_parameters, max_depth, tree_type = get_tree_params_and_type(tree_infos, _get_parameters_for_sklearn_iforest,
extra_config)
tree_parameters, max_depth, tree_type = get_tree_params_and_type(
tree_infos, _get_parameters_for_sklearn_iforest, extra_config
)
# Generate the tree implementation based on the selected strategy.
if tree_type == TreeImpl.gemm:
net_parameters = [
get_parameters_for_gemm_common(
tree_param.lefts, tree_param.rights, tree_param.features, tree_param.thresholds, tree_param.values,
n_features
tree_param.lefts, tree_param.rights, tree_param.features, tree_param.thresholds, tree_param.values, n_features
)
for tree_param in tree_parameters
]
@ -238,11 +240,11 @@ def convert_sklearn_isolation_forest(operator, device, extra_config):
for tree_param in tree_parameters
]
if tree_type == TreeImpl.tree_trav:
return TreeTraversalIsolationForestImpl(net_parameters, max_depth, n_features, classes,
extra_config=extra_config)
return TreeTraversalIsolationForestImpl(net_parameters, max_depth, n_features, classes, extra_config=extra_config)
else: # Remaining possible case: tree_type == TreeImpl.perf_tree_trav
return PerfectTreeTraversalIsolationForestImpl(net_parameters, max_depth, n_features, classes,
extra_config=extra_config)
return PerfectTreeTraversalIsolationForestImpl(
net_parameters, max_depth, n_features, classes, extra_config=extra_config
)
# Register the converters.

Просмотреть файл

@ -4,15 +4,14 @@
# license information.
# --------------------------------------------------------------------------
import torch
"""
Converters for scikit-learn linear models: LinearRegression, LogisticRegression, LinearSVC, SGDClassifier, LogisticRegressionCV.
"""
import numpy as np
from onnxconverter_common.registration import register_converter
from ._linear_implementations import LinearModel
"""
Converters for scikit-learn linear models: LinearRegression, LogisticRegression, LinearSVC, SGDClassifier, LogisticRegressionCV
"""
from .._linear_implementations import LinearModel
def convert_sklearn_linear_model(operator, device, extra_config):

Просмотреть файл

@ -3,11 +3,14 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import torch
"""
Converter for scikit-learn Normalizer.
"""
from onnxconverter_common.registration import register_converter
from ._base_operator import BaseOperator
from ._normalizer_implementations import Normalizer
from .._normalizer_implementations import Normalizer
def convert_sklearn_normalizer(operator, device, extra_config):

Просмотреть файл

@ -4,12 +4,14 @@
# license information.
# --------------------------------------------------------------------------
"""
Converter for scikit-learn one hot encoder.
"""
import numpy as np
from onnxconverter_common.registration import register_converter
import torch
from ._base_operator import BaseOperator
from ._one_hot_encoder_implementations import OneHotEncoderString, OneHotEncoder
from .._one_hot_encoder_implementations import OneHotEncoderString, OneHotEncoder
def convert_sklearn_one_hot_encoder(operator, device, extra_config):

Просмотреть файл

@ -4,12 +4,14 @@
# license information.
# --------------------------------------------------------------------------
import torch
"""
Converters for scikit-learn scalers: RobustScaler, MaxAbsScaler, MinMaxScaler, StandardScaler.
"""
import numpy as np
from onnxconverter_common.registration import register_converter
from ._base_operator import BaseOperator
from ._scaler_implementations import Scaler
from .._scaler_implementations import Scaler
def convert_sklearn_robust_scaler(operator, device, extra_config):

Просмотреть файл

@ -4,16 +4,16 @@
# license information.
# --------------------------------------------------------------------------
import torch
import scipy
"""
Converters for scikit-learn SV models: SVC, NuSVC. (LinearSVC is covered by linear_classifier.py).
"""
import numpy as np
import scipy
import torch
from onnxconverter_common.registration import register_converter
from ._base_operator import BaseOperator
"""
Converters for scikit-learn linear models: SVC, NuSVC. (LinearSVC is covered by linear_classifier.py)
"""
from .._base_operator import BaseOperator
class SVC(BaseOperator, torch.nn.Module):

Просмотреть файл

@ -11,9 +11,9 @@ Converters for XGBoost models.
import numpy as np
from onnxconverter_common.registration import register_converter
from . import constants
from ._gbdt_commons import convert_gbdt_classifier_common, convert_gbdt_common
from ._tree_commons import TreeParameters
from .. import constants
from .._gbdt_commons import convert_gbdt_classifier_common, convert_gbdt_common
from .._tree_commons import TreeParameters
def _tree_traversal(tree_info, lefts, rights, features, thresholds, values):

Просмотреть файл

@ -14,7 +14,7 @@ dt_implementation_map = {
}
iforest_implementation_map = {
"tree_trav": "<class 'hummingbird.ml.operator_converters.skl_iforest.TreeTraversalIsolationForestImpl'>",
"perf_tree_trav": "<class 'hummingbird.ml.operator_converters.skl_iforest.PerfectTreeTraversalIsolationForestImpl'>",
"gemm": "<class 'hummingbird.ml.operator_converters.skl_iforest.GEMMIsolationForestImpl'>",
"tree_trav": "<class 'hummingbird.ml.operator_converters.sklearn.skl_iforest.TreeTraversalIsolationForestImpl'>",
"perf_tree_trav": "<class 'hummingbird.ml.operator_converters.sklearn.skl_iforest.PerfectTreeTraversalIsolationForestImpl'>",
"gemm": "<class 'hummingbird.ml.operator_converters.sklearn.skl_iforest.GEMMIsolationForestImpl'>",
}