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

Fixes missing WhiteKernel with return_std=True #1163

Merged
merged 2 commits into from
Feb 4, 2025
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
12 changes: 8 additions & 4 deletions CHANGELOGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

## 1.19.0

In progress.

## 1.18.1

* Fixes missing WhiteKernel with return_std=True #1163
[#1163](https://github.com/onnx/sklearn-onnx/issues/1163)
* Fix empty column selector
[#1159](https://github.com/onnx/sklearn-onnx/issues/1159)
* Fix conversion for XGBClassifier and XGBRegressor
[#1157](https://github.com/onnx/sklearn-onnx/issues/1157)
* Test SelectKBest + StandardScaler pipeline
[#1156](https://github.com/onnx/sklearn-onnx/issues/1156)
* Fix np.NAN into np.nan,
[#1148](https://github.com/onnx/sklearn-onnx/issues/1148)

Expand Down
15 changes: 12 additions & 3 deletions skl2onnx/operator_converters/_gp_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ def convert_kernel_diag(
op_version=op_version,
)

if type(kernel) is WhiteKernel:
onnx_zeros = _zero_vector_of_size(
X, keepdims=0, dtype=dtype, op_version=op_version
)
return OnnxAdd(
onnx_zeros,
np.array([kernel.noise_level], dtype=dtype),
output_names=output_names,
op_version=op_version,
)

if type(kernel) in {RBF, ExpSineSquared, RationalQuadratic, Matern}:
onnx_zeros = _zero_vector_of_size(
X, keepdims=0, dtype=dtype, op_version=op_version
Expand Down Expand Up @@ -114,9 +125,7 @@ def convert_kernel_diag(
op_version=op_version,
)

raise RuntimeError(
"Unable to convert diag method for class {}.".format(type(kernel))
)
raise RuntimeError(f"Unable to convert diag method for class {type(kernel)}")


def py_make_float_array(cst, dtype, as_tensor=False):
Expand Down
41 changes: 41 additions & 0 deletions tests/test_issues_2025.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-License-Identifier: Apache-2.0
import unittest
from sklearn.utils._testing import ignore_warnings
from sklearn.exceptions import ConvergenceWarning


class TestInvestigate2025(unittest.TestCase):
@ignore_warnings(category=(ConvergenceWarning, FutureWarning))
def test_issue_1161_gaussian(self):
# https://github.com/onnx/sklearn-onnx/issues/1161
import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import WhiteKernel
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType

# Generate sample data
X = np.array([[1], [3], [5], [6], [7], [8], [10], [12], [14], [15]])
y = np.array([3, 2, 7, 8, 7, 6, 9, 11, 10, 12])

# Define the kernel
kernel = WhiteKernel()

# Create and train the Gaussian Process Regressor
gpr = GaussianProcessRegressor(
kernel=kernel, n_restarts_optimizer=10, alpha=1e-2
)
gpr.fit(X, y)

# Convert the trained model to ONNX format
initial_type = [("float_input", FloatTensorType([None, 1]))]
onnx_model = convert_sklearn(
gpr,
initial_types=initial_type,
options={GaussianProcessRegressor: {"return_std": True}},
)
self.assertTrue(onnx_model is not None)

Check notice

Code scanning / CodeQL

Imprecise assert Note test

assertTrue(a is not b) cannot provide an informative message. Using assertIsNot(a, b) instead will give more informative messages.


if __name__ == "__main__":
unittest.main(verbosity=2)
22 changes: 22 additions & 0 deletions tests/test_sklearn_gaussian_process_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,28 @@ def test_issue_1073_multidimension_process(self):
model.predict(vx1.astype(np.float64)).ravel(), pred[0].ravel()
)

@ignore_warnings(category=(ConvergenceWarning, FutureWarning))
def test_white_kernel_return_std(self):
X = np.array([[1], [3], [5], [6], [7], [8], [10], [12], [14], [15]])
y = np.array([3, 2, 7, 8, 7, 6, 9, 11, 10, 12])
kernel = WhiteKernel()
gpr = GaussianProcessRegressor(
kernel=kernel, n_restarts_optimizer=10, alpha=1e-2
)
gpr.fit(X, y)
initial_type = [("X", FloatTensorType([None, 1]))]
onnx_model = to_onnx(
gpr,
initial_types=initial_type,
options={GaussianProcessRegressor: {"return_std": True}},
)
self.check_outputs(
gpr,
onnx_model,
X.astype(np.float32),
predict_attributes={"return_std": True},
)


if __name__ == "__main__":
# import logging
Expand Down
Loading