Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
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
2 changes: 2 additions & 0 deletions _doc/sphinxdoc/source/api/npy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Decorators

.. autosignature:: mlprodict.npy.onnx_numpy_wrapper.onnxnumpy_np

.. autosignature:: mlprodict.npy.onnx_sklearn_wrapper.onnxsklearn_regressor

.. autosignature:: mlprodict.npy.onnx_sklearn_wrapper.onnxsklearn_transformer

OnnxNumpyCompiler
Expand Down
127 changes: 127 additions & 0 deletions _unittests/ut_npy/test_custom_predictor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# -*- coding: utf-8 -*-
"""
@brief test log(time=3s)
"""
import unittest
import warnings
from logging import getLogger
import numpy
from sklearn.base import RegressorMixin, BaseEstimator
from sklearn.linear_model import LinearRegression
from pyquickhelper.pycode import ExtTestCase, ignore_warnings
from skl2onnx import update_registered_converter
from skl2onnx.algebra.onnx_ops import ( # pylint: disable=E0611
OnnxIdentity, OnnxMatMul, OnnxAdd)
from skl2onnx.common.data_types import guess_numpy_type
from mlprodict.onnx_conv import to_onnx
from mlprodict.onnxrt import OnnxInference
from mlprodict.npy import onnxsklearn_regressor


class CustomLinearRegressor(RegressorMixin, BaseEstimator):
def __init__(self):
BaseEstimator.__init__(self)
RegressorMixin.__init__(self)

def fit(self, X, y=None, sample_weights=None):
lr = LinearRegression().fit(X, y, sample_weights)
self.coef_ = lr.coef_ # pylint: disable=W0201
self.intercept_ = lr.intercept_ # pylint: disable=W0201
return self

def predict(self, X):
return X @ self.coef_ + self.intercept_


def custom_linear_regressor_shape_calculator(operator):
op = operator.raw_operator
input_type = operator.inputs[0].type.__class__
input_dim = operator.inputs[0].type.shape[0]
output_type = input_type([input_dim, op.coef_.shape[-1]])
operator.outputs[0].type = output_type


def custom_linear_regressor_converter(scope, operator, container):
op = operator.raw_operator
opv = container.target_opset
out = operator.outputs
X = operator.inputs[0]
dtype = guess_numpy_type(X.type)
m = OnnxAdd(
OnnxMatMul(X, op.coef_.astype(dtype), op_version=opv),
op.intercept_, op_version=opv)
Y = OnnxIdentity(m, op_version=opv, output_names=out[:1])
Y.add_to(scope, container)


class CustomLinearRegressor3(CustomLinearRegressor):
pass


@onnxsklearn_regressor(register_class=CustomLinearRegressor3)
def custom_linear_regressor_converter3(X, op=None):
if X.dtype is None:
raise AssertionError("X.dtype cannot be None.")
coef = op.coef_.astype(X.dtype)
intercept = op.intercept_.astype(X.dtype)
return (X @ coef) + intercept


class TestCustomTransformer(ExtTestCase):

def setUp(self):
logger = getLogger('skl2onnx')
logger.disabled = True
with warnings.catch_warnings():
warnings.simplefilter("ignore", ResourceWarning)
update_registered_converter(
CustomLinearRegressor, "SklearnCustomLinearRegressor",
custom_linear_regressor_shape_calculator,
custom_linear_regressor_converter)

@ignore_warnings((DeprecationWarning, RuntimeWarning))
def test_function_regressor(self):
X = numpy.random.randn(20, 2).astype(numpy.float32)
y = (X.sum(axis=1) + numpy.random.randn(
X.shape[0]).astype(numpy.float32))
dec = CustomLinearRegressor()
dec.fit(X, y)
onx = to_onnx(dec, X.astype(numpy.float32))
oinf = OnnxInference(onx)
exp = dec.predict(X)
got = oinf.run({'X': X})
self.assertEqualArray(exp, got['variable'])

@ignore_warnings((DeprecationWarning, RuntimeWarning))
def test_function_regressor3_float32(self):
X = numpy.random.randn(20, 2).astype(numpy.float32)
y = (X.sum(axis=1) + numpy.random.randn(
X.shape[0]).astype(numpy.float32))
dec = CustomLinearRegressor3()
dec.fit(X, y)
onx = to_onnx(dec, X.astype(numpy.float32))
oinf = OnnxInference(onx)
exp = dec.predict(X)
got = oinf.run({'X': X})
self.assertEqualArray(exp, got['variable'])
X2 = custom_linear_regressor_converter3(X, op=dec)
self.assertEqualArray(X2, got['variable'])

@ignore_warnings((DeprecationWarning, RuntimeWarning))
def test_function_regressor3_float64(self):
X = numpy.random.randn(20, 2).astype(numpy.float64)
y = (X.sum(axis=1) + numpy.random.randn(
X.shape[0]).astype(numpy.float64))
dec = CustomLinearRegressor3()
dec.fit(X, y)
onx = to_onnx(dec, X.astype(numpy.float64))
oinf = OnnxInference(onx)
exp = dec.predict(X)
got = oinf.run({'X': X})
self.assertEqualArray(exp, got['variable'])
X2 = custom_linear_regressor_converter3(X, op=dec)
self.assertEqualArray(X2, got['variable'])


if __name__ == "__main__":
unittest.main()
13 changes: 13 additions & 0 deletions _unittests/ut_npy/test_custom_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ def test_function_transformer3_float32(self):
X2 = decorrelate_transformer_converter3(X, op=dec)
self.assertEqualArray(X2, got['variable'])

@ignore_warnings((DeprecationWarning, RuntimeWarning))
def test_function_transformer3_float64(self):
X = numpy.random.randn(20, 2).astype(numpy.float64)
dec = DecorrelateTransformer3()
dec.fit(X)
onx = to_onnx(dec, X.astype(numpy.float64))
oinf = OnnxInference(onx)
exp = dec.transform(X)
got = oinf.run({'X': X})
self.assertEqualArray(exp, got['variable'])
X2 = decorrelate_transformer_converter3(X, op=dec)
self.assertEqualArray(X2, got['variable'])


if __name__ == "__main__":
unittest.main()
3 changes: 2 additions & 1 deletion mlprodict/npy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
from .onnx_numpy_compiler import OnnxNumpyCompiler
from .onnx_numpy_wrapper import onnxnumpy, onnxnumpy_default, onnxnumpy_np
from .onnx_sklearn_wrapper import (
onnxsklearn_transformer, update_registered_converter_npy)
update_registered_converter_npy,
onnxsklearn_transformer, onnxsklearn_regressor)
13 changes: 8 additions & 5 deletions mlprodict/npy/onnx_numpy_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def decorator_fct(fct):
signature=signature)
name = "onnxnumpy_%s_%s_%s" % (fct.__name__, str(op_version), runtime)
newclass = type(
name, (wrapper_onnxnumpy,), {'__doc__': fct.__doc__})
name, (wrapper_onnxnumpy,),
{'__doc__': fct.__doc__, '__name__': name})
_created_classes_inst.append(name, newclass)
return newclass(compiled)
return decorator_fct
Expand Down Expand Up @@ -188,11 +189,12 @@ def _populate(self, version):
fct=self.data["fct"], op_version=self.data["op_version"],
runtime=self.data["runtime"], signature=self.data["signature"],
version=version)
name = "onnxnumpy_np_%s_%s_%s_%s" % (
self.data["fct"].__name__, str(self.data["op_version"]),
self.data["runtime"], str(version).split('.')[-1])
newclass = type(
"onnxnumpy_np_%s_%s_%s_%s" % (
self.data["fct"].__name__, str(self.data["op_version"]),
self.data["runtime"], str(version).split('.')[-1]),
(wrapper_onnxnumpy,), {'__doc__': self.data["fct"].__doc__})
name, (wrapper_onnxnumpy,),
{'__doc__': self.data["fct"].__doc__, '__name__': name})

self.signed_compiled[version] = newclass(compiled)

Expand All @@ -218,6 +220,7 @@ def decorator_fct(fct):
newclass = type(
name, (wrapper_onnxnumpy_np,), {
'__doc__': fct.__doc__,
'__name__': name,
'__getstate__': wrapper_onnxnumpy_np.__getstate__,
'__setstate__': wrapper_onnxnumpy_np.__setstate__})
_created_classes_inst.append(name, newclass)
Expand Down
Loading