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

FIX SparseCoder set_params works correctly #15236

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions sklearn/decomposition/dict_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,12 +1018,18 @@ def __init__(self, dictionary, transform_algorithm='omp',
transform_n_nonzero_coefs=None, transform_alpha=None,
split_sign=False, n_jobs=None, positive_code=False,
transform_max_iter=1000):
self._set_sparse_coding_params(dictionary.shape[0],
Copy link
Member

Choose a reason for hiding this comment

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

Is this helper method still used?

transform_algorithm,
transform_n_nonzero_coefs,
transform_alpha, split_sign, n_jobs,
positive_code, transform_max_iter)
self.components_ = dictionary
self.dictionary = dictionary
self.transform_algorithm = transform_algorithm
self.transform_n_nonzero_coefs = transform_n_nonzero_coefs
self.transform_alpha = transform_alpha
self.transform_max_iter = transform_max_iter
self.split_sign = split_sign
self.n_jobs = n_jobs
self.positive_code = positive_code

@property
def n_components(self):
Copy link
Member

Choose a reason for hiding this comment

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

why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

_set_sparse_coding_params used to set self.n_components as inferred from dictionary. Since that no longer happens, I added the property in case n_components is still required.

return self.dictionary.shape[0]

def fit(self, X, y=None):
"""Do nothing and return the estimator unchanged
Expand All @@ -1042,6 +1048,7 @@ def fit(self, X, y=None):
self : object
Returns the object itself
"""
self.components_ = self.dictionary
return self


Expand Down
32 changes: 31 additions & 1 deletion sklearn/decomposition/tests/test_dict_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,41 @@ def test_sparse_coder_estimator():
V = rng.randn(n_components, n_features) # random init
V /= np.sum(V ** 2, axis=1)[:, np.newaxis]
code = SparseCoder(dictionary=V, transform_algorithm='lasso_lars',
transform_alpha=0.001).transform(X)
transform_alpha=0.001).fit(X).transform(X)
assert not np.all(code == 0)
assert np.sqrt(np.sum((np.dot(code, V) - X) ** 2)) < 0.1


def test_sparse_coder_set_params_works():
# checks for a bug in SparseCoder that made set_params not
# actually work
n_components = 12
rng = np.random.RandomState(0)
correct_V = rng.randn(n_components, n_features) # random init
correct_V /= np.sum(correct_V ** 2, axis=1)[:, np.newaxis]
wrong_V = 1 / correct_V

# temporarily set wrong V and then set_params the correct one to
# check if set_params actually has an effect
coder = SparseCoder(dictionary=wrong_V, transform_algorithm='lasso_lars',
transform_alpha=0.001)
coder.set_params(dictionary=correct_V)
code = coder.fit(X).transform(X)
assert not np.all(code == 0)
assert np.sqrt(np.sum((np.dot(code, correct_V) - X) ** 2)) < 0.1


@pytest.mark.parametrize('dictionary, n_components', [
(np.zeros((1, 1)), 1),
(np.zeros((1, 3)), 1),
(np.zeros((3, 1)), 3),
(np.zeros((5, 4)), 5),
])
def test_sparse_coder_n_components(dictionary, n_components):
coder = SparseCoder(dictionary=dictionary)
assert coder.n_components == n_components


def test_sparse_coder_parallel_mmap():
# Non-regression test for:
# https://github.com/scikit-learn/scikit-learn/issues/5956
Expand Down