Skip to content

Commit 8e8df99

Browse files
Fix license header in new files
1 parent 6c3c435 commit 8e8df99

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

sklearnex/svm/poly_kernel.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,68 @@
77
#
88
# http://www.apache.org/licenses/LICENSE-2.0
99
#
10-
# Unless required by applicable law or agreed to in writing,
10+
# Unless required by applicable law or agreed to in writing, software
1111
# distributed under the License is distributed on an "AS IS" BASIS,
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
# ==============================================================================
1616

1717
import numpy as np
18+
from scipy import sparse
19+
from sklearn.metrics.pairwise import polynomial_kernel as sklearn_poly_kernel
1820

1921
from onedal.primitives import poly_kernel as onedal_poly_kernel
2022

2123

22-
def poly_kernel(X, Y=None, gamma=1.0, coef0=0.0, degree=3, queue=None):
24+
def poly_kernel(
25+
X,
26+
Y=None,
27+
degree=3,
28+
gamma=None,
29+
coef0=1,
30+
queue=None,
31+
):
2332
"""
24-
Sklearnex interface for the polynomial kernel using oneDAL backend.
33+
Compute the polynomial kernel using the oneDAL backend when possible.
2534
26-
K(x, y) = (gamma * <x, y> + coef0) ** degree
27-
for each pair of rows x in X and y in Y.
35+
Falls back to scikit-learn's ``polynomial_kernel`` for unsupported cases
36+
such as sparse inputs.
2837
2938
Parameters
3039
----------
3140
X : array-like of shape (n_samples_X, n_features)
3241
Input feature array.
3342
3443
Y : array-like of shape (n_samples_Y, n_features), default=None
35-
Optional second feature array. If None, Y = X.
36-
37-
gamma : float, default=1.0
38-
Scaling factor for the inner product.
39-
40-
coef0 : float, default=0.0
41-
Constant term added to scaled inner product.
44+
Optional second feature array. If None, ``Y = X``.
4245
4346
degree : int, default=3
4447
Degree of the polynomial kernel.
4548
49+
gamma : float, default=None
50+
Scaling factor for the inner product. If None, ``1 / n_features`` is used.
51+
52+
coef0 : float, default=1
53+
Constant term added to the scaled inner product.
54+
4655
queue : SyclQueue or None, default=None
4756
Optional SYCL queue for device execution.
4857
4958
Returns
5059
-------
5160
kernel_matrix : ndarray of shape (n_samples_X, n_samples_Y)
52-
Polynomial kernel Gram matrix.
61+
Computed polynomial kernel Gram matrix.
5362
"""
63+
# Fall back to sklearn if sparse input
64+
if sparse.issparse(X) or (Y is not None and sparse.issparse(Y)):
65+
return sklearn_poly_kernel(X, Y, degree=degree, gamma=gamma, coef0=coef0)
66+
67+
# Handle gamma default like sklearn
68+
if gamma is None:
69+
gamma = 1.0 / X.shape[1]
70+
71+
# Use oneDAL accelerated path
5472
return onedal_poly_kernel(
5573
X, Y=Y, gamma=gamma, coef0=coef0, degree=degree, queue=queue
5674
)
Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
1-
# ==============================================================================
2-
# Copyright contributors to the oneDAL project
3-
#
4-
# Licensed under the Apache License, Version 2.0 (the "License");
5-
# you may not use this file except in compliance with the License.
6-
# You may obtain a copy of the License at
7-
#
8-
# http://www.apache.org/licenses/LICENSE-2.0
9-
#
10-
# Unless required by applicable law or agreed to in writing,
11-
# distributed under the License is distributed on an "AS IS" BASIS,
12-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
# See the License for the specific language governing permissions and
14-
# limitations under the License.
15-
# ==============================================================================
16-
171
import numpy as np
2+
from scipy import sparse
3+
from sklearn.metrics.pairwise import polynomial_kernel as sklearn_poly_kernel
184

195
from sklearnex.svm.poly_kernel import poly_kernel
206

217

22-
def test_poly_kernel_basic():
8+
def test_poly_kernel_dense():
9+
"""Test poly_kernel on dense input arrays."""
2310
X = np.array([[1, 2], [3, 4]])
2411
Y = np.array([[5, 6], [7, 8]])
2512

26-
K = poly_kernel(X, Y, gamma=1.0, coef0=0.0, degree=2)
13+
result = poly_kernel(X, Y, degree=2, gamma=0.5, coef0=1)
14+
expected = sklearn_poly_kernel(X, Y, degree=2, gamma=0.5, coef0=1)
2715

28-
# expected polynomial kernel manually
29-
expected = (np.dot(X, Y.T)) ** 2
30-
assert np.allclose(K, expected), "Polynomial kernel computation is incorrect"
16+
np.testing.assert_allclose(result, expected, rtol=1e-5, atol=1e-8)
3117

3218

33-
def test_poly_kernel_default_Y():
34-
X = np.array([[1, 2], [3, 4]])
35-
K = poly_kernel(X)
36-
expected = (np.dot(X, X.T)) ** 3 # default degree=3
37-
assert np.allclose(K, expected), "Polynomial kernel with Y=None is incorrect"
19+
def test_poly_kernel_sparse_fallback():
20+
"""Test that poly_kernel falls back to sklearn implementation for sparse inputs."""
21+
X = sparse.csr_matrix([[1, 0], [0, 1]])
22+
Y = sparse.csr_matrix([[0, 1], [1, 0]])
23+
24+
result = poly_kernel(X, Y, degree=3, gamma=1.0, coef0=1.0)
25+
expected = sklearn_poly_kernel(X, Y, degree=3, gamma=1.0, coef0=1.0)
26+
27+
np.testing.assert_allclose(result, expected, rtol=1e-5, atol=1e-8)

0 commit comments

Comments
 (0)