-
Notifications
You must be signed in to change notification settings - Fork 30
Issue/64/mp_sparse_encode #65
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
Open
zalgo3
wants to merge
9
commits into
development
Choose a base branch
from
issue/64/mp_sparse_encode
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8ed7d39
#64 implement matching pursuit into sparse coding (with bug)
zalgo3 8da2c10
#64 add matching pursuit and unittest
zalgo3 2d2d827
#64 rename variable name and change docstring to linear_model.matchin…
zalgo3 22de620
#64 fix bugs in test_decomposition_dict_learning (normalized the dict…
zalgo3 b9e1d6e
#64 fix import error
zalgo3 a2c8935
#66 normalized dictionaries in unittest
zalgo3 722b241
#64 use overrided sparse_encode in sparse_encode_with_mask
zalgo3 00615fb
#64 raise ValueError when columns of dictionary are not normalized.
zalgo3 1b304fa
Merge pull request #68 from hacarus/issue/66/normalize_dictionaries_i…
sutkss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| from .ksvd import KSVD | ||
| from .dict_learning import sparse_encode_with_mask | ||
| from .dict_learning import sparse_encode_with_mask, sparse_encode | ||
|
|
||
| __all__ = [ | ||
| 'KSVD', | ||
| 'sparse_encode_with_mask' | ||
| 'sparse_encode' | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| from .admm import LassoADMM, FusedLassoADMM, TrendFilteringADMM, QuadraticTrendFilteringADMM | ||
| from .mp import matching_pursuit | ||
|
|
||
| __all__ = [ | ||
| 'LassoADMM', | ||
| 'FusedLassoADMM', | ||
| 'TrendFilteringADMM', | ||
| 'QuadraticTrendFilteringADMM', | ||
| 'matching_pursuit' | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Matching pursuit algorithms | ||
| """ | ||
|
|
||
| import numpy as np | ||
| from sklearn.utils import check_array | ||
| from sklearn.preprocessing import normalize | ||
|
|
||
| def matching_pursuit(dictionary, signal, n_nonzero_coefs=None, copy_dictionary=True, copy_signal=True, tol=None): | ||
| """Matching Pursuit (MP) | ||
|
|
||
| Solves n_targets Matching Pursuit problems. | ||
| The goal is to find a sparse array `coefs` such that:: | ||
| signal ~= coefs * dictionary | ||
| Parameters | ||
| ---------- | ||
| dictionary : array, shape (n_components, n_features) | ||
| Input data. Columns are assumed to have unit norm. | ||
|
|
||
| signal : array, shape (n_samples, n_features) | ||
| Input targets. | ||
|
|
||
| n_nonzero_coefs : int | ||
| Targeted number of non-zero elements | ||
|
|
||
| copy_X : bool, optional | ||
| Whether the input data must be copied by the algorithm. A false | ||
| value is only helpful if it is already Fortran-ordered, otherwise a | ||
| copy is made anyway. | ||
|
|
||
| copy_y : bool, optional | ||
| Whether the covariance vector Xy must be copied by the algorithm. | ||
| If False, it may be overwritten. | ||
|
|
||
| tol : float | ||
| Maximum norm of the residual. | ||
| """ | ||
| normalized = normalize(dictionary) | ||
| if abs(np.linalg.norm(normalized - dictionary, 'fro')) > len(dictionary) * 1e-8: | ||
| raise ValueError("All columns of the dictionary must have unit norm.") | ||
| if tol is not None and tol < 0: | ||
| raise ValueError("Epsilon cannot be negative") | ||
| dictionary = check_array(dictionary, order='F', copy=copy_dictionary) | ||
| if copy_signal: | ||
| signal = signal.copy() | ||
|
|
||
| n_active = 0 | ||
|
|
||
| n_samples = signal.shape[0] | ||
| n_components = dictionary.shape[0] | ||
| n_features = dictionary.shape[1] | ||
| coefs = np.zeros((n_samples, n_components)) | ||
| while(True): | ||
| inners = dictionary.dot(signal.T) | ||
| max_ids = np.argmax(np.abs(inners), axis=0) | ||
| for i, max_id in enumerate(max_ids): | ||
| coefs[i, max_id] += inners[max_id, i] | ||
| signal[i] -= inners[max_id, i] * dictionary[max_id] | ||
| n_active += 1 | ||
| if tol is not None and np.linalg.norm(signal) <= tol: | ||
| break | ||
| if n_active == n_features or n_active == n_nonzero_coefs: | ||
| break | ||
| return coefs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import unittest | ||
|
|
||
| from spmimage.linear_model import matching_pursuit | ||
| import numpy as np | ||
| from sklearn.preprocessing import normalize | ||
| from numpy.testing import assert_array_almost_equal | ||
|
|
||
|
|
||
| class TestMatchingPursuit(unittest.TestCase): | ||
| def setUp(self): | ||
| np.random.seed() | ||
|
|
||
| def test_matching_pursuit(self): | ||
| X = np.array([[1., 2.], [3., 4.], [5., 6.]]) | ||
| beta = np.array([[0., 1., 0.], [3., 0., 0.]]) | ||
| y = beta.dot(X) | ||
| with self.assertRaises(ValueError): | ||
| predict = matching_pursuit(dictionary=X, signal=y, tol=1e-6) | ||
| X = normalize(X) | ||
| y = beta.dot(X) | ||
| predict = matching_pursuit(dictionary=X, signal=y, tol=1e-6) | ||
| assert_array_almost_equal(beta, predict) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zalgo-edu Please make sure each vector in dictionary has norm 1.
If this condition is not satisfied, please raise warning message.