The GTE models are trained by Alibaba DAMO Academy. They are mainly based on the BERT framework and currently offer three different sizes of models, including GTE-large, GTE-base, and GTE-small. The GTE models are trained on a large-scale corpus of relevance text pairs, covering a wide range of domains and scenarios. This enables the GTE models to be applied to various downstream tasks of text embeddings, including information retrieval, semantic textual similarity, text reranking, etc.
Metrics
We compared the performance of the GTE models with other popular text embedding models on the MTEB benchmark. For more detailed comparison results, please refer to the MTEB leaderboard.
1import torch.nn.functional as F
2from torch import Tensor
3from transformers import AutoTokenizer, AutoModel
45defaverage_pool(last_hidden_states: Tensor,6 attention_mask: Tensor)-> Tensor:7 last_hidden = last_hidden_states.masked_fill(~attention_mask[...,None].bool(),0.0)8return last_hidden.sum(dim=1)/ attention_mask.sum(dim=1)[...,None]910input_texts =[11"what is the capital of China?",12"how to implement quick sort in python?",13"Beijing",14"sorting algorithms"15]1617tokenizer = AutoTokenizer.from_pretrained("thenlper/gte-large")18model = AutoModel.from_pretrained("thenlper/gte-large")1920# Tokenize the input texts21batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')2223outputs = model(**batch_dict)24embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])2526# (Optionally) normalize embeddings27embeddings = F.normalize(embeddings, p=2, dim=1)28scores =(embeddings[:1] @ embeddings[1:].T)*10029print(scores.tolist())
Use with sentence-transformers:
python
1from sentence_transformers import SentenceTransformer
2from sentence_transformers.util import cos_sim
34sentences =['That is a happy person','That is a very happy person']56model = SentenceTransformer('thenlper/gte-large')7embeddings = model.encode(sentences)8print(cos_sim(embeddings[0], embeddings[1]))
Limitation
This model exclusively caters to English texts, and any lengthy texts will be truncated to a maximum of 512 tokens.
Citation
If you find our paper or models helpful, please consider citing them as follows:
@article{li2023towards,
title={Towards general text embeddings with multi-stage contrastive learning},
author={Li, Zehan and Zhang, Xin and Zhang, Yanzhao and Long, Dingkun and Xie, Pengjun and Zhang, Meishan},
journal={arXiv preprint arXiv:2308.03281},
year={2023}
}