tf.keras.ops.cdist

Computes pairwise distances between two collections of vectors.

This function computes the Euclidean distance between each pair of the two collections of inputs.

x Tensor of shape (..., m, d).
y Tensor of shape (..., n, d).

A tensor of shape (..., m, n) with the pairwise distances.

Example:

x = keras.ops.convert_to_tensor([[0.0, 0.0], [1.0, 1.0]])
y = keras.ops.convert_to_tensor([[1.0, 0.0], [0.0, 1.0]])
keras.ops.cdist(x, y)
array([[1.       , 1.       ],
       [1.       , 1.4142135]], dtype=float32)