UKPLab / sentence-transformers

Multilingual Sentence & Image Embeddings with BERT
https://www.SBERT.net
Apache License 2.0
14.36k stars 2.39k forks source link

Outdated doc for model.similarity? #2769

Open OmarShehata opened 2 weeks ago

OmarShehata commented 2 weeks ago

Using version 2.2.2. Several places in the docs show computing similarity like this (example here https://www.sbert.net/index.html)

# 3. Calculate the embedding similarities
similarities = model.similarity(embeddings, embeddings)

however, this produces an error:

AttributeError: 'SentenceTransformer' object has no attribute 'similarity'

I think the correct form is:

from sentence_transformers import util
similarities = util.pytorch_cos_sim(embeddings, embeddings)

I can open a PR to fix this across the docs, but wanted to check in first that this is intentional/expected. It does appear to be defined here:

https://github.com/UKPLab/sentence-transformers/blob/e5c15a51a72a5432370c4daa1d0ef7be67b4ce50/sentence_transformers/SentenceTransformer.py#L607

tomaarsen commented 2 weeks ago

Hello!

With version 2.2.2, your approach is indeed the correct one. However, since 3.0.0 there is a new similarity method to abstract away the similarity score computation. Behind the scenes, it still does cosine similarity by default. The documentation and examples have been updated for the most recent sentence-transformers version.