-
Notifications
You must be signed in to change notification settings - Fork 34
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
Speed up kronecker product #70
Conversation
Thanks for this. To be honest, it's kind of disappointing that numpy's |
Pull Request Test Coverage Report for Build 4544439588
💛 - Coveralls |
I was able to reproduce this in a simple test case using numpy 1.22.2: >>> import timeit
>>> import numpy as np
>>> a = np.random.rand(1000)
>>> b = np.random.rand(1000)
>>> timeit.timeit(lambda: np.kron(a, b), number=200)
1.302669789060019
>>> timeit.timeit(lambda: (a[:,None] * b[None,:]).reshape(a.size * b.size), number=200)
0.25691784801892936
>>> np.allclose(np.kron(a, b), (a[:,None] * b[None,:]).reshape(a.size * b.size))
True
>>> np.__version__
'1.22.2' However, if I try on numpy 1.24.2, the performance gap closes, and the current version on >>> import timeit
>>> import numpy as np
>>> a = np.random.rand(1000)
>>> b = np.random.rand(1000)
>>> timeit.timeit(lambda: np.kron(a, b), number=200)
0.2618443979881704
>>> timeit.timeit(lambda: (a[:,None] * b[None,:]).reshape(a.size * b.size), number=200)
0.30681791296228766
>>> np.allclose(np.kron(a, b), (a[:,None] * b[None,:]).reshape(a.size * b.size))
True
>>> np.__version__
'1.24.2' Have you tried upgrading numpy? |
One alternative idea is to warn in this function if somebody is using an old numpy that is known to be slow. |
My best guess is that the performance improvement is due to numpy/numpy#21354. It might be better here to just require the first numpy release that includes that commit (1.23, I expect) as the minimum supported by the toolbox, rather than to emit a warning. |
Thank you for your investigation. I was using an older version of Numpy and was not aware of the improved performance. II agree with @garrison, so I closed this PR. |
I just tried on numpy 1.23.0, and the performance is essentially the same between those two methods. I would support modifying |
Rewrote Kronecker product calculation to not use
np.kron
function. This is because using this numpy function for vector calculations is very computationally expensive. This PR can speed up the reconstruction time as the following image.Experiment Environment: