speechbrain.utils.distances module

Distance metrics and related functions

Summary

Functions:

cosine_similarity_matrix

Computes a matrix evaluating all pairwise cosine similarities.

Reference

speechbrain.utils.distances.cosine_similarity_matrix(a: Tensor, b: Tensor, eps: float = 1e-08) Tensor[source]

Computes a matrix evaluating all pairwise cosine similarities. The cosine similarity can otherwise be determined with torch.nn.CosineSimilarity.

Parameters:
  • a (torch.Tensor) – Tensor of shape [..., X, dim] where dim is the dimension where the cosine similarity will be computed and X is any value >= 0.

  • b (torch.Tensor) – Tensor of shape [..., Y, dim], where other dimensions are otherwise identical to a’s and Y is any value >= 0.

  • eps (float) – Epsilon value for numerical stability, in order to avoid a division by zero. Does not significantly affect results.

Returns:

Tensor of shape [..., X, Y] living on the same device and dtype as the input tensors. e.g. ignoring first dimensions out[3, 0] would be the cosine similarity of a[3] and b[0].

Return type:

torch.Tensor