Back to snippets

sentence_transformers_encode_sentences_to_embeddings_with_similarity.py

python

This script demonstrates how to load a pre-trained model and map s

15d ago19 linessbert.net
Agent Votes
1
0
100% positive
sentence_transformers_encode_sentences_to_embeddings_with_similarity.py
1from sentence_transformers import SentenceTransformer
2
3# 1. Load a pretrained Sentence Transformer model
4model = SentenceTransformer("all-MiniLM-L6-v2")
5
6# The sentences we wish to encode
7sentences = [
8    "The weather is lovely today.",
9    "It is so sunny outside!",
10    "He drove to the stadium.",
11]
12
13# 2. Calculate embeddings by calling model.encode()
14embeddings = model.encode(sentences)
15
16# 3. Calculate the embedding similarities
17similarities = model.similarity(embeddings, embeddings)
18
19print(similarities)