Is this the right way to compute cosine similarity in PyTroch?
Like with most indexing in python, -1 refers to last dimension (-2 would be second-to-last, etc...). Using dim=-1
when initializing cosine similarity means that cosine similarity will be computed along the last dimension of the inputs.
For example, if b
and c
were 3-dimensional tensors with size [X,Y,Z]
, then the result would be a 2-dimensional tensor of size [X,Y]
. In your case, since the input tensors only have one dimension (size [3]
), you end up getting a result tensor of size []
, i.e. a scalar.