Downloads · 30 days
100
14% of all-time downloads
afschowdhury/retrieval-mpnet-bn
retrieval-mpnet-bn is a sentence similarity model from afschowdhury. Use it when you need a score for how close two texts are. It is set up for sentence-transformers.
This is a sentence-transformers model: It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.
Downloads · 30 days
100
14% of all-time downloads
All-time downloads
709
Public
Repo size
2.2 GB
Likes
0
Public
Click a slice to open those files.
.bin1.1 GB · 98%
From the Hugging Face model README
retrieval-mpnet-bnThis is a sentence-transformers model: It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.
<!--- Describe your model here -->The model was fine-tuned using Multilingual Knowledge Distillation method. We selected multi-qa-mpnet-base-cos-v1 model and added a mean tokens pooling layer as the teacher model
from sentence_transformers import models, SentenceTransformer
mpnet_model = models.Transformer('sentence-transformers/multi-qa-mpnet-base-cos-v1')
pooling_model = models.Pooling(mpnet_model.get_word_embedding_dimension(),
pooling_mode_mean_tokens=True,
pooling_mode_cls_token=False,
pooling_mode_max_tokens=False)
teacher = SentenceTransformer(modules=[mpnet_model, pooling_model])
and xlm-roberta-large as the student model hence it's a multilingual model and works relatively well for Bengali .

Our model is intented to be used for semantic search: It encodes queries / questions and text paragraphs in a dense vector space. It finds relevant documents for the given passages.
Note that there is a limit of 512 word pieces: Text longer than that will be truncated. Further note that the model was just trained on input text up to 250 word pieces. It might not work well for longer text.
Using this model becomes easy when you have sentence-transformers installed:
pip install -U sentence-transformers
Then you can use the model like this:
from sentence_transformers import SentenceTransformer, util
query = "আফগানিস্তান কত রান করেছিল"
docs = ["ম্যাচটা সিকান্দার রাজারই ছিল। অন্তত রান তাড়ায় নামা শ্রীলঙ্কার ইনিংসের ১৫ ওভার পর্যন্ত অবশ্যই। কিন্তু ব্যাটে বলে দারুণ খেলা জিম্বাবুয়ে অধিনায়ককে হাসতে দিলেন না শ্রীলঙ্কার দুই অভিজ্ঞ ক্রিকেটার। অ্যাঞ্জেলো ম্যাথুস-দাসুন শানাকার সপ্তম উইকেট জুটি ম্যাচ বের করে নেয় জিম্বাবুয়ের নাগাল থেকে। ম্যাথুস অবশ্য দলকে জিতিয়ে ফিরতে পারেননি। তিনি যখন আউট হন, ২ বলে ৬ রান দরকার শ্রীলঙ্কার। দুষ্মন্ত চামিরা ৪ ও ২ রান নিয়ে শেষ বলে গড়ানো ম্যাচে জয় এনে দলকে। ",
"অক্ষর প্যাটেল ও অর্শদীপ সিংয়ের দারুণ বোলিংয়ের পর যশস্বী জয়সোয়াল ও শিবম দুবের জোড়া অর্ধশতকে ইন্দোরে সহজ জয়ে এক ম্যাচ বাকি থাকতেই সিরিজ জিতেছে ভারত। ইন্দোরে তিনে নামা গুলবদিন নাইবের ৩৫ বলে ৫৭ রানের ইনিংসে আফগানিস্তান তুলেছিল ১৭২ রান, কিন্তু ভারত সেটি পেরিয়ে গেছে ২৬ বল ও ৬ উইকেট বাকি রেখেই।",
"এদিন প্রথম থেকে আক্রমণ ও বল দখলে এগিয়ে ছিল মিসরই। প্রতিযোগিতার সবচেয়ে সফল দলটির এগিয়ে যেতে সময় লাগে মাত্র ২ মিনিট। বাঁ পাশ থেকে আসা ক্রসে সালাহ চেষ্টা করেও ঠিকঠাক সংযোগ ঘটাতে পারেননি। তবে তাঁর পায়ের ছোঁয়ায় বল আসে মোস্তফা মোহাম্মদের কাছে। ভুল করেননি এই ফরোয়ার্ড। দারুণ ফিনিশিংয়ে গোল করে এগিয়ে দেন দলকে।"]
# Load the model
model = SentenceTransformer('afschowdhury/retrieval-mpnet-bn')
# Encode the 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 & scores
for doc, score in doc_score_pairs:
print(score, doc)
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
import torch.nn.functional as F
#Mean Pooling - Take average of all tokens
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output.last_hidden_state #First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
#Encode text
def encode(texts):
# Tokenize sentences
encoded_input = tokenizer(texts, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input, return_dict=True)
# Perform pooling
embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
# Normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
return embeddings
# Sentences we want sentence embeddings for
query = "আফগানিস্তান কত রান করেছিল"
docs = ["ম্যাচটা সিকান্দার রাজারই ছিল। অন্তত রান তাড়ায় নামা শ্রীলঙ্কার ইনিংসের ১৫ ওভার পর্যন্ত অবশ্যই। কিন্তু ব্যাটে বলে দারুণ খেলা জিম্বাবুয়ে অধিনায়ককে হাসতে দিলেন না শ্রীলঙ্কার দুই অভিজ্ঞ ক্রিকেটার। অ্যাঞ্জেলো ম্যাথুস-দাসুন শানাকার সপ্তম উইকেট জুটি ম্যাচ বের করে নেয় জিম্বাবুয়ের নাগাল থেকে। ম্যাথুস অবশ্য দলকে জিতিয়ে ফিরতে পারেননি। তিনি যখন আউট হন, ২ বলে ৬ রান দরকার শ্রীলঙ্কার। দুষ্মন্ত চামিরা ৪ ও ২ রান নিয়ে শেষ বলে গড়ানো ম্যাচে জয় এনে দলকে। ",
"অক্ষর প্যাটেল ও অর্শদীপ সিংয়ের দারুণ বোলিংয়ের পর যশস্বী জয়সোয়াল ও শিবম দুবের জোড়া অর্ধশতকে ইন্দোরে সহজ জয়ে এক ম্যাচ বাকি থাকতেই সিরিজ জিতেছে ভারত। ইন্দোরে তিনে নামা গুলবদিন নাইবের ৩৫ বলে ৫৭ রানের ইনিংসে আফগানিস্তান তুলেছিল ১৭২ রান, কিন্তু ভারত সেটি পেরিয়ে গেছে ২৬ বল ও ৬ উইকেট বাকি রেখেই।",
"এদিন প্রথম থেকে আক্রমণ ও বল দখলে এগিয়ে ছিল মিসরই। প্রতিযোগিতার সবচেয়ে সফল দলটির এগিয়ে যেতে সময় লাগে মাত্র ২ মিনিট। বাঁ পাশ থেকে আসা ক্রসে সালাহ চেষ্টা করেও ঠিকঠাক সংযোগ ঘটাতে পারেননি। তবে তাঁর পায়ের ছোঁয়ায় বল আসে মোস্তফা মোহাম্মদের কাছে। ভুল করেননি এই ফরোয়ার্ড। দারুণ ফিনিশিংয়ে গোল করে এগিয়ে দেন দলকে।"]
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained("afschowdhury/retrieval-mpnet-bn")
model = AutoModel.from_pretrained("afschowdhury/retrieval-mpnet-bn")
#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 & scores
for doc, score in doc_score_pairs:
print(score, doc)
In the following some technical details how this model must be used:
| Setting | Value |
|---|---|
| Dimensions | 768 |
| Produces normalized embeddings | No |
| Pooling-Method | Mean pooling |
| Suitable score functions | dot-product (util.dot_score), cosine-similarity (util.cos_sim), or euclidean distance |
Note: When loaded with sentence-transformers, this model doesn;t produces normalized embeddings like it's base model as while training , we didn't added the normalzed layer in student model's architecture. In that case, dot-product and cosine-similarity aren't equivalent. However, for retrieval applications, the performance difference is negligible. For similarity search, we recommend to use cosine-similarity as score function.
<!-- write a background section --> <!-- write about training data and training procedure and losses -->SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: XLMRobertaModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
)
Asif Faisal Chowdhury
E-mail: [email protected] | Linked-in: afschowdhury