Skip to content

Commit 6c3c435

Browse files
Add sklearnex interface for polynomial kernel
Signed-off-by: SnigdhaSarkar16 <sarkarsnigdha16@gmail.com>
1 parent fd05db2 commit 6c3c435

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

sklearnex/svm/poly_kernel.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77
#
88
# http://www.apache.org/licenses/LICENSE-2.0
99
#
10-
# Unless required by applicable law or agreed to in writing, software
10+
# Unless required by applicable law or agreed to in writing,
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+
1819
from onedal.primitives import poly_kernel as onedal_poly_kernel
1920

21+
2022
def poly_kernel(X, Y=None, gamma=1.0, coef0=0.0, degree=3, queue=None):
2123
"""
22-
sklearnex interface for the polynomial kernel using oneDAL backend.
24+
Sklearnex interface for the polynomial kernel using oneDAL backend.
2325
2426
K(x, y) = (gamma * <x, y> + coef0) ** degree
2527
for each pair of rows x in X and y in Y.
@@ -49,4 +51,6 @@ def poly_kernel(X, Y=None, gamma=1.0, coef0=0.0, degree=3, queue=None):
4951
kernel_matrix : ndarray of shape (n_samples_X, n_samples_Y)
5052
Polynomial kernel Gram matrix.
5153
"""
52-
return onedal_poly_kernel(X, Y=Y, gamma=gamma, coef0=coef0, degree=degree, queue=queue)
54+
return onedal_poly_kernel(
55+
X, Y=Y, gamma=gamma, coef0=coef0, degree=degree, queue=queue
56+
)

sklearnex/svm/tests/test_poly_kernel_sklearnex.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@
77
#
88
# http://www.apache.org/licenses/LICENSE-2.0
99
#
10-
# Unless required by applicable law or agreed to in writing, software
10+
# Unless required by applicable law or agreed to in writing,
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 sklearnex.svm.poly_kernel import poly_kernel as skl_poly_kernel
19-
from onedal.primitives import poly_kernel as onedal_poly_kernel
18+
19+
from sklearnex.svm.poly_kernel import poly_kernel
20+
2021

2122
def test_poly_kernel_basic():
2223
X = np.array([[1, 2], [3, 4]])
2324
Y = np.array([[5, 6], [7, 8]])
2425

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)
26+
K = poly_kernel(X, Y, gamma=1.0, coef0=0.0, degree=2)
27+
28+
# expected polynomial kernel manually
29+
expected = (np.dot(X, Y.T)) ** 2
30+
assert np.allclose(K, expected), "Polynomial kernel computation is incorrect"
2931

30-
# check they are close
31-
assert np.allclose(K_skl, K_onedal)
32+
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"

0 commit comments

Comments
 (0)