Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert GPU CPU intercept_ equal when fit_intercept is false in cuml.LogisticRegression #5567

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/cuml/linear_model/logistic_regression_mg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class LogisticRegressionMG(MGFitMixin, LogisticRegression):
self.solver_model.coef_ = value

def prepare_for_fit(self, n_classes):
self.qnparams = QNParams(
self.solver_model.qnparams = QNParams(
loss=self.loss,
penalty_l1=self.l1_strength,
penalty_l2=self.l2_strength,
Expand Down Expand Up @@ -179,7 +179,7 @@ class LogisticRegressionMG(MGFitMixin, LogisticRegression):
self.prepare_for_fit(self._num_classes)
cdef uintptr_t mat_coef_ptr = self.coef_.ptr

cdef qn_params qnpams = self.qnparams.params
cdef qn_params qnpams = self.solver_model.qnparams.params

if self.dtype == np.float32:
qnFit(
Expand Down
9 changes: 8 additions & 1 deletion python/cuml/solvers/qn.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,15 @@ class QN(Base,

if self.fit_intercept:
self.intercept_ = self._coef_[-1]
else:
return

_num_classes_dim, _ = self.coef_.shape
_num_classes = self.get_num_classes(_num_classes_dim)

if _num_classes == 2:
self.intercept_ = CumlArray.zeros(shape=1)
else:
self.intercept_ = CumlArray.zeros(shape=_num_classes)

def get_param_names(self):
return super().get_param_names() + \
Expand Down
3 changes: 3 additions & 0 deletions python/cuml/tests/test_linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,9 @@ def test_logistic_regression(
)
assert len(np.unique(cu_preds)) == len(np.unique(y_test))

if fit_intercept is False:
assert np.array_equal(culog.intercept_, sklog.intercept_)
Comment on lines +564 to +565
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the point of this change was to ensure that intercept_ is equal in both cases. Why are we only testing for equality when fit_intercept is False ?

Copy link
Contributor Author

@lijinf2 lijinf2 Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When fit_intercept is True, intercept_ can be a list of non-zero. It is possible that culog.intercept_ and sklog.intercept_ are different, given their implementations are not exactly the same.

When fit_intercept is False, intercept_ is a list of zero. The test case is able to assert equal the culog.intercept_ and sklog.intercept_.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But they would still be similar, no? You can use the cuml.testing.utils.array_equal function like here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had tried assert array_equal(culog.intercept_, sklog.intercept_, 1e-3, with_sign=True).
Some tests failed:
FAILED tests/test_linear_model.py::test_logistic_regression[column_info0-1000-2-float32-none-1.0-True-1.0-0.001] - AssertionError: assert <array_equal: [0.60477465] [0.6086404] unit_tol=0.001 total_tol=0.0001 with_sign=True>

FAILED tests/test_linear_model.py::test_logistic_regression[column_info0-1000-10-float32-l1-1.0-True-1.0-0.001] - AssertionError: assert <array_equal: [-0.15404558 0.10340142 0.12519604 ... -0.13840534 0.13014889 -0.01927175] [-0.14051172 0.11931933 0.14109698 ... -0.12780005 0.14333196 -0.0074189 ] unit_tol=0.001 t...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mean accuracies of culog and sklog are the same though. It seems the models converged to different optima.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's a valid point.



@given(
dtype=floating_dtypes(sizes=(32, 64)),
Expand Down