-
Notifications
You must be signed in to change notification settings - Fork 19.7k
New pull request -- much cleaner #327
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a808ae6
Add a test for the max-norm constraint
phreeza b48e39a
Add a test for the identity, non-negative, and unit-norm constraints
phreeza 4956ed9
small PEP-8 changes
phreeza d59f251
make some texts a bit more explicit
phreeza f184db8
add exotic inputs to identity test
phreeza 514ac06
missing axis parameter
phreeza acef252
change constraints tests to new constraint api
phreeza 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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import unittest | ||
| import numpy as np | ||
| from numpy.testing import assert_allclose | ||
| from theano import tensor as T | ||
|
|
||
|
|
||
| class TestConstraints(unittest.TestCase): | ||
| def setUp(self): | ||
| self.some_values = [0.1, 0.5, 3, 8, 1e-7] | ||
| np.random.seed(3537) | ||
| self.example_array = np.random.random((100, 100)) * 100. - 50. | ||
| self.example_array[0, 0] = 0. # 0 could possibly cause trouble | ||
|
|
||
| def test_maxnorm(self): | ||
| from keras.constraints import maxnorm | ||
|
|
||
| for m in self.some_values: | ||
| norm_instance = maxnorm(m) | ||
| normed = norm_instance(self.example_array) | ||
| assert (np.all(normed.eval() < m)) | ||
|
|
||
| # a more explicit example | ||
| norm_instance = maxnorm(2.0) | ||
| x = np.array([[0, 0, 0], [1.0, 0, 0], [3, 0, 0], [3, 3, 3]]).T | ||
| x_normed_target = np.array([[0, 0, 0], [1.0, 0, 0], [2.0, 0, 0], [2./np.sqrt(3), 2./np.sqrt(3), 2./np.sqrt(3)]]).T | ||
| x_normed_actual = norm_instance(x).eval() | ||
| assert_allclose(x_normed_actual, x_normed_target) | ||
|
|
||
| def test_nonneg(self): | ||
| from keras.constraints import nonneg | ||
|
|
||
| nonneg_instance = nonneg() | ||
|
|
||
| normed = nonneg_instance(self.example_array) | ||
| assert (np.all(np.min(normed.eval(), axis=1) == 0.)) | ||
|
|
||
| def test_identity(self): | ||
| from keras.constraints import identity | ||
|
|
||
| identity_instance = identity() | ||
|
|
||
| normed = identity_instance(self.example_array) | ||
| assert (np.all(normed == self.example_array)) | ||
|
|
||
| def test_identity_oddballs(self): | ||
| """ | ||
| test the identity constraint on some more exotic input. | ||
| this does not need to pass for the desired real life behaviour, | ||
| but it should in the current implementation. | ||
| """ | ||
| from keras.constraints import identity | ||
| identity_instance = identity() | ||
|
|
||
| oddball_examples = ["Hello", [1], -1, None] | ||
| assert(oddball_examples == identity_instance(oddball_examples)) | ||
|
|
||
| def test_unitnorm(self): | ||
| from keras.constraints import unitnorm | ||
| unitnorm_instance = unitnorm() | ||
|
|
||
| normalized = unitnorm_instance(self.example_array) | ||
|
|
||
| norm_of_normalized = np.sqrt(np.sum(normalized.eval()**2, axis=1)) | ||
| difference = norm_of_normalized - 1. #in the unit norm constraint, it should be equal to 1. | ||
| largest_difference = np.max(np.abs(difference)) | ||
| self.assertAlmostEqual(largest_difference, 0.) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
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.
Why the camel case here only?
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.
I don't know ... I think it's because of habit when using the unittest framework, I think it magically recognised that particular name, so it "has" to be camel case to be discovered appropriately. My knowledge could be stale, but that's why.