-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG+1] Fixes n_iter_without_progress and min_grad_norm in TSNE #6497
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -542,3 +542,67 @@ def test_index_offset(): | |
# Make sure translating between 1D and N-D indices are preserved | ||
assert_equal(_barnes_hut_tsne.test_index2offset(), 1) | ||
assert_equal(_barnes_hut_tsne.test_index_offset(), 1) | ||
|
||
|
||
def test_n_iter_without_progress(): | ||
# Make sure that the parameter n_iter_without_progress is used correctly | ||
random_state = check_random_state(0) | ||
X = random_state.randn(100, 2) | ||
tsne = TSNE(n_iter_without_progress=2, verbose=2, | ||
random_state=0, method='exact') | ||
|
||
old_stdout = sys.stdout | ||
sys.stdout = StringIO() | ||
try: | ||
tsne.fit_transform(X) | ||
finally: | ||
out = sys.stdout.getvalue() | ||
sys.stdout.close() | ||
sys.stdout = old_stdout | ||
|
||
# The output needs to contain the value of n_iter_without_progress | ||
assert("did not make any progress during the " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use nose.tools.assert_in |
||
"last 2 episodes. Finished." in out) | ||
|
||
|
||
def test_min_grad_norm(): | ||
# Make sure that the parameter min_grad_norm is used correctly | ||
random_state = check_random_state(0) | ||
X = random_state.randn(100, 2) | ||
min_grad_norm = 0.002 | ||
tsne = TSNE(min_grad_norm=min_grad_norm, verbose=2, | ||
random_state=0, method='exact') | ||
|
||
old_stdout = sys.stdout | ||
sys.stdout = StringIO() | ||
try: | ||
tsne.fit_transform(X) | ||
finally: | ||
out = sys.stdout.getvalue() | ||
sys.stdout.close() | ||
sys.stdout = old_stdout | ||
|
||
lines_out = out.split('\n') | ||
|
||
# extract the gradient norm from the verbose output | ||
gradient_norm_values = [] | ||
for line in lines_out: | ||
# When the computation is Finished just an old gradient norm value | ||
# is repeated that we do not need to store | ||
if 'Finished' in line: | ||
break | ||
|
||
start_grad_norm = line.find('gradient norm') | ||
if start_grad_norm >= 0: | ||
line = line[start_grad_norm:] | ||
line = line.replace('gradient norm = ', '') | ||
gradient_norm_values.append(float(line)) | ||
|
||
# Compute how often the gradient norm is smaller than min_grad_norm | ||
gradient_norm_values = np.array(gradient_norm_values) | ||
n_smaller_gradient_norms = \ | ||
len(gradient_norm_values[gradient_norm_values <= min_grad_norm]) | ||
|
||
# The gradient norm can be smaller than min_grad_norm at most once, | ||
# because in the moment it becomes smaller the optimization stops | ||
assert_less_equal(n_smaller_gradient_norms, 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs newline at end of file. |
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.
This behavior is somewhat peculiar and we could work around it, but let's leave it for now.