onlinehd.spatial.cos_cdist

onlinehd.spatial.cos_cdist(x1: torch.Tensor, x2: torch.Tensor, eps: float = 1e-08)

Computes pairwise cosine similarity between samples in x1 and x2, forcing each point l2-norm to be at least eps. This similarity between (n?, f?) samples described in \(x1\) and the (m?, f?) samples described in \(x2\) with scalar \(\varepsilon > 0\) is the (n?, m?) matrix \(\delta\) given by:

\[\delta_{ij} = \frac{x1_i \cdot x2_j}{\max\{\|x1_i\|, \varepsilon\} \max\{\|x2_j\|, \varepsilon\}}\]
Parameters
  • x1 (torch.Tensor) – The (n?, f?) sized matrix of datapoints to score with x2.

  • x2 (torch.Tensor) – The (m?, f?) sized matrix of datapoints to score with x1.

  • eps (float, > 0) – Scalar to prevent zero-norm vectors.

Returns

The (n?, m?) sized tensor dist where dist[i,j] = cos(x1[i], x2[j]) given by the equation above.

Return type

torch.Tensor