intfloat/e5-base-unsupervised
323k
1---2tags:3- Sentence Transformers4- sentence-similarity5- sentence-transformers6language:7- en8license: mit9---10# E5-base-unsupervised11 12**This model is similar to [e5-base](https://huggingface.co/intfloat/e5-base) but without supervised fine-tuning.**13 14[Text Embeddings by Weakly-Supervised Contrastive Pre-training](https://arxiv.org/pdf/2212.03533.pdf).15Liang Wang, Nan Yang, Xiaolong Huang, Binxing Jiao, Linjun Yang, Daxin Jiang, Rangan Majumder, Furu Wei, arXiv 202216 17This model has 12 layers and the embedding size is 768.18 19## Usage20 21Below is an example to encode queries and passages from the MS-MARCO passage ranking dataset.22 23```python24import torch.nn.functional as F25 26from torch import Tensor27from transformers import AutoTokenizer, AutoModel28 29 30def average_pool(last_hidden_states: Tensor,31 attention_mask: Tensor) -> Tensor:32 last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)33 return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]34 35 36# Each input text should start with "query: " or "passage: ".37# For tasks other than retrieval, you can simply use the "query: " prefix.38input_texts = ['query: how much protein should a female eat',39 'query: summit define',40 "passage: As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",41 "passage: Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments."]42 43tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-base-unsupervised')44model = AutoModel.from_pretrained('intfloat/e5-base-unsupervised')45 46# Tokenize the input texts47batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')48 49outputs = model(**batch_dict)50embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])51 52# normalize embeddings53embeddings = F.normalize(embeddings, p=2, dim=1)54scores = (embeddings[:2] @ embeddings[2:].T) * 10055print(scores.tolist())56```57 58## Training Details59 60Please refer to our paper at [https://arxiv.org/pdf/2212.03533.pdf](https://arxiv.org/pdf/2212.03533.pdf).61 62## Benchmark Evaluation63 64Check out [unilm/e5](https://github.com/microsoft/unilm/tree/master/e5) to reproduce evaluation results 65on the [BEIR](https://arxiv.org/abs/2104.08663) and [MTEB benchmark](https://arxiv.org/abs/2210.07316).66 67## Support for Sentence Transformers68 69Below is an example for usage with sentence_transformers.70```python71from sentence_transformers import SentenceTransformer72model = SentenceTransformer('intfloat/e5-base-unsupervised')73input_texts = [74 'query: how much protein should a female eat',75 'query: summit define',76 "passage: As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",77 "passage: Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments."78]79embeddings = model.encode(input_texts, normalize_embeddings=True)80```81 82Package requirements83 84`pip install sentence_transformers~=2.2.2`85 86Contributors: [michaelfeil](https://huggingface.co/michaelfeil)87 88## FAQ89 90**1. Do I need to add the prefix "query: " and "passage: " to input texts?**91 92Yes, this is how the model is trained, otherwise you will see a performance degradation.93 94Here are some rules of thumb:95- Use "query: " and "passage: " correspondingly for asymmetric tasks such as passage retrieval in open QA, ad-hoc information retrieval.96 97- Use "query: " prefix for symmetric tasks such as semantic similarity, paraphrase retrieval.98 99- Use "query: " prefix if you want to use embeddings as features, such as linear probing classification, clustering100 101**2. Why are my reproduced results slightly different from reported in the model card?**102 103Different versions of `transformers` and `pytorch` could cause negligible but non-zero performance differences.104 105## Citation106 107If you find our paper or models helpful, please consider cite as follows:108 109```110@article{wang2022text,111 title={Text Embeddings by Weakly-Supervised Contrastive Pre-training},112 author={Wang, Liang and Yang, Nan and Huang, Xiaolong and Jiao, Binxing and Yang, Linjun and Jiang, Daxin and Majumder, Rangan and Wei, Furu},113 journal={arXiv preprint arXiv:2212.03533},114 year={2022}115}116```117 118## Limitations119 120This model only works for English texts. Long texts will be truncated to at most 512 tokens.121 