Last active
August 30, 2022 12:33
-
-
Save Emekaborisama/adcc96fb1e2c1726388b458c7f66c2dd to your computer and use it in GitHub Desktop.
transformers_inference
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
# Sentences we want sentence embeddings for | |
sentences = ['This is an example sentence', 'This is sample of the sentence'] | |
import time | |
start = time.time() | |
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt') | |
# Compute token embeddings | |
with torch.no_grad(): | |
model_output = model(**encoded_input) | |
# Perform pooling | |
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask']) | |
# Normalize embeddings | |
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1) | |
#calculate similarity | |
cosine_scores = util.pytorch_cos_sim(sentence_embeddings[0], sentence_embeddings[1]) | |
cosine_scores | |
end = time.time() | |
print(end - start) | |
print(f"pytorch vanilla cpu: {(end- start)/2:.2f}s/sequence") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment