Merged
Conversation
Speed-up in 3 places:
- the computation of pairwise distance is faster with sklearn.metrics.pairwise.euclidean_distances
- faster computation of K = np.exp(-M / reg)
- faster computation of the error every 10 iterations
Example with this little script:
import time
import numpy as np
import ot
rng = np.random.RandomState(0)
transport = ot.da.SinkhornTransport()
time1 = time.time()
Xs, ys, Xt = rng.randn(10000, 100), rng.randint(0, 2, size=10000), rng.randn(10000, 100)
transport.fit(Xs=Xs, Xt=Xt)
time2 = time.time()
print("OT Computation Time {:6.2f} sec".format(time2-time1))
transport = ot.da.SinkhornLpl1Transport()
transport.fit(Xs=Xs, ys=ys, Xt=Xt)
time3 = time.time()
print("OT LpL1 Computation Time {:6.2f} sec".format(time3-time2))
Before
OT Computation Time 19.93 sec
OT LpL1 Computation Time 133.43 sec
After
OT Computation Time 7.55 sec
OT LpL1 Computation Time 82.25 sec
Collaborator
|
Hello @LeoGautheron , Thank you for all your work. Those are very nice speedup and i can confirm that I have similar gains. Still have a few comments.
When i run the following code ot.tic()
M1=ot.dist(x1,x2)
ot.toc()
ot.tic()
M2=euclidean_distances(x1,x2, squared=True)
ot.toc()
ot.tic()
x1p2 = np.sum(np.square(x1), 1)
x2p2 = np.sum(np.square(x2), 1)
M3=x1p2.reshape((-1, 1)) + x2p2.reshape((1, -1)) - 2 * np.dot(x1, x2.T)
ot.toc()I get the following: with the last one pure numpy. I think the gain is sufficient with the last implementation and avoid an additional dependency for POT.
# Next N lines equivalent to:
# K= np.exp(-M/reg)For the next guy who wants to have a look at the function. In any case its nice work and the computational gain is very important. |
Collaborator
|
just copy the code you need then https://github.com/scikit-learn/scikit-learn/blob/a24c8b46/sklearn/metrics/pairwise.py#L163 it's pure python code. |
Author
|
I used the code from sklearn, still the same performances now :) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Speed-up in 3 places:
Example with this little script:
import time
import numpy as np
import ot
rng = np.random.RandomState(0)
transport = ot.da.SinkhornTransport()
time1 = time.time()
Xs, ys, Xt = rng.randn(10000, 100), rng.randint(0, 2, size=10000), rng.randn(10000, 100)
transport.fit(Xs=Xs, Xt=Xt)
time2 = time.time()
print("OT Computation Time {:6.2f} sec".format(time2-time1))
transport = ot.da.SinkhornLpl1Transport()
transport.fit(Xs=Xs, ys=ys, Xt=Xt)
time3 = time.time()
print("OT LpL1 Computation Time {:6.2f} sec".format(time3-time2))
Before
OT Computation Time 19.93 sec
OT LpL1 Computation Time 133.43 sec
After
OT Computation Time 7.55 sec
OT LpL1 Computation Time 82.25 sec