Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Perceptron classifier converter #236

Merged
merged 1 commit into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions skl2onnx/_supported_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# Linear classifiers
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import Perceptron, SGDClassifier
from sklearn.svm import LinearSVC

# Linear regressors
Expand Down Expand Up @@ -113,7 +113,7 @@
# included in the following list and one output for everything not in
# the list.
sklearn_classifier_list = [
LogisticRegression, LogisticRegressionCV, SGDClassifier,
LogisticRegression, LogisticRegressionCV, Perceptron, SGDClassifier,
LinearSVC, SVC, NuSVC,
GradientBoostingClassifier, RandomForestClassifier,
DecisionTreeClassifier,
Expand Down Expand Up @@ -171,6 +171,7 @@ def build_sklearn_operator_name_map():
LogisticRegressionCV: 'SklearnLinearClassifier',
NuSVC: 'SklearnSVC',
NuSVR: 'SklearnSVR',
Perceptron: 'SklearnSGDClassifier',
Ridge: 'SklearnLinearRegressor',
RidgeCV: 'SklearnLinearRegressor',
SGDRegressor: 'SklearnLinearRegressor',
Expand Down
104 changes: 104 additions & 0 deletions tests/test_sklearn_perceptron_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""Tests scikit-learn's Perceptron converter."""

import unittest
import numpy as np
from sklearn.linear_model import Perceptron
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType, Int64TensorType
from skl2onnx.common.data_types import onnx_built_with_ml
from test_utils import dump_data_and_model, fit_classification_model


class TestPerceptronClassifierConverter(unittest.TestCase):

@unittest.skipIf(not onnx_built_with_ml(),
reason="Requires ONNX-ML extension.")
def test_model_perceptron_binary_class(self):
model, X = fit_classification_model(
Perceptron(random_state=42), 2)
model_onnx = convert_sklearn(
model,
"scikit-learn Perceptron binary classifier",
[("input", FloatTensorType([None, X.shape[1]]))],
)
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X.astype(np.float32),
model,
model_onnx,
basename="SklearnPerceptronClassifierBinary-Out0",
allow_failure="StrictVersion(onnx.__version__)"
" < StrictVersion('1.2') or "
"StrictVersion(onnxruntime.__version__)"
" <= StrictVersion('0.2.1')",
)

@unittest.skipIf(not onnx_built_with_ml(),
reason="Requires ONNX-ML extension.")
def test_model_perceptron_multi_class(self):
model, X = fit_classification_model(
Perceptron(random_state=42), 5)
model_onnx = convert_sklearn(
model,
"scikit-learn Perceptron multi-class classifier",
[("input", FloatTensorType([None, X.shape[1]]))],
)
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X.astype(np.float32),
model,
model_onnx,
basename="SklearnPerceptronClassifierMulti-Out0",
allow_failure="StrictVersion(onnx.__version__)"
" < StrictVersion('1.2') or "
"StrictVersion(onnxruntime.__version__)"
" <= StrictVersion('0.2.1')",
)

@unittest.skipIf(not onnx_built_with_ml(),
reason="Requires ONNX-ML extension.")
def test_model_perceptron_binary_class_int(self):
model, X = fit_classification_model(
Perceptron(random_state=42), 2, is_int=True)
model_onnx = convert_sklearn(
model,
"scikit-learn Perceptron binary classifier",
[("input", Int64TensorType([None, X.shape[1]]))],
)
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X.astype(np.int64),
model,
model_onnx,
basename="SklearnPerceptronClassifierBinaryInt-Out0",
allow_failure="StrictVersion(onnx.__version__)"
" < StrictVersion('1.2') or "
"StrictVersion(onnxruntime.__version__)"
" <= StrictVersion('0.2.1')",
)

@unittest.skipIf(not onnx_built_with_ml(),
reason="Requires ONNX-ML extension.")
def test_model_perceptron_multi_class_int(self):
model, X = fit_classification_model(
Perceptron(random_state=42), 5, is_int=True)
model_onnx = convert_sklearn(
model,
"scikit-learn Perceptron multi-class classifier",
[("input", Int64TensorType([None, X.shape[1]]))],
)
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X.astype(np.int64),
model,
model_onnx,
basename="SklearnPerceptronClassifierMultiInt-Out0",
allow_failure="StrictVersion(onnx.__version__)"
" < StrictVersion('1.2') or "
"StrictVersion(onnxruntime.__version__)"
" <= StrictVersion('0.2.1')",
)


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