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

Support all-zeroes feature vectors for MG sparse logistic regression #6082

Merged
merged 1 commit into from
Oct 1, 2024
Merged
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
10 changes: 10 additions & 0 deletions cpp/src/glm/qn/mg/standardization.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ void mean_stddev(const raft::handle_t& handle,
{
auto stream = handle.get_stream();
int D = X.n;

if (X.nnz == 0) {
SimpleVec<T> meanVec(mean_vector, D);
meanVec.fill(0., stream);

SimpleVec<T> stddevVec(stddev_vector, D);
stddevVec.fill(0., stream);
return;
}

mean(handle, X, n_samples, mean_vector);

// calculate stdev.S
Expand Down
41 changes: 41 additions & 0 deletions python/cuml/cuml/tests/dask/test_dask_logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,3 +1119,44 @@ def make_classification_with_nnz(
)

assert lr_on.dtype == datatype


@pytest.mark.parametrize("standardization", [False, True])
@pytest.mark.parametrize("fit_intercept", [False, True])
def test_sparse_all_zeroes(standardization, fit_intercept, client):
n_parts = 2
datatype = "float32"

X = np.array([(0, 0), (0, 0), (0, 0), (0, 0)], datatype)
y = np.array([1.0, 1.0, 0.0, 0.0], datatype)
X = csr_matrix(X)
X_da_csr, y_da = _prep_training_data_sparse(client, X, y, n_parts)

from cuml.dask.linear_model import LogisticRegression as cumlLBFGS_dask

mg = cumlLBFGS_dask(
fit_intercept=fit_intercept,
verbose=True,
standardization=standardization,
)
mg.fit(X_da_csr, y_da)
mg_preds = mg.predict(X_da_csr).compute()

from sklearn.linear_model import LogisticRegression

cpu_lr = LogisticRegression(fit_intercept=fit_intercept)
cpu_lr.fit(X, y)
cpu_preds = cpu_lr.predict(X)

assert array_equal(mg_preds, cpu_preds)

assert array_equal(
mg.coef_,
cpu_lr.coef_,
with_sign=True,
)
assert array_equal(
mg.intercept_,
cpu_lr.intercept_,
with_sign=True,
)
Loading