Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
olegkkruglov committed Sep 3, 2024
1 parent fceaefc commit 73cddd3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
21 changes: 10 additions & 11 deletions onedal/linear_model/incremental_linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ def partial_fit(self, X, y, queue=None):
self._dtype = get_dtype(X)
self._params = self._get_onedal_params(self._dtype)

y = np.asarray(y).astype(dtype=self._dtype)
self._y_ndim_1 = y.ndim == 1
y = np.asarray(y, dtype=self._dtype)

X, y = _check_X_y(X, y, dtype=[np.float64, np.float32], accept_2d_y=True)
X, y = _check_X_y(
X, y, dtype=[np.float64, np.float32], accept_2d_y=True, force_all_finite=False
)

self.n_features_in_ = _num_features(X, fallback_1d=True)
X_table, y_table = to_table(X, y)
Expand Down Expand Up @@ -139,14 +140,10 @@ def finalize_fit(self, queue=None):

packed_coefficients = from_table(result.model.packed_coefficients)
self.coef_, self.intercept_ = (
packed_coefficients[:, 1:],
packed_coefficients[:, 0],
packed_coefficients[:, 1:].squeeze(),
packed_coefficients[:, 0].squeeze(),
)

if self.coef_.shape[0] == 1 and self._y_ndim_1:
self.coef_ = self.coef_.ravel()
self.intercept_ = self.intercept_[0]

return self


Expand Down Expand Up @@ -216,9 +213,11 @@ def partial_fit(self, X, y, queue=None):
self._dtype = get_dtype(X)
self._params = self._get_onedal_params(self._dtype)

y = np.asarray(y).astype(dtype=self._dtype)
y = np.asarray(y, dtype=self._dtype)

X, y = _check_X_y(X, y, dtype=[np.float64, np.float32], accept_2d_y=True)
X, y = _check_X_y(
X, y, dtype=[np.float64, np.float32], accept_2d_y=True, force_all_finite=False
)

self.n_features_in_ = _num_features(X, fallback_1d=True)
X_table, y_table = to_table(X, y)
Expand Down
7 changes: 4 additions & 3 deletions onedal/spmd/linear_model/incremental_linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ def partial_fit(self, X, y, queue=None):
self._dtype = get_dtype(X)
self._params = self._get_onedal_params(self._dtype)

y = np.asarray(y).astype(dtype=self._dtype)
self._y_ndim_1 = y.ndim == 1
y = np.asarray(y, dtype=self._dtype)

X, y = _check_X_y(X, y, dtype=[np.float64, np.float32], accept_2d_y=True)
X, y = _check_X_y(
X, y, dtype=[np.float64, np.float32], accept_2d_y=True, force_all_finite=False
)

self.n_features_in_ = _num_features(X, fallback_1d=True)
X_table, y_table = to_table(X, y)
Expand Down
8 changes: 4 additions & 4 deletions sklearnex/linear_model/tests/test_incremental_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_sklearnex_fit_on_gold_data(dataframe, queue, fit_intercept, macro_block
X = np.array([[1], [2]])
X = X.astype(dtype=dtype)
X_df = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
y = np.array([1, 2])
y = np.array([[1], [2]])
y = y.astype(dtype=dtype)
y_df = _convert_to_dataframe(y, sycl_queue=queue, target_df=dataframe)

Expand Down Expand Up @@ -185,16 +185,16 @@ def test_sklearnex_partial_fit_on_random_data(
inclin.partial_fit(X_split_df, y_split_df)

tol = 1e-4 if inclin.coef_.dtype == np.float32 else 1e-7
assert_allclose(coef, inclin.coef_.T, atol=tol)
assert_allclose(coef.T.squeeze(), inclin.coef_, atol=tol)

if fit_intercept:
assert_allclose(intercept, inclin.intercept_, atol=tol)

X_test = gen.random(size=(num_samples, num_features), dtype=dtype)
if fit_intercept:
expected_y_pred = X_test @ coef + intercept[np.newaxis, :]
expected_y_pred = (X_test @ coef + intercept[np.newaxis, :]).squeeze()
else:
expected_y_pred = X_test @ coef
expected_y_pred = (X_test @ coef).squeeze()

X_test_df = _convert_to_dataframe(X_test, sycl_queue=queue, target_df=dataframe)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def test_incremental_linear_regression_fit_spmd_gold(
[13.0, 32.0],
[14.0, 64.0],
[15.0, 128.0],
]
).astype(dtype=dtype)
],
dtype=dtype,
)
dpt_X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
local_X = _get_local_tensor(X)
local_dpt_X = _convert_to_dataframe(local_X, sycl_queue=queue, target_df=dataframe)
Expand Down Expand Up @@ -143,8 +144,9 @@ def test_incremental_linear_regression_partial_fit_spmd_gold(
[13.0, 32.0],
[14.0, 64.0],
[15.0, 128.0],
]
).astype(dtype=dtype)
],
dtype=dtype,
)
dpt_X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
local_X = _get_local_tensor(X)
split_local_X = np.array_split(local_X, num_blocks)
Expand Down

0 comments on commit 73cddd3

Please sign in to comment.