Last active
March 22, 2021 07:39
-
-
Save purva91/1775c4d702f0cbd5cddce4717f6aa244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sentence_transformers import SentenceTransformer | |
sbert_model = SentenceTransformer('bert-base-nli-mean-tokens') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sentence_embeddings = model.encode(sentences) | |
#print('Sample BERT embedding vector - length', len(sentence_embeddings[0])) | |
#print('Sample BERT embedding vector - note includes negative values', sentence_embeddings[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query = "I had pizza and pasta" | |
query_vec = model.encode([query])[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for sent in sentences: | |
sim = cosine(query_vec, model.encode([sent])[0]) | |
print("Sentence = ", sent, "; similarity = ", sim) |
NameError: name 'cosine' is not defined
Correct one is:
from scipy.spatial import distance
for sent in sentences:
sim = distance.cosine(query_vec, model.encode([sent])[0])
print("Sentence = ", sent, "; similarity (close is good) = ", sim)
from sentence_transformers import SentenceTransformer
sbert_model = SentenceTransformer('bert-base-nli-mean-tokens')
to
model = SentenceTransformer('bert-base-nli-mean-tokens')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi dear. .encode function is not working.
Error is : ----> 'Doc2Vec' object has no attribute 'encode'