# How to use CosineSimilarity function in right way?

I’m confused the document of CosineSimilarity. A similarity can be computed between two objects. If we have a hundred of vectors then the total number of similarities is one hundred by one hundred. But, the size of the output in the document is one hundred. I don’t understand what it means…

Btw, in my case I have two matrices, the shape of first matrix is (N, C) and the shape of second matrix is (M, C) in row vectors. In this case, how can I compute a similarity matrix between two matrices of which size is (N, M) using CosineSimilarity function?

To compute cosine similarity between two vectors `x, y` you need to have both vectors same size as you can see from the example in the same page you linked in your comment:

``````>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> cos = nn.CosineSimilarity(dim)
``````

in your case you have `N =/= M` so that would be challenging if you use the built in cosine function in pytorch.

You could instead calculate the cosine as follows: