Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sentence similarity python

# sentence_transformer: Python module using Sentence BERT 
# check documentation in source link for further details
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import pytorch_cos_sim # cosine silarity

# stsb-roberta-large is one of the possible pre-trained models
model = SentenceTransformer('stsb-roberta-large')

# list of sentences (type: str)
sentences = list(...) 

# calculate the embeddings
embeddings = bert_model.encode(sentences)

# example: cosine similarity among first and second sentences
cos_similarity = pytorch_cos_sim(embeddings[0], embeddings[1])
Comment

PREVIOUS NEXT
Code Example
Python :: select default option django form 
Python :: python tkinter code example 
Python :: overriding update in serializer django 
Python :: django queryset to list 
Python :: for loop to convert a list to lowercase 
Python :: save model pytorch 
Python :: compare two dates python 
Python :: wikipedia python module 
Python :: blender show python version 
Python :: Set symmetric Using Python Set symmetric_difference() Method 
Python :: how to check if value is in list python 
Python :: numpy generate random array 
Python :: Insurance codechef solution 
Python :: python argument parser default value 
Python :: Invalid comparison between dtype=datetime64[ns] and date filter 
Python :: how to convert datetime to integer in python 
Python :: python function with two parameters 
Python :: tf MaxPooling2D 
Python :: find a key in a dictionary python 
Python :: pytorch dataloader 
Python :: how to separate url from text in python 
Python :: python qt always on top 
Python :: how to create a virtual environment in python 
Python :: python get first letter of string 
Python :: python password generation 
Python :: last element of list python 
Python :: histogram seaborn python 
Python :: change the side of the axis plt python 
Python :: python how to add to a list 
Python :: python regex search a words among list 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =