đ sentence-transformers/msmarco-distilbert-base-tas-b
This model maps sentences and paragraphs to a 768-dimensional dense vector space and is optimized for semantic search tasks.
đ Quick Start
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.
⨠Features
- Sentence and Paragraph Mapping: Maps sentences and paragraphs to a 768-dimensional dense vector space.
- Semantic Search Optimization: Optimized for semantic search tasks.
đĻ Installation
Using this model becomes easy when you have sentence-transformers installed:
pip install -U sentence-transformers
đģ Usage Examples
Basic Usage (Sentence-Transformers)
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"]
model = SentenceTransformer('sentence-transformers/msmarco-distilbert-base-tas-b')
query_emb = model.encode(query)
doc_emb = model.encode(docs)
scores = util.dot_score(query_emb, doc_emb)[0].cpu().tolist()
doc_score_pairs = list(zip(docs, scores))
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
for doc, score in doc_score_pairs:
print(score, doc)
Advanced 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
def cls_pooling(model_output):
return model_output.last_hidden_state[:,0]
def encode(texts):
encoded_input = tokenizer(texts, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input, return_dict=True)
embeddings = cls_pooling(model_output)
return embeddings
query = "How many people live in London?"
docs = ["Around 9 Million people live in London", "London is known for its financial district"]
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/msmarco-distilbert-base-tas-b")
model = AutoModel.from_pretrained("sentence-transformers/msmarco-distilbert-base-tas-b")
query_emb = encode(query)
doc_emb = encode(docs)
scores = torch.mm(query_emb, doc_emb.transpose(0, 1))[0].cpu().tolist()
doc_score_pairs = list(zip(docs, scores))
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
for doc, score in doc_score_pairs:
print(score, doc)
đ Documentation
For an automated evaluation of this model, see the Sentence Embeddings Benchmark: https://seb.sbert.net
đ§ Technical Details
SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: DistilBertModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': True, 'pooling_mode_mean_tokens': False, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
)
đ License
This project is licensed under the apache-2.0
license.
đ Citing & Authors
Have a look at: DistilBert TAS-B Model