CoolFace
Modelpublic

sentence-transformers/stsb-bert-base

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
1likes634downloads
README.md103 linesDownload Raw Back to root
1---2license: apache-2.03library_name: sentence-transformers4tags:5- sentence-transformers6- feature-extraction7- sentence-similarity8- transformers9pipeline_tag: sentence-similarity10---11 12**⚠️ This model is deprecated. Please don't use it as it produces sentence embeddings of low quality. You can find recommended sentence embedding models here: [SBERT.net - Pretrained Models](https://www.sbert.net/docs/pretrained_models.html)**13 14 15# sentence-transformers/stsb-bert-base16 17This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.18 19 20 21## Usage (Sentence-Transformers)22 23Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:24 25```26pip install -U sentence-transformers27```28 29Then you can use the model like this:30 31```python32from sentence_transformers import SentenceTransformer33sentences = ["This is an example sentence", "Each sentence is converted"]34 35model = SentenceTransformer('sentence-transformers/stsb-bert-base')36embeddings = model.encode(sentences)37print(embeddings)38```39 40 41 42## Usage (HuggingFace Transformers)43Without [sentence-transformers](https://www.SBERT.net), 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.44 45```python46from transformers import AutoTokenizer, AutoModel47import torch48 49 50#Mean Pooling - Take attention mask into account for correct averaging51def mean_pooling(model_output, attention_mask):52    token_embeddings = model_output[0] #First element of model_output contains all token embeddings53    input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()54    return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)55 56 57# Sentences we want sentence embeddings for58sentences = ['This is an example sentence', 'Each sentence is converted']59 60# Load model from HuggingFace Hub61tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/stsb-bert-base')62model = AutoModel.from_pretrained('sentence-transformers/stsb-bert-base')63 64# Tokenize sentences65encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')66 67# Compute token embeddings68with torch.no_grad():69    model_output = model(**encoded_input)70 71# Perform pooling. In this case, max pooling.72sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])73 74print("Sentence embeddings:")75print(sentence_embeddings)76```77 78 79 80## Full Model Architecture81```82SentenceTransformer(83  (0): Transformer({'max_seq_length': 128, 'do_lower_case': False}) with Transformer model: BertModel 84  (1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})85)86```87 88## Citing & Authors89 90This model was trained by [sentence-transformers](https://www.sbert.net/). 91        92If you find this model helpful, feel free to cite our publication [Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks](https://arxiv.org/abs/1908.10084):93```bibtex 94@inproceedings{reimers-2019-sentence-bert,95    title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",96    author = "Reimers, Nils and Gurevych, Iryna",97    booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",98    month = "11",99    year = "2019",100    publisher = "Association for Computational Linguistics",101    url = "http://arxiv.org/abs/1908.10084",102}103```