Last active
August 3, 2021 14:42
-
-
Save yangyushi/017612c46e485eb369f7e0a97ba56060 to your computer and use it in GitHub Desktop.
pairwise dot product with einsum
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
| #!/usr/bin/env python3 | |
| import numpy as np | |
| from time import time | |
| a = np.random.random((10000, 3)) | |
| b = np.random.random((10000, 3)) | |
| t0 = time() | |
| c = np.array([np.cross(x, y) for x, y in zip(a, b)]) | |
| print(f'vanilla: {time() - t0:.4f}') | |
| t0 = time() | |
| d = np.cross(a, b) | |
| print(f'einsim: {time() - t0:.4f}') | |
| print(f'identical? {np.isclose(c, d).all()}') |
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
| #!/usr/bin/env python3 | |
| import numpy as np | |
| from time import time | |
| a = np.random.random((1000000, 3)) | |
| b = np.random.random((1000000, 3)) | |
| t0 = time() | |
| c = np.array([x@y for x,y in zip(a, b)]) | |
| print(f'vanilla: {time() - t0:.4f}') | |
| t0 = time() | |
| d = np.einsum('ij,ij->i', a, b) | |
| print(f'einsim: {time() - t0:.4f}') | |
| print(f'identical? {np.isclose(c, d).all()}') |
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
| import numpy as np | |
| from time import time | |
| a = np.random.random((100000, 3, 3)) | |
| b = np.random.random((100000, 3, 3)) | |
| # traditional pairwise dot product | |
| t0 = time() | |
| d = np.array([x @ y for x, y in zip(a, b)]) | |
| print(f"vanilla: {(time() - t0) * 1e3:.2f}ms") | |
| # use the Einstein summation | |
| t0 = time() | |
| c = np.einsum('ijk,ikl->ijl', a, b) | |
| print(f"einsum: {(time() - t0) * 1e3:.2f} ms") | |
| print("identical?", np.allclose(c, d)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The result that I got is
einsumis much faster than doing dot products inside a for loop. Maybe there is a better way but I don't know.