Sentence-transformers: Looking for fast CPU based Cosine similarity function with python.

Created on 1 Oct 2020  路  3Comments  路  Source: UKPLab/sentence-transformers

Hi,
I am looking for a fast CPU based cosine similarity function with python. I am sure you evaluated different functions and methods. Could you give me an hint?

Thanks
Philip

Most helpful comment

Yes, I usually use that method on CPU and it works great.

Correct, storing unit length vector would reduce the operation to a simple torch.mm()

All 3 comments

Hi Philip,
I think this function is also quite quick on CPUs:
https://github.com/UKPLab/sentence-transformers/blob/master/sentence_transformers/util.py#L13

the trick is to normalize the embeddings to unit length. Then, cosine similarity is just the dot product.

For dot product, you can try pytorch vs. numpy vs. tensorflow (if you like).

For me, pytorch worked faster than numpy. Hence, I added pytorch_cos_sim to the util file.

Another trick can be to combine the embeddings to matrices. So if you want to compute cosine_similarity between a_i and b_i for all i, it can make sense to combine all a's to matrix A and all b's to matrix B. Then you can compute cosine_similarity(A, B) and the diagonal gives you the pairwise scores.

Best
Nils

Awsome! Thanks.
With pytorch you mean pytorch on CPU in this case - right?

PS: To make it even more efficient I could preprocess all my embeddings to be in unit length and then remove this unit len. computation from the function. That way I would be even fast when working on the same dataset multiple times. Because I would only have to compute unit len. once...

Yes, I usually use that method on CPU and it works great.

Correct, storing unit length vector would reduce the operation to a simple torch.mm()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FrancescoSaverioZuppichini picture FrancescoSaverioZuppichini  路  3Comments

mchari picture mchari  路  4Comments

lagatorc picture lagatorc  路  4Comments

naserahmadi picture naserahmadi  路  3Comments

umairspn picture umairspn  路  5Comments