-
Notifications
You must be signed in to change notification settings - Fork 102
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
Investigate issue 1102 #1103
Open
xadupre
wants to merge
5
commits into
onnx:main
Choose a base branch
from
xadupre:histi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Investigate issue 1102 #1103
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import unittest | ||
import packaging.version as pv | ||
from sklearn.utils._testing import ignore_warnings | ||
from sklearn.exceptions import ConvergenceWarning | ||
from onnxruntime import __version__ as ort_version | ||
|
||
|
||
class TestInvestigate(unittest.TestCase): | ||
|
||
@unittest.skipIf( | ||
pv.Version(ort_version) < pv.Version("1.17.3"), | ||
reason="opset 19 not implemented", | ||
) | ||
@ignore_warnings(category=(ConvergenceWarning, FutureWarning)) | ||
def test_issue_1102(self): | ||
from typing import Any | ||
from sklearn.datasets import make_regression | ||
import lightgbm | ||
import numpy | ||
import onnxruntime | ||
import skl2onnx | ||
from onnx.reference import ReferenceEvaluator | ||
from onnxmltools.convert.lightgbm.operator_converters.LightGbm import ( | ||
convert_lightgbm, | ||
) | ||
from skl2onnx import update_registered_converter, to_onnx | ||
from skl2onnx.common import shape_calculator | ||
from sklearn import base, multioutput, pipeline, preprocessing | ||
|
||
def Normalizer() -> list[tuple[str, Any]]: | ||
return [ | ||
("cast64", skl2onnx.sklapi.CastTransformer(dtype=numpy.float64)), | ||
("scaler", preprocessing.StandardScaler()), | ||
("cast32", skl2onnx.sklapi.CastTransformer(dtype=numpy.float32)), | ||
] | ||
|
||
def Embedder(**kwargs: dict[str, Any]) -> list[tuple[str, Any]]: | ||
return [("basemodel", lightgbm.LGBMRegressor(**kwargs))] | ||
|
||
def BoL2EmotionV2( | ||
backbone_kwargs: dict[str, Any] | None = None, | ||
) -> base.BaseEstimator: | ||
backbone = Embedder(**(backbone_kwargs or {})) | ||
normalizer = Normalizer() | ||
model = pipeline.Pipeline([*normalizer, *backbone]) | ||
return multioutput.MultiOutputRegressor(model) | ||
|
||
model = BoL2EmotionV2() | ||
X, y = make_regression(100, n_features=4, n_targets=2) | ||
model.fit(X, y) | ||
|
||
update_registered_converter( | ||
lightgbm.LGBMRegressor, | ||
"LightGbmLGBMRegressor", | ||
shape_calculator.calculate_linear_regressor_output_shapes, | ||
convert_lightgbm, | ||
options={"split": None}, | ||
) | ||
|
||
sample = X.astype(numpy.float32) | ||
|
||
exported = to_onnx( | ||
model, | ||
X=sample, | ||
name="BoL2emotion", | ||
target_opset={"": 19, "ai.onnx.ml": 2}, | ||
) | ||
expected = model.predict(sample) | ||
|
||
ref = ReferenceEvaluator(exported) | ||
got = ref.run(None, dict(X=sample))[0] | ||
numpy.testing.assert_allclose(expected, got, 1e-4) | ||
|
||
sess = onnxruntime.InferenceSession( | ||
exported.SerializeToString(), providers=["CPUExecutionProvider"] | ||
) | ||
got = sess.run(None, dict(X=sample))[0] | ||
numpy.testing.assert_allclose(expected, got, 1e-4) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main(verbosity=2) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Module is imported with 'import' and 'import from' Note test