MPA/sambert
2205
1---2library_name: sentence-transformers3pipeline_tag: sentence-similarity4tags:5- sentence-transformers6- feature-extraction7- sentence-similarity8- transformers9language:10- he11---12 13# Sambert - embeddings model for Hebrew14 15This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.16 17<!--- Describe your model here -->18 19## Usage (Sentence-Transformers)20sentence-transformer for Hebrew21 22Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:23 24```25pip install -U sentence-transformers26```27 28Then you can use the model like this:29 30```python31from sentence_transformers import SentenceTransformer, util32sentences = ["אמא הלכה לגן", "אבא הלך לגן", "ירקוני קונה לנו פיצות"]33 34model = SentenceTransformer('MPA/sambert')35embeddings = model.encode(sentences)36print(util.cos_sim(embeddings, embeddings))37```38 39 40 41## Usage (HuggingFace Transformers)42Without [sentence-transformers](https://www.SBERT.net), 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.43 44```python45from transformers import AutoTokenizer, AutoModel46import torch47 48 49#Mean Pooling - Take attention mask into account for correct averaging50def mean_pooling(model_output, attention_mask):51 token_embeddings = model_output[0] #First element of model_output contains all token embeddings52 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()53 return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)54 55 56# Sentences we want sentence embeddings for57sentences = ["אמא הלכה לגן", "אבא הלך לגן", "ירקוני קונה לנו פיצות"]58 59# Load model from HuggingFace Hub60tokenizer = AutoTokenizer.from_pretrained('MPA/sambert')61model = AutoModel.from_pretrained('MPA/sambert')62 63# Tokenize sentences64encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')65 66# Compute token embeddings67with torch.no_grad():68 model_output = model(**encoded_input)69 70# Perform pooling. In this case, mean pooling.71sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])72 73print("Sentence embeddings:")74print(sentence_embeddings)75```76 77 78 79## Evaluation Results80 81<!--- Describe how your model was evaluated -->82 83For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name={MODEL_NAME})84 85 86## Training87This model were trained in 2 stages:881. Unsupervised - ~2M paragraphs with 'MultipleNegativesRankingLoss' on cls-token892. Supervised - ~70k paragraphs with 'CosineSimilarityLoss'90The model was trained with the parameters:91 92**DataLoader**:93 94`torch.utils.data.dataloader.DataLoader` of length 11672 with parameters:95```96{'batch_size': 4, 'sampler': 'torch.utils.data.sampler.RandomSampler', 'batch_sampler': 'torch.utils.data.sampler.BatchSampler'}97```98 99**Loss**:100 101`sentence_transformers.losses.CosineSimilarityLoss.CosineSimilarityLoss` 102 103Parameters of the fit()-Method:104```105{106 "epochs": 1,107 "evaluation_steps": 1000,108 "evaluator": "sentence_transformers.evaluation.EmbeddingSimilarityEvaluator.EmbeddingSimilarityEvaluator",109 "max_grad_norm": 1,110 "optimizer_class": "<class 'torch.optim.adamw.AdamW'>",111 "optimizer_params": {112 "lr": 2e-05113 },114 "scheduler": "WarmupLinear",115 "steps_per_epoch": null,116 "warmup_steps": 500,117 "weight_decay": 0.01118}119```120 121 122## Full Model Architecture123```124SentenceTransformer(125 (0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: BertModel 126 (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, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False})127)128```129 130## Citing & Authors131 132<!--- Describe where people can find more information -->133Based on 134 135@misc{gueta2022large,136 title={Large Pre-Trained Models with Extra-Large Vocabularies: A Contrastive Analysis of Hebrew BERT Models and a New One to Outperform Them All}, 137 author={Eylon Gueta and Avi Shmidman and Shaltiel Shmidman and Cheyn Shmuel Shmidman and Joshua Guedalia and Moshe Koppel and Dan Bareket and Amit Seker and Reut Tsarfaty},138 year={2022},139 eprint={2211.15199},140 archivePrefix={arXiv},141 primaryClass={cs.CL}142}