This is a port of the
DistilBert TAS-B Model
to
sentence-transformers
model: It maps sentences & paragraphs to a 768 dimensional dense vector space and is optimized for the task of semantic search.
from sentence_transformers import SentenceTransformer, util
query = "How many people live in London?"
docs = ["Around 9 Million people live in London", "London is known for its financial district"]
#Load the model
model = SentenceTransformer('sentence-transformers/msmarco-distilbert-base-tas-b')
#Encode query and documents
query_emb = model.encode(query)
doc_emb = model.encode(docs)
#Compute dot score between query and all document embeddings
scores = util.dot_score(query_emb, doc_emb)[0].cpu().tolist()
#Combine docs & scores
doc_score_pairs = list(zip(docs, scores))
#Sort by decreasing score
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
#Output passages & scoresfor doc, score in doc_score_pairs:
print(score, doc)
Usage (HuggingFace Transformers)
Without
sentence-transformers
, 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.
from transformers import AutoTokenizer, AutoModel
import torch
#CLS Pooling - Take output from first tokendefcls_pooling(model_output):
return model_output.last_hidden_state[:,0]
#Encode textdefencode(texts):
# Tokenize sentences
encoded_input = tokenizer(texts, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddingswith torch.no_grad():
model_output = model(**encoded_input, return_dict=True)
# Perform pooling
embeddings = cls_pooling(model_output)
return embeddings
# Sentences we want sentence embeddings for
query = "How many people live in London?"
docs = ["Around 9 Million people live in London", "London is known for its financial district"]
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/msmarco-distilbert-base-tas-b")
model = AutoModel.from_pretrained("sentence-transformers/msmarco-distilbert-base-tas-b")
#Encode query and docs
query_emb = encode(query)
doc_emb = encode(docs)
#Compute dot score between query and all document embeddings
scores = torch.mm(query_emb, doc_emb.transpose(0, 1))[0].cpu().tolist()
#Combine docs & scores
doc_score_pairs = list(zip(docs, scores))
#Sort by decreasing score
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
#Output passages & scoresfor doc, score in doc_score_pairs:
print(score, doc)
Evaluation Results
For an automated evaluation of this model, see the
Sentence Embeddings Benchmark
:
https://seb.sbert.net
msmarco-distilbert-base-tas-b huggingface.co is an AI model on huggingface.co that provides msmarco-distilbert-base-tas-b's model effect (), which can be used instantly with this sentence-transformers msmarco-distilbert-base-tas-b model. huggingface.co supports a free trial of the msmarco-distilbert-base-tas-b model, and also provides paid use of the msmarco-distilbert-base-tas-b. Support call msmarco-distilbert-base-tas-b model through api, including Node.js, Python, http.
msmarco-distilbert-base-tas-b huggingface.co is an online trial and call api platform, which integrates msmarco-distilbert-base-tas-b's modeling effects, including api services, and provides a free online trial of msmarco-distilbert-base-tas-b, you can try msmarco-distilbert-base-tas-b online for free by clicking the link below.
sentence-transformers msmarco-distilbert-base-tas-b online free url in huggingface.co:
msmarco-distilbert-base-tas-b is an open source model from GitHub that offers a free installation service, and any user can find msmarco-distilbert-base-tas-b on GitHub to install. At the same time, huggingface.co provides the effect of msmarco-distilbert-base-tas-b install, users can directly use msmarco-distilbert-base-tas-b installed effect in huggingface.co for debugging and trial. It also supports api for free installation.
msmarco-distilbert-base-tas-b install url in huggingface.co: