Last active
August 30, 2022 12:29
-
-
Save Emekaborisama/8985bc50c56e1c6e89749818c75dc6cb to your computer and use it in GitHub Desktop.
load huggingface model
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,util | |
from transformers import AutoTokenizer, AutoModel | |
import torch | |
import torch.nn.functional as F | |
#Mean Pooling - Take attention mask into account for correct averaging | |
def mean_pooling(model_output, attention_mask): | |
token_embeddings = model_output[0] | |
print(token_embeddings) | |
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float() | |
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9) | |
# Load model from HuggingFace Hub | |
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L12-v2') | |
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L12-v2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment