Instructions to use sentence-transformers/all-MiniLM-L6-v2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use sentence-transformers/all-MiniLM-L6-v2 with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") sentences = [ "That is a happy person", "That is a happy dog", "That is a very happy person", "Today is a sunny day" ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [4, 4] - Transformers
How to use sentence-transformers/all-MiniLM-L6-v2 with Transformers:
# Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2") model = AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2") - Inference
- Notebooks
- Google Colab
- Kaggle
Using embeddings to do sentence similarity
Has anyone used the embeddings to calculate sentence similarity like the example card? If so, what are the steps you took to do this?
This is actually a straight forward task, thanks to huggingface/sentence transformers utilities.
We just need to compare the embeddings using a similarity score utility.
Step 1: Encode the sentences to be compared
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
embeddings1 = model.encode(sentences1, convert_to_tensor=True)
embeddings2 = model.encode(sentences2, convert_to_tensor=True)
(where, sentencs1 and sentences2 are list of sentences(strings))
Step 2: Compute the similarity using a similarity matrix
(cosine similarity or dot product)
from sentence_transformers import util
cosine_scores = util.cos_sim(embeddings1, embeddings2)
Step 3: Output the pairs with their score
for i in range(len(sentences1)): print("{} \t\t {} \t\t Score: {:.4f}".format(sentences1[i], sentences2[i], cosine_scores[i][i]))
For more references, you can visit Sentence-Transformers website:
https://www.sbert.net/docs/usage/semantic_textual_similarity.html
hi