Skip to content
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
6 changes: 6 additions & 0 deletions python/cuml/cuml/random_projection/random_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ class GaussianRandomProjection(_BaseRandomProjection):
-----
Inspired by Scikit-learn's implementation:
https://scikit-learn.org/stable/modules/random_projection.html

Currently passing a sparse array to `transform` may result in close (but
not exactly identical) results due to https://github.com/cupy/cupy/issues/9323.
"""

def _gen_random_matrix(self, n_components, n_features, dtype):
Expand Down Expand Up @@ -362,6 +365,9 @@ class SparseRandomProjection(_BaseRandomProjection):
-----
Inspired by Scikit-learn's implementation:
https://scikit-learn.org/stable/modules/random_projection.html

Currently passing a dense array to `transform` may result in close (but
not exactly identical) results due to https://github.com/cupy/cupy/issues/9323.
"""

def __init__(
Expand Down
13 changes: 11 additions & 2 deletions python/cuml/tests/test_random_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,19 @@ def test_random_seed_consistency(cls, sparse):
t1 = model1.transform(X)
model2 = cls(n_components=5, random_state=42).fit(X)
t2 = model2.transform(X)
np.testing.assert_allclose(
np.testing.assert_array_equal(
asdense(model1.components_), asdense(model2.components_)
)
np.testing.assert_allclose(asdense(t1), asdense(t2))
# Due to https://github.com/cupy/cupy/issues/9323 only sparse @ sparse or
# dense @ dense outputs are exactly reproducible. All other combinations
# result in close but not identical outputs. For now we document this and
# relax the test constraint.
if (cls is SparseRandomProjection) != sparse:
# Mix of sparse and dense, check outputs are close
np.testing.assert_allclose(asdense(t1), asdense(t2), rtol=1e-4)
else:
# Both dense or sparse, can check exactly
np.testing.assert_array_equal(asdense(t1), asdense(t2))


@pytest.mark.parametrize("cls", classes)
Expand Down