Skip to content

Commit a7cfaf9

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

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
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
)

sklearnex/svm/tests/test_poly_kernel_sklearnex.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,37 @@
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 sklearnex.svm.poly_kernel import poly_kernel
2022

2123

22-
def test_poly_kernel_basic():
24+
def test_poly_kernel_dense():
25+
"""Test poly_kernel on dense input arrays."""
2326
X = np.array([[1, 2], [3, 4]])
2427
Y = np.array([[5, 6], [7, 8]])
2528

26-
K = poly_kernel(X, Y, gamma=1.0, coef0=0.0, degree=2)
29+
result = poly_kernel(X, Y, degree=2, gamma=0.5, coef0=1)
30+
expected = sklearn_poly_kernel(X, Y, degree=2, gamma=0.5, coef0=1)
2731

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

3234

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"
35+
def test_poly_kernel_sparse_fallback():
36+
"""Test that poly_kernel falls back to sklearn implementation for sparse inputs."""
37+
X = sparse.csr_matrix([[1, 0], [0, 1]])
38+
Y = sparse.csr_matrix([[0, 1], [1, 0]])
39+
40+
result = poly_kernel(X, Y, degree=3, gamma=1.0, coef0=1.0)
41+
expected = sklearn_poly_kernel(X, Y, degree=3, gamma=1.0, coef0=1.0)
42+
43+
np.testing.assert_allclose(result, expected, rtol=1e-5, atol=1e-8)

0 commit comments

Comments
 (0)