Last active
June 11, 2022 17:13
-
-
Save kastnerkyle/7a985f63c1ba92ac8bca to your computer and use it in GitHub Desktop.
Example of pairwise processing with "smart broadcasting"
This file contains 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 | |
def exponential_kernel(x1, x2): | |
# Broadcasting tricks to get every pairwise distance. | |
return np.exp(-((x1[np.newaxis, :, :] - x2[:, np.newaxis, :]) ** 2).sum(2)).T | |
a = np.random.randn(100, 5) | |
print(exponential_kernel(a, a).shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment