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

Remove trivial Runtime error in SVM converter (2) #691

Merged
merged 4 commits into from
Jul 27, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
#
# Configuration file for the Sphinx documentation builder.

from github_link import make_linkcode_resolve
import os
import sys
import warnings
import skl2onnx
import sphinx_readable_theme

sys.path.append(os.path.abspath('exts'))
from github_link import make_linkcode_resolve


# -- Project information -----------------------------------------------------

project = 'sklearn-onnx'
copyright = '2018-2020, Microsoft'
copyright = '2018-2021, Microsoft'
author = 'Microsoft'
version = skl2onnx.__version__
release = version
Expand Down Expand Up @@ -121,13 +121,12 @@
'capture_repr': ('_repr_html_', '__repr__'),
'ignore_repr_types': r'matplotlib.text|matplotlib.axes',
'binder': {
'org': 'microsoft',
'repo': 'skl2onnx',
'org': 'onnx',
'repo': 'sklearn-onnx',
'binderhub_url': 'https://mybinder.org',
'branch': 'master',
'dependencies': os.path.abspath(
os.path.join(os.path.dirname(__file__), 'requirements.txt')),
'use_jupyter_lab': True
os.path.join(os.path.dirname(__file__), 'requirements.txt'))
},
}

Expand Down
1 change: 0 additions & 1 deletion docs/tutorial/plot_kcustom_converter_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from skl2onnx import update_registered_converter
from skl2onnx.algebra.onnx_ops import OnnxIdentity
from skl2onnx.algebra.onnx_operator import OnnxSubEstimator
from skl2onnx import to_onnx

Expand Down
2 changes: 1 addition & 1 deletion skl2onnx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
Main entry point to the converter from the *scikit-learn* to *onnx*.
"""
__version__ = "1.9.0"
__version__ = "1.9.1.dev"
__author__ = "Microsoft"
__producer__ = "skl2onnx"
__producer_version__ = __version__
Expand Down
23 changes: 20 additions & 3 deletions skl2onnx/common/_apply_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,39 @@ def apply_normalizer(scope, inputs, outputs, container,
# Normalizer only produces floats.
if norm == 'L1':
norm = scope.get_unique_variable_name('norm')
norm_abs = scope.get_unique_variable_name('norm_abs')
container.add_node(
'Abs', input, norm_abs,
name=scope.get_unique_operator_name('Abs'))

if container.target_opset < 13:
container.add_node(
'ReduceSum', input, norm, axes=[1], keepdims=1,
name=scope.get_unique_operator_name('NormalizerNorm'))
'ReduceSum', norm_abs, norm, axes=[1], keepdims=1,
name=scope.get_unique_operator_name('ReduceSum'))
else:
axis_name = scope.get_unique_variable_name('axis')
container.add_initializer(
axis_name, onnx_proto.TensorProto.INT64, [1], [1])
container.add_node(
'ReduceSum', [input, axis_name], norm, keepdims=1,
'ReduceSum', [norm_abs, axis_name], norm, keepdims=1,
name=scope.get_unique_operator_name('ReduceSum'))
apply_div( # noqa
scope, [input, norm], output, container,
operator_name=scope.get_unique_operator_name(
'NormalizerNorm'))
elif norm == 'L2':
norm = scope.get_unique_variable_name('norm')
norm2 = scope.get_unique_variable_name('norm2')
container.add_node(
'ReduceSumSquare', input, norm, axes=[1], keepdims=1,
name=scope.get_unique_operator_name('ReduceSumSquare'))
container.add_node(
'Sqrt', [norm], norm2,
name=scope.get_unique_operator_name('Sqrt'))
apply_div( # noqa
scope, [input, norm2], output, container,
operator_name=scope.get_unique_operator_name(
'NormalizerNorm'))
else:
raise NotImplementedError(
"Normalization not implemented for norm %r." % norm)
4 changes: 0 additions & 4 deletions skl2onnx/operator_converters/support_vector_machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,6 @@ def convert_sklearn_svm_classifier(
# transformed_confidences = (
# sum_of_confidences / (3 * (np.abs(sum_of_confidences) + 1)))
# return votes + transformed_confidences
if list(op.classes_) != list(range(len(op.classes_))):
raise RuntimeError(
"Classes different from first n integers are not supported "
"in SVC converter.")

cst3 = scope.get_unique_variable_name('cst3')
container.add_initializer(cst3, proto_dtype, [], [3])
Expand Down
73 changes: 49 additions & 24 deletions tests/test_sklearn_normalizer_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,77 @@
import numpy
from sklearn.preprocessing import Normalizer
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import Int64TensorType, FloatTensorType
from skl2onnx.common.data_types import (
Int64TensorType, FloatTensorType, DoubleTensorType)
from test_utils import dump_data_and_model


class TestSklearnNormalizerConverter(unittest.TestCase):
def test_model_normalizer(self):
model = Normalizer(norm="l2")
model_onnx = convert_sklearn(
model,
"scikit-learn normalizer",
[("input", Int64TensorType([None, 1]))],
)
model, "scikit-learn normalizer",
[("input", Int64TensorType([None, 1]))])
self.assertTrue(model_onnx is not None)
self.assertTrue(len(model_onnx.graph.node) == 1)

def test_model_normalizer_float(self):
def test_model_normalizer_float_l1(self):
model = Normalizer(norm="l1")
model_onnx = convert_sklearn(
model, "scikit-learn normalizer",
[("input", FloatTensorType([None, 3]))])
self.assertTrue(model_onnx is not None)
self.assertTrue(len(model_onnx.graph.node) == 1)
dump_data_and_model(
numpy.array([[1, -1, 3], [3, 1, 2]], dtype=numpy.float32),
model, model_onnx,
basename="SklearnNormalizerL1-SkipDim1")

def test_model_normalizer_float_l2(self):
model = Normalizer(norm="l2")
model_onnx = convert_sklearn(
model,
"scikit-learn normalizer",
[("input", FloatTensorType([None, 3]))],
)
model, "scikit-learn normalizer",
[("input", FloatTensorType([None, 3]))])
self.assertTrue(model_onnx is not None)
self.assertTrue(len(model_onnx.graph.node) == 1)
dump_data_and_model(
numpy.array([[1, 1, 3], [3, 1, 2]], dtype=numpy.float32),
model,
model_onnx,
basename="SklearnNormalizerL2-SkipDim1",
)
numpy.array([[1, -1, 3], [3, 1, 2]], dtype=numpy.float32),
model, model_onnx,
basename="SklearnNormalizerL2-SkipDim1")

def test_model_normalizer_double_l1(self):
model = Normalizer(norm="l1")
model_onnx = convert_sklearn(
model, "scikit-learn normalizer",
[("input", DoubleTensorType([None, 3]))])
self.assertTrue(model_onnx is not None)
dump_data_and_model(
numpy.array([[1, -1, 3], [3, 1, 2]], dtype=numpy.float64),
model, model_onnx,
basename="SklearnNormalizerL1Double-SkipDim1")

def test_model_normalizer_double_l2(self):
model = Normalizer(norm="l2")
model_onnx = convert_sklearn(
model, "scikit-learn normalizer",
[("input", DoubleTensorType([None, 3]))])
self.assertTrue(model_onnx is not None)
dump_data_and_model(
numpy.array([[1, -1, 3], [3, 1, 2]], dtype=numpy.float64),
model, model_onnx,
basename="SklearnNormalizerL2Double-SkipDim1")

def test_model_normalizer_float_noshape(self):
model = Normalizer(norm="l2")
model_onnx = convert_sklearn(
model,
"scikit-learn normalizer",
[("input", FloatTensorType([]))],
)
model, "scikit-learn normalizer",
[("input", FloatTensorType([]))])
self.assertTrue(model_onnx is not None)
self.assertTrue(len(model_onnx.graph.node) == 1)
dump_data_and_model(
numpy.array([[1, 1, 3], [3, 1, 2]], dtype=numpy.float32),
model,
model_onnx,
basename="SklearnNormalizerL2-SkipDim1",
)
numpy.array([[1, -1, 3], [3, 1, 2]], dtype=numpy.float32),
model, model_onnx,
basename="SklearnNormalizerL2NoShape-SkipDim1")


if __name__ == "__main__":
Expand Down