Skip to content

[MRG+1] TSNE: Avoid power computation if exponent is 1 - #10610

Merged
glemaitre merged 6 commits into
scikit-learn:masterfrom
TomDLT:tsne_exponent
Feb 13, 2018
Merged

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

Conversation

@TomDLT

@TomDLT TomDLT commented Feb 9, 2018 •

Copy link
Copy Markdown
Member

Follows #10593
This is a second speed improvement for TSNE with "barnes_hut" solver.

The idea is to avoid an exponent computation.
This exponent is almost always equal to -1.
Indeed, the embedded space is equal to 2 in almost every application, and:

n_components = 2  # Dimension of the embedded space. Default = 2
degrees_of_freedom = max(self.n_components - 1.0, 1)
exponent = (degrees_of_freedom + 1.0) / -2.0

This PR changes exponent into -exponent which is now equal to 1 in most cases.

It leads to a 24% speed improvement on the gradient descent.

master_gradient_duration = array([  0.442,   3.06 ,   8.352,  40.11 ,  94.581])
branch_gradient_duration = array([  0.34 ,   2.271,   6.384,  32.006,  67.879])
relative_improvement = array([ 0.23076923,  0.25784314,  0.23563218,  0.20204438,  0.28231886])

figure_1

or total TSNE duration including NearestNeighbors (NN):
figure_1

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

@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.

Great catch!

Comment thread sklearn/manifold/_barnes_hut_tsne.pyx Outdated
# is about 10-15x more expensive than the
# following for loop
exponent = (dof + 1.0) / -2.0
exponent = (dof + 1.0) / 2.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.

It doesn't look like this has a cdef!! It should also be set at the beginning of the function.

Should probably use cython -a to check for other untyped variables...

@TomDLT TomDLT changed the title TSNE: Avoid power computation if exponent is 1 [MRG] TSNE: Avoid power computation if exponent is 1 Feb 10, 2018

@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.

Thanks. I'm not sure if things like m deserve typing, nor if summary would've been implicitly typed because it's assigned a pointer... But this is good. I assume those typings didn't make a big dent on the benchmark

@jnothman jnothman changed the title [MRG] TSNE: Avoid power computation if exponent is 1 [MRG+1] TSNE: Avoid power computation if exponent is 1 Feb 10, 2018
Comment thread sklearn/manifold/_barnes_hut_tsne.pyx Outdated
dij += buff[ax] * buff[ax]
qij = ((1.0 + dij / dof) ** exponent)
qij = dof / (dof + dij)
if exponent != 1:

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.

Careful, comparison with a float, that could suck :)

@glemaitre

Copy link
Copy Markdown
Member

I was checking a bit more and there is no real reasoning to have dof to be a float. dof is defined from the n_components:

degrees_of_freedom = max(self.n_components - 1.0, 1)

So basically, it will always be an integer. Then the comparison that you made will be ok. It required to change a bit more code but this is internal code. My only issue is when computing qij = dof / (dof + dij), then we need to cast dof to a float it seems which would introduce an overhead. The bench should show what is the best. IMO, having integer around will be cleaner :)

@TomDLT

TomDLT commented Feb 13, 2018

Copy link
Copy Markdown
Member Author

Is it better @glemaitre ?

@glemaitre

Copy link
Copy Markdown
Member

@TomDLT You forgot to change the signature of def gradient(..., int dof, ...) I think.

@glemaitre

Copy link
Copy Markdown
Member

and def compute_gradient(..., int dof, ...)

quad_tree._QuadTree qt,
float theta,
float dof,
int dof,

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.

isn't it already here?

int n_dimensions,
int verbose,
float dof = 1.0,
int dof=1,

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.

and here?

@sklearn-lgtm

Copy link
Copy Markdown

This pull request introduces 37 alerts and fixes 18 - view on lgtm.com

new alerts:

  • 37 for Explicit export is not defined

fixed alerts:

  • 6 for Mismatch between signature and use of an overridden method
  • 3 for Missing call to init during object initialization
  • 3 for Potentially uninitialized local variable
  • 2 for Comparison using is when operands support eq
  • 2 for Wrong number of arguments for format
  • 1 for Conflicting attributes in base classes
  • 1 for Mismatch between signature and use of an overriding method

Comment posted by lgtm.com

@glemaitre

Copy link
Copy Markdown
Member

Ups sorry. Too much gradient everywhere. I misread :)

@glemaitre glemaitre 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 apart of one revert think. @TomDLT before to merge, can you run the bench to see if something changed?

Comment thread sklearn/manifold/t_sne.py Outdated
# Q is a heavy-tailed distribution: Student's t-distribution
dist = pdist(X_embedded, "sqeuclidean")
dist /= degrees_of_freedom
dist /= float(degrees_of_freedom)

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 would let it as is ensuring that we have from __future__ import division on the top

@glemaitre

Copy link
Copy Markdown
Member

by the way, I don't get why LGTM is complaining :)

@TomDLT

TomDLT commented Feb 13, 2018

Copy link
Copy Markdown
Member Author

Rerunning the benchmark (on a different computer): The ~24% speed improvement is still there.

master_gradient_duration = array([   0.808,    5.053,   11.637,   61.819,  136.813])
branch_gradient_duration = array([   0.679,    3.538,    8.616,   46.186,  104.56 ])
relative_improvement = array([ 0.15965347,  0.29982189,  0.25960299,  0.25288342,  0.23574514])

figure_2

or total TSNE duration including NearestNeighbors (NN):
figure_1

@glemaitre

Copy link
Copy Markdown
Member

Great merging when it's green

@sklearn-lgtm

Copy link
Copy Markdown

This pull request introduces 37 alerts and fixes 18 - view on lgtm.com

new alerts:

  • 37 for Explicit export is not defined

fixed alerts:

  • 6 for Mismatch between signature and use of an overridden method
  • 3 for Missing call to init during object initialization
  • 3 for Potentially uninitialized local variable
  • 2 for Comparison using is when operands support eq
  • 2 for Wrong number of arguments for format
  • 1 for Conflicting attributes in base classes
  • 1 for Mismatch between signature and use of an overriding method

Comment posted by lgtm.com

@glemaitre
glemaitre merged commit 43eb87e into scikit-learn:master Feb 13, 2018
@glemaitre

Copy link
Copy Markdown
Member

Thanks Tom

@TomDLT

TomDLT commented Feb 13, 2018

Copy link
Copy Markdown
Member Author

Thanks for the reviews !

@jnothman

jnothman commented Feb 13, 2018 via email

Copy link
Copy Markdown
Member

@sj

sj commented Feb 13, 2018

Copy link
Copy Markdown

Thanks for flagging this up @jnothman — I'll try to figure out what went wrong here on the lgtm.com side.

@TomDLT
TomDLT deleted the tsne_exponent 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.

5 participants