readme: ---
  pipeline_tag: sentence-similarity
  language: "multilingual"
  tags:
  - feature-extraction
  - sentence-similarity
  - transformers
  - multilingual
  ---
  
  # mstsb-paraphrase-multilingual-mpnet-base-v2
  
  This is a fine-tuned version of `paraphrase-multilingual-mpnet-base-v2` from [sentence-transformers](https://www.SBERT.net) model with [Semantic Textual Similarity Benchmark](http://ixa2.si.ehu.eus/stswiki/index.php/Main_Page) extended to 15 languages: It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering, semantic search and measuring the similarity between two sentences.
  
  <!--- Describe your model here -->
  This model is fine-tuned version of `paraphrase-multilingual-mpnet-base-v2` for semantic textual similarity with multilingual data. The dataset used for this fine-tuning is STSb extended to 15 languages with Google Translator. For mantaining data quality the sentence pairs with a confidence value below 0.7 were dropped. The extended dataset is available at [GitHub](https://github.com/Huertas97/Multilingual-STSB). The languages included in the extended version are: ar, cs, de, en, es, fr, hi, it, ja, nl, pl, pt, ru, tr, zh-CN, zh-TW. The pooling operation used to condense the word embeddings into a sentence embedding is mean pooling (more info below). 
  
  <!-- ## Usage (Sentence-Transformers)
  
  Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
  
  ```
  pip install -U sentence-transformers
  ```
  
  Then you can use the model like this:
  
  ```python
  from sentence_transformers import SentenceTransformer
  # It support several languages
  sentences = ["This is an example sentence", "Esta es otra frase de ejemplo", "最後の例文"]
  
  # The pooling technique is automatically detected (mean pooling)
  model = SentenceTransformer('mstsb-paraphrase-multilingual-mpnet-base-v2')
  embeddings = model.encode(sentences)
  print(embeddings)
  ``` -->
  
  
  
  ## Usage (HuggingFace Transformers)
  Without [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.
  
  ```python
  from transformers import AutoTokenizer, AutoModel
  import torch
  
  # We should define the proper pooling function: Mean pooling
  # Mean Pooling - Take attention mask into account for correct averaging
  def mean_pooling(model_output, attention_mask):
      token_embeddings = model_output[0] #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)
  
  
  # Sentences we want sentence embeddings for
  sentences = ["This is an example sentence", "Esta es otra frase de ejemplo", "最後の例文"]
  
  # Load model from HuggingFace Hub
  tokenizer = AutoTokenizer.from_pretrained('AIDA-UPM/mstsb-paraphrase-multilingual-mpnet-base-v2')
  model = AutoModel.from_pretrained('AIDA-UPM/mstsb-paraphrase-multilingual-mpnet-base-v2')
  
  # Tokenize sentences
  encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
  
  # Compute token embeddings
  with torch.no_grad():
      model_output = model(**encoded_input)
  
  # Perform pooling. In this case, max pooling.
  sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
  
  print("Sentence embeddings:")
  print(sentence_embeddings)
  ```
  
  
  
  ## Evaluation Results
  
  <!--- Describe how your model was evaluated -->
  Check the test results in the Semantic Textual Similarity Tasks. The 15 languages available at the [Multilingual STSB](https://github.com/Huertas97/Multilingual-STSB) have been combined into monolingual and cross-lingual tasks, giving a total of 31 tasks. Monolingual tasks have both sentences from the same language source (e.g., Ar-Ar, Es-Es), while cross-lingual tasks have two sentences, each in a different language being one of them English (e.g., en-ar, en-es). 
  
  
  Here we compare the average multilingual semantic textual similairty capabilities between the  `paraphrase-multilingual-mpnet-base-v2` based model and the `mstsb-paraphrase-multilingual-mpnet-base-v2` fine-tuned model across the 31 tasks. It is worth noting that both models are multilingual, but the second model is adjusted with multilingual data for semantic similarity. The average of correlation coefficients is computed by transforming each correlation coefficient to a Fisher's z value, averaging them, and then back-transforming to a correlation coefficient.
  
  
  | Model                                       | Average Spearman Cosine Test |
  |:---------------------------------------------:|:------------------------------:|
  | mstsb-paraphrase-multilingual-mpnet-base-v2 | 0.835890                     |
  | paraphrase-multilingual-mpnet-base-v2       | 0.818896                     |
  
  <br>
  
  The following tables 