'How to compute the cosine similarity between a 3D-tensor and a vector?

I want to calculate the cosine-similarity between a 3D tensor x:

torch.Size([119, 768, 51])

and the vector y

torch.Size([768])

This should of course result in this 2D Matrix:

torch.Size([119, 51])

Using sklearn.metrics.pairwise.cosine_similarity gives me the error:

*** ValueError: Found array with dim 3. check_pairwise_arrays expected <= 2.

How do I accomplish this?



Solution 1:[1]

Firstly, do a transpose and reshape to obtain a tensor of size

torch.Size([119*51, 768])

Secondly, expand the first dimension of y

torch.Size([1, 768])

Finally, compute similarity then reshape results to expected size:

torch.Size([119*51, 1])
torch.Size([119, 51])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cao-nv