# from transformers import AutoTokenizer
model_name = "michaelfeil/ct2fast-gte-base"
model_name_orig="thenlper/gte-base"from hf_hub_ctranslate2 import EncoderCT2fromHfHub
model = EncoderCT2fromHfHub(
# load in int8 on CUDA
model_name_or_path=model_name,
device="cuda",
compute_type="int8_float16"
)
outputs = model.generate(
text=["I like soccer", "I like tennis", "The eiffel tower is in Paris"],
max_length=64,
) # perform downstream tasks on outputs
outputs["pooler_output"]
outputs["last_hidden_state"]
outputs["attention_mask"]
# alternative, use SentenceTransformer Mix-In# for end-to-end Sentence embeddings generation# (not pulling from this CT2fast-HF repo)from hf_hub_ctranslate2 import CT2SentenceTransformer
model = CT2SentenceTransformer(
model_name_orig, compute_type="int8_float16", device="cuda"
)
embeddings = model.encode(
["I like soccer", "I like tennis", "The eiffel tower is in Paris"],
batch_size=32,
convert_to_numpy=True,
normalize_embeddings=True,
)
print(embeddings.shape, embeddings)
scores = (embeddings @ embeddings.T) * 100# Hint: you can also host this code via REST API and# via github.com/michaelfeil/infinity
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
.
import torch.nn.functional as F
from torch import Tensor
from transformers import AutoTokenizer, AutoModel
defaverage_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
input_texts = [
"what is the capital of China?",
"how to implement quick sort in python?",
"Beijing",
"sorting algorithms"
]
tokenizer = AutoTokenizer.from_pretrained("thenlper/gte-base")
model = AutoModel.from_pretrained("thenlper/gte-base")
# Tokenize the input texts
batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')
outputs = model(**batch_dict)
embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
# (Optionally) normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = (embeddings[:1] @ embeddings[1:].T) * 100print(scores.tolist())
Use with sentence-transformers:
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
sentences = ['That is a happy person', 'That is a very happy person']
model = SentenceTransformer('thenlper/gte-base')
embeddings = model.encode(sentences)
print(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:
@misc{li2023general,
title={Towards General Text Embeddings with Multi-stage Contrastive Learning},
author={Zehan Li and Xin Zhang and Yanzhao Zhang and Dingkun Long and Pengjun Xie and Meishan Zhang},
year={2023},
eprint={2308.03281},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
Runs of michaelfeil ct2fast-gte-base on huggingface.co
6
Total runs
0
24-hour runs
-1
3-day runs
-1
7-day runs
-15
30-day runs
More Information About ct2fast-gte-base huggingface.co Model
ct2fast-gte-base huggingface.co is an AI model on huggingface.co that provides ct2fast-gte-base's model effect (), which can be used instantly with this michaelfeil ct2fast-gte-base model. huggingface.co supports a free trial of the ct2fast-gte-base model, and also provides paid use of the ct2fast-gte-base. Support call ct2fast-gte-base model through api, including Node.js, Python, http.
ct2fast-gte-base huggingface.co is an online trial and call api platform, which integrates ct2fast-gte-base's modeling effects, including api services, and provides a free online trial of ct2fast-gte-base, you can try ct2fast-gte-base online for free by clicking the link below.
michaelfeil ct2fast-gte-base online free url in huggingface.co:
ct2fast-gte-base is an open source model from GitHub that offers a free installation service, and any user can find ct2fast-gte-base on GitHub to install. At the same time, huggingface.co provides the effect of ct2fast-gte-base install, users can directly use ct2fast-gte-base installed effect in huggingface.co for debugging and trial. It also supports api for free installation.