Skip to content

[MRG+1] TSNE: only compute error when needed - #10593

Merged
glemaitre merged 6 commits into
scikit-learn:masterfrom
TomDLT:tsne_compute_error
Feb 7, 2018
Merged

glemaitre merged 6 commits into
scikit-learn:masterfrom
TomDLT:tsne_compute_error

Conversation

@TomDLT

@TomDLT TomDLT commented Feb 6, 2018 •

Copy link
Copy Markdown
Member

This is a first speed improvement for TSNE with both solvers.
The idea is to compute the KL-divergence only every 50 iterations, when the stopping criterion is actually checked.

With method='barnes_hut', it leads to a 40% speed improvement on the gradient descent.

master_gradient_duration = array([   1.411,    8.815,   18.606,   97.405,  208.401])
branch_gradient_duration = array([   0.836,    4.988,   11.352,   61.483,  136.631])

figure_1

The benchmark is available here, where I used --pca-components 0 and a single CPU.

@jnothman

jnothman commented Feb 6, 2018

Copy link
Copy Markdown
Member

Tests failing... I've not looked, but hope it's minor, because this sounds great!

@TomDLT

TomDLT commented Feb 6, 2018 •

Copy link
Copy Markdown
Member Author

Tests fixed.


With method='exact', the speed improvement is also 30%-40%:

master_duration = array([    1.902,    21.813,    73.14 ,  1624.989])
branch_duration = array([    2.096,    15.586,    51.036,  1002.764])

figure_1

@TomDLT
TomDLT force-pushed the tsne_compute_error branch from 1910079 to 794d790 Compare February 6, 2018 18:15
@TomDLT TomDLT changed the title TSNE: only compute error when needed [MRG] TSNE: only compute error when needed Feb 6, 2018
@jnothman

jnothman commented Feb 6, 2018

Copy link
Copy Markdown
Member

Nice!

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very nice!

Comment thread sklearn/manifold/_barnes_hut_tsne.pyx Outdated
np.int64_t start,
int verbose) nogil:
int verbose,
int compute_error) nogil:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should be a bint for clarity?

Comment thread sklearn/manifold/_barnes_hut_tsne.pyx Outdated
long start,
long stop) nogil:
long stop,
int compute_error) nogil:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should be a bint for clarity?

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, a few points...

Comment thread sklearn/manifold/t_sne.py Outdated
for i in range(it, n_iter):
check_convergence = (i + 1) % n_iter_check == 0
# only compute the error when needed
if 'compute_error' in kwargs:

Copy link
Copy Markdown
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? What is the case where kwargs does not include compute_error? Do we need to be passing it in at all or just setting it here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It was to deal with this test.
I though it was safer to put this safe guard, but I am not sure anymore since _gradient_descent is private

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about just put it comute_error=True in the default kwargs set above when kwargs is None? Then the logic in the loop is clearer and independent of the call params.

Comment thread sklearn/manifold/t_sne.py Outdated
check_convergence = (i + 1) % n_iter_check == 0
# only compute the error when needed
if 'compute_error' in kwargs:
kwargs['compute_error'] = check_convergence

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you need or i == n_iter - 1 here so that the verbose-printed KL-divergence and that set in kl_divergence_ is accurate.

Comment thread sklearn/manifold/t_sne.py Outdated
opt_args['kwargs']['angle'] = self.angle
# Repeat verbose argument for _kl_divergence_bh
opt_args['kwargs']['verbose'] = self.verbose
opt_args['kwargs']['compute_error'] = True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we can set this above where opt_args is defined, as it is invariant to method.

@TomDLT
TomDLT force-pushed the tsne_compute_error branch from f1a999b to 67164ab Compare February 6, 2018 23:54
Comment thread sklearn/manifold/t_sne.py Outdated
"verbose": self.verbose,
"kwargs": dict(skip_num_points=skip_num_points),
"kwargs": dict(skip_num_points=skip_num_points,
compute_error=True),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this is needed anymore, as the default True will be upheld.

Comment thread sklearn/manifold/t_sne.py Outdated
kl_divergence = 2.0 * np.dot(
P, np.log(np.maximum(P, MACHINE_EPSILON) / Q))
else:
kl_divergence = 0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe return NaN so that this value is not mishandled?

@TomDLT
TomDLT force-pushed the tsne_compute_error branch from 76430c3 to 049af56 Compare February 7, 2018 00:09

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@jnothman jnothman changed the title [MRG] TSNE: only compute error when needed [MRG+1] TSNE: only compute error when needed Feb 7, 2018
@glemaitre
glemaitre merged commit 6af51e6 into scikit-learn:master Feb 7, 2018
@glemaitre

Copy link
Copy Markdown
Member

Merging. Thanks!!!

@TomDLT
TomDLT deleted the tsne_compute_error branch June 15, 2018 13:39
@TomDLT TomDLT mentioned this pull request Sep 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants