This is a sentence-transformers model: It maps sentences & paragraphs to a 384 dimensional dense vector space and can be used for tasks like clustering or semantic search.
Using this model becomes easy when you have sentence-transformers installed:
pip install -U sentence-transformers
Then you can use the model like this:
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('sentence-transformers/paraphrase-MiniLM-L6-v2')
embeddings = model.encode(sentences)
print(embeddings)
Without sentence-transformers, you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.
from transformers import AutoTokenizer, AutoModel
import torch
#Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #First element of model_output contains all 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)
# Sentences we want sentence embeddings for
sentences = ['This is an example sentence', 'Each sentence is converted']
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/paraphrase-MiniLM-L6-v2')
model = AutoModel.from_pretrained('sentence-transformers/paraphrase-MiniLM-L6-v2')
# Tokenize sentences
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. In this case, max pooling.
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
print("Sentence embeddings:")
print(sentence_embeddings)
For an automated evaluation of this model, see the Sentence Embeddings Benchmark: https://seb.sbert.net
SentenceTransformer(
(0): Transformer({'max_seq_length': 128, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 384, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
)
This model was trained by sentence-transformers.
If you find this model helpful, feel free to cite our publication Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks:
@inproceedings{reimers-2019-sentence-bert,
title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
author = "Reimers, Nils and Gurevych, Iryna",
booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
month = "11",
year = "2019",
publisher = "Association for Computational Linguistics",
url = "http://arxiv.org/abs/1908.10084",
}
checkpoint-1148/optimizer.pt 172 MiBcheckpoint-1435/optimizer.pt 172 MiBcheckpoint-1722/optimizer.pt 172 MiBcheckpoint-2009/optimizer.pt 172 MiBcheckpoint-2296/optimizer.pt 172 MiBcheckpoint-287/optimizer.pt 172 MiBcheckpoint-574/optimizer.pt 172 MiBcheckpoint-8323/optimizer.pt 172 MiBcheckpoint-861/optimizer.pt 172 MiBcheckpoint-8610/optimizer.pt 172 MiBcheckpoint-8897/optimizer.pt 172 MiBcheckpoint-9184/optimizer.pt 172 MiBcheckpoint-1148/model.safetensors 87 MiBcheckpoint-1435/model.safetensors 87 MiBcheckpoint-1722/model.safetensors 87 MiBcheckpoint-2009/model.safetensors 87 MiBcheckpoint-2296/model.safetensors 87 MiBcheckpoint-287/model.safetensors 87 MiBcheckpoint-574/model.safetensors 87 MiBcheckpoint-8323/model.safetensors 87 MiBcheckpoint-861/model.safetensors 87 MiBcheckpoint-8610/model.safetensors 87 MiBcheckpoint-8897/model.safetensors 87 MiBcheckpoint-9184/model.safetensors 87 MiBmodel.safetensors 87 MiBcheckpoint-1148/rng_state.pth 13.9 KiBcheckpoint-1435/rng_state.pth 13.9 KiBcheckpoint-1722/rng_state.pth 13.9 KiBcheckpoint-2009/rng_state.pth 13.9 KiBcheckpoint-2296/rng_state.pth 13.9 KiBcheckpoint-287/rng_state.pth 13.9 KiBcheckpoint-574/rng_state.pth 13.9 KiBcheckpoint-8323/rng_state.pth 13.9 KiBcheckpoint-861/rng_state.pth 13.9 KiBcheckpoint-8610/rng_state.pth 13.9 KiBcheckpoint-8897/rng_state.pth 13.9 KiBcheckpoint-9184/rng_state.pth 13.9 KiBcheckpoint-1148/training_args.bin 4.7 KiBcheckpoint-1435/training_args.bin 4.7 KiBcheckpoint-1722/training_args.bin 4.7 KiBcheckpoint-2009/training_args.bin 4.7 KiBcheckpoint-2296/training_args.bin 4.7 KiBcheckpoint-287/training_args.bin 4.7 KiBcheckpoint-574/training_args.bin 4.7 KiBcheckpoint-8323/training_args.bin 4.7 KiBcheckpoint-861/training_args.bin 4.7 KiBcheckpoint-8610/training_args.bin 4.7 KiBcheckpoint-8897/training_args.bin 4.7 KiBcheckpoint-9184/training_args.bin 4.7 KiBcheckpoint-1148/scheduler.pt 1 KiBcheckpoint-1435/scheduler.pt 1 KiBcheckpoint-1722/scheduler.pt 1 KiBcheckpoint-2009/scheduler.pt 1 KiBcheckpoint-2296/scheduler.pt 1 KiBcheckpoint-287/scheduler.pt 1 KiBcheckpoint-574/scheduler.pt 1 KiBcheckpoint-8323/scheduler.pt 1 KiBcheckpoint-861/scheduler.pt 1 KiBcheckpoint-8610/scheduler.pt 1 KiBcheckpoint-8897/scheduler.pt 1 KiBcheckpoint-9184/scheduler.pt 1 KiB