Using the model directly available in HuggingFace transformers requires to add a mean pooling operation to obtain a sentence embedding.
import torch
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained('facebook/contriever')
model = AutoModel.from_pretrained('facebook/contriever')
sentences = [
"Where was Marie Curie born?",
"Maria Sklodowska, later known as Marie Curie, was born on November 7, 1867.",
"Born in Paris on 15 May 1859, Pierre Curie was the son of Eugène Curie, a doctor of French Catholic origin from Alsace."
]
# Apply tokenizer
inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddings
outputs = model(**inputs)
# Mean poolingdefmean_pooling(token_embeddings, mask):
token_embeddings = token_embeddings.masked_fill(~mask[..., None].bool(), 0.)
sentence_embeddings = token_embeddings.sum(dim=1) / mask.sum(dim=1)[..., None]
return sentence_embeddings
embeddings = mean_pooling(outputs[0], inputs['attention_mask'])
Runs of facebook contriever on huggingface.co
8.0M
Total runs
0
24-hour runs
364.9K
3-day runs
573.0K
7-day runs
415.1K
30-day runs
More Information About contriever huggingface.co Model
contriever huggingface.co
contriever huggingface.co is an AI model on huggingface.co that provides contriever's model effect (), which can be used instantly with this facebook contriever model. huggingface.co supports a free trial of the contriever model, and also provides paid use of the contriever. Support call contriever model through api, including Node.js, Python, http.
contriever huggingface.co is an online trial and call api platform, which integrates contriever's modeling effects, including api services, and provides a free online trial of contriever, you can try contriever online for free by clicking the link below.
facebook contriever online free url in huggingface.co:
contriever is an open source model from GitHub that offers a free installation service, and any user can find contriever on GitHub to install. At the same time, huggingface.co provides the effect of contriever install, users can directly use contriever installed effect in huggingface.co for debugging and trial. It also supports api for free installation.