sentence similarity python

Solutions on MaxInterview for sentence similarity python by the best coders in the world

showing results for - "sentence similarity python"
Allison
14 Feb 2017
1# sentence_transformer: Python module using Sentence BERT 
2# check documentation in source link for further details
3from sentence_transformers import SentenceTransformer
4from sentence_transformers.util import pytorch_cos_sim # cosine silarity
5
6# stsb-roberta-large is one of the possible pre-trained models
7model = SentenceTransformer('stsb-roberta-large')
8
9# list of sentences (type: str)
10sentences = list(...) 
11
12# calculate the embeddings
13embeddings = bert_model.encode(sentences)
14
15# example: cosine similarity among first and second sentences
16cos_similarity = pytorch_cos_sim(embeddings[0], embeddings[1])