Replies: 1 comment 2 replies
-
Hi @ydalmia, there are a number of ways to accomplish this, the simplest one is: import numpy as np
from einops import rearrange, reduce
x = np.random.randn(1000, 32)
y = np.random.randn(500, 32)
diff = rearrange(x, 'i c -> i 1 c') - rearrange(y, 'j c -> 1 j c')
sq_distances = reduce(diff ** 2, 'i j c -> i j', 'sum') Here is how you can verify result: from scipy.spatial.distance import cdist
cdist_result = cdist(x, y, metric='sqeuclidean')
assert np.allclose(sq_distances, cdist_result) Code in einops will work for pytorch, tensorflow, jax, etc - just use tensors from corresponding frameworks |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
Let me start by saying this is a super cool package! Out of curiosity, I was wondering how one could use Einops to get the squared euclidian distance matrix between two lists of d-dimensional points. For example, if A had 1000 points, each with 32 dimensions, (so shape (1000, 32) and B had 500 points, ie shape (500, 32), then the result would be (5000, 500) with the squared euclidian distance between each point in A and each point in B.
I know that scipy cdist can perform this calculation, but is there a way to use Einops?
Beta Was this translation helpful? Give feedback.
All reactions