Skip to content

Instantly share code, notes, and snippets.

@kastnerkyle
Last active June 11, 2022 17:13
Show Gist options
  • Save kastnerkyle/7a985f63c1ba92ac8bca to your computer and use it in GitHub Desktop.
Save kastnerkyle/7a985f63c1ba92ac8bca to your computer and use it in GitHub Desktop.
Example of pairwise processing with "smart broadcasting"
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