cosine_similarity

torchhd.cosine_similarity(input: VSATensor, others: VSATensor, **kwargs) VSATensor[source]

Cosine similarity between the input vector and each vector in others.

Aliased as torchhd.cos.

Parameters:
  • input (Tensor) – hypervectors to compare against others

  • others (Tensor) – hypervectors to compare with

Shapes:
  • Input: \((*, d)\)

  • Others: \((n, d)\) or \((d)\)

  • Output: \((*, n)\) or \((*)\), depends on shape of others

Note

Output dtype is torch.get_default_dtype().

Examples:

>>> x = torchhd.random(3, 6)
>>> x
tensor([[ 1., -1., -1., -1.,  1., -1.],
        [-1., -1.,  1., -1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1., -1.]])
>>> torchhd.cosine_similarity(x, x)
tensor([[ 1.0000,  0.0000,  0.0000],
        [ 0.0000,  1.0000, -0.3333],
        [ 0.0000, -0.3333,  1.0000]])

>>> x = torchhd.random(3, 6, "FHRR")
>>> x
tensor([[ 0.986+0.166j,  0.886+0.463j,  0.205+0.978j,  0.952+0.304j,  0.923+0.384j, -0.529+0.848j],
        [-0.293+0.956j,  0.965+0.259j,  0.999-0.023j, -0.665-0.746j,  0.451-0.892j, -0.082+0.996j],
        [-0.991-0.127j, -0.326-0.945j,  0.785+0.618j,  0.518-0.855j,  0.149+0.988j,  0.020-0.999j]])
>>> torchhd.cosine_similarity(x, x)
tensor([[ 1.0000,  0.1884, -0.1779],
        [ 0.1884,  1.0000, -0.1900],
        [-0.1779, -0.1900,  1.0000]])