Skip to content

Commit fd05db2

Browse files
Add sklearnex interface for polynomial kernel (poly_kernel)
Signed-off-by: SnigdhaSarkar16 <sarkarsnigdha16@gmail.com>
1 parent 91885a3 commit fd05db2

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

sklearnex/svm/poly_kernel.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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, software
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+
17+
import numpy as np
18+
from onedal.primitives import poly_kernel as onedal_poly_kernel
19+
20+
def poly_kernel(X, Y=None, gamma=1.0, coef0=0.0, degree=3, queue=None):
21+
"""
22+
sklearnex interface for the polynomial kernel using oneDAL backend.
23+
24+
K(x, y) = (gamma * <x, y> + coef0) ** degree
25+
for each pair of rows x in X and y in Y.
26+
27+
Parameters
28+
----------
29+
X : array-like of shape (n_samples_X, n_features)
30+
Input feature array.
31+
32+
Y : array-like of shape (n_samples_Y, n_features), default=None
33+
Optional second feature array. If None, Y = X.
34+
35+
gamma : float, default=1.0
36+
Scaling factor for the inner product.
37+
38+
coef0 : float, default=0.0
39+
Constant term added to scaled inner product.
40+
41+
degree : int, default=3
42+
Degree of the polynomial kernel.
43+
44+
queue : SyclQueue or None, default=None
45+
Optional SYCL queue for device execution.
46+
47+
Returns
48+
-------
49+
kernel_matrix : ndarray of shape (n_samples_X, n_samples_Y)
50+
Polynomial kernel Gram matrix.
51+
"""
52+
return onedal_poly_kernel(X, Y=Y, gamma=gamma, coef0=coef0, degree=degree, queue=queue)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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, software
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+
17+
import numpy as np
18+
from sklearnex.svm.poly_kernel import poly_kernel as skl_poly_kernel
19+
from onedal.primitives import poly_kernel as onedal_poly_kernel
20+
21+
def test_poly_kernel_basic():
22+
X = np.array([[1, 2], [3, 4]])
23+
Y = np.array([[5, 6], [7, 8]])
24+
25+
# sklearnex interface result
26+
K_skl = skl_poly_kernel(X, Y, gamma=0.5, coef0=1.0, degree=2)
27+
# direct oneDAL result
28+
K_onedal = onedal_poly_kernel(X, Y, gamma=0.5, coef0=1.0, degree=2)
29+
30+
# check they are close
31+
assert np.allclose(K_skl, K_onedal)

0 commit comments

Comments
 (0)