Compute the matrix of squared Euclidean distances and the matrix of cosine similarities between every point of a and every point of b.
For point ai (row i of a) and point bj (row j of b),
Sij=d=1∑D(aid−bjd)2,Cij=max(∥ai∥∥bj∥, 10−8)ai⋅bj,where ai⋅bj is the dot product and ∥ai∥ is the Euclidean norm, the square root of ai⋅ai. The floor of 10−8 makes Cij equal to 0 whenever either point is the zero vector. The input a has shape (N, D) and b has shape (M, D).
Return a dict with two float32 JAX arrays of shape (N, M), sq_dist holding S and cosine holding C, where row i and column j hold the values for ai and bj.
Input: a = [[1,0],[0,2]], b = [[1,0],[0,1],[-1,-1]]
Output: {"sq_dist":[[0.0,2.0,5.0],[5.0,1.0,10.0]],"cosine":[[1.0,0.0,-0.707107],[0.0,1.0,-0.707107]]}
Explanation: Point (0, 2) of a and point (-1, -1) of b differ by (1, 3), so their squared distance is 1 + 9 = 10. Their dot product is -2 and their norms are 2 and 1.414214, so their cosine is -2 / 2.828427 = -0.707107.
Input: a = [[0,0,0],[1,2,2]], b = [[2,4,4],[0,0,0]]
Output: {"sq_dist":[[36.0,0.0],[9.0,9.0]],"cosine":[[0.0,0.0],[1.0,0.0]]}
Input: a = [[3,4],[-3,-4],[0.5,0]], b = [[3,4]]
Output: {"sq_dist":[[0.0],[100.0],[22.25]],"cosine":[[1.0],[-1.0],[0.6]]}
jax.vmap(f, in_axes=(None, 0)) maps over rows of b with one row of a.
jax.vmap(jax.vmap(f, in_axes=(None, 0)), in_axes=(0, None)) gives an (N, M) result.
If f returns (d1, d2), vmap returns two batched arrays.
Accepts: array
Accepts: array
Compute the matrix of squared Euclidean distances and the matrix of cosine similarities between every point of a and every point of b.
For point ai (row i of a) and point bj (row j of b),
Sij=d=1∑D(aid−bjd)2,Cij=max(∥ai∥∥bj∥, 10−8)ai⋅bj,where ai⋅bj is the dot product and ∥ai∥ is the Euclidean norm, the square root of ai⋅ai. The floor of 10−8 makes Cij equal to 0 whenever either point is the zero vector. The input a has shape (N, D) and b has shape (M, D).
Return a dict with two float32 JAX arrays of shape (N, M), sq_dist holding S and cosine holding C, where row i and column j hold the values for ai and bj.
Input: a = [[1,0],[0,2]], b = [[1,0],[0,1],[-1,-1]]
Output: {"sq_dist":[[0.0,2.0,5.0],[5.0,1.0,10.0]],"cosine":[[1.0,0.0,-0.707107],[0.0,1.0,-0.707107]]}
Explanation: Point (0, 2) of a and point (-1, -1) of b differ by (1, 3), so their squared distance is 1 + 9 = 10. Their dot product is -2 and their norms are 2 and 1.414214, so their cosine is -2 / 2.828427 = -0.707107.
Input: a = [[0,0,0],[1,2,2]], b = [[2,4,4],[0,0,0]]
Output: {"sq_dist":[[36.0,0.0],[9.0,9.0]],"cosine":[[0.0,0.0],[1.0,0.0]]}
Input: a = [[3,4],[-3,-4],[0.5,0]], b = [[3,4]]
Output: {"sq_dist":[[0.0],[100.0],[22.25]],"cosine":[[1.0],[-1.0],[0.6]]}
jax.vmap(f, in_axes=(None, 0)) maps over rows of b with one row of a.
jax.vmap(jax.vmap(f, in_axes=(None, 0)), in_axes=(0, None)) gives an (N, M) result.
If f returns (d1, d2), vmap returns two batched arrays.
Accepts: array
Accepts: array