readme: ---
  pipeline_tag: sentence-similarity
  tags:
  - sentence-transformers
  - feature-extraction
  - sentence-similarity
  - transformers
  - ko
  - en
  widget:
    source_sentence: "대한민국의 수도는?"
    sentences:
      - "서울특별시는 한국이 정치,경제,문화 중심 도시이다."
      - "부산은 대한민국의 제2의 도시이자 최대의 해양 물류 도시이다."
      - "제주도는 대한민국에서 유명한 관광지이다"
      - "Seoul is the capital of Korea"
      - "울산광역시는 대한민국 남동부 해안에 있는 광역시이다" 
  
  ---
  
  # moco-sentencedistilbertV2.1
  
  This 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.
  
  <!--- Describe your model here -->
  
  - 이 모델은 [bongsoo/mdistilbertV2.1](https://huggingface.co/bongsoo/mdistilbertV2.1) MLM 모델을
  <br>sentencebert로 만든 후,추가적으로 STS Tearch-student 증류 학습 시켜 만든 모델 입니다.
  - **vocab: 152,537 개**(기존 119,548 vocab 에 32,989 신규 vocab 추가)
  
  ## 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
  sentences = ["서울은 한국이 수도이다", "The capital of Korea is Seoul"]
  
  model = SentenceTransformer('bongsoo/moco-sentencedistilbertV2.1')
  embeddings = model.encode(sentences)
  print(embeddings)
  
  # sklearn 을 이용하여 cosine_scores를 구함
  # => 입력값 embeddings 은 (1,768) 처럼 2D 여야 함.
  from sklearn.metrics.pairwise import paired_cosine_distances, paired_euclidean_distances, paired_manhattan_distances
  cosine_scores = 1 - (paired_cosine_distances(embeddings[0].reshape(1,-1), embeddings[1].reshape(1,-1)))
  
  print(f'*cosine_score:{cosine_scores[0]}')
  ```
  #### 출력(Outputs)
  ```
  [[ 0.27124503 -0.5836643   0.00736023 ... -0.0038319   0.01802095 -0.09652182]
   [ 0.2765149  -0.5754248   0.00788184 ...  0.07659392 -0.07825544 -0.06120609]]
  *cosine_score:0.9513546228408813
  ```
  
  ## 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.
  ```
  pip install transformers[torch]
  ```
  
  - 평균 폴링(mean_pooling) 방식 사용. ([cls 폴링](https://huggingface.co/sentence-transformers/bert-base-nli-cls-token), [max 폴링](https://huggingface.co/sentence-transformers/bert-base-nli-max-tokens))
  ```python
  from transformers import AutoTokenizer, AutoModel
  import torch
  
  
  #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 = ["서울은 한국이 수도이다", "The capital of Korea is Seoul"]
  
  # Load model from HuggingFace Hub
  tokenizer = AutoTokenizer.from_pretrained('bongsoo/moco-sentencedistilbertV2.1')
  model = AutoModel.from_pretrained('bongsoo/moco-sentencedistilbertV2.1')
  
  # 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, mean pooling.
  sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
  
  print("Sentence embeddings:")
  print(sentence_embeddings)
  
  # sklearn 을 이용하여 cosine_scores를 구함
  # => 입력값 embeddings 은 (1,768) 처럼 2D 여야 함.
  from sklearn.metrics.pairwise import paired_cosine_distances, paired_euclidean_distances, paired_manhattan_distances
  cosine_scores = 1 - (paired_cosine_distances(sentence_embeddings[0].reshape(1,-1), sentence_embeddings[1].reshape(1,-1)))
  
  print(f'*cosine_score:{cosine_scores[0]}')
  ```
  
  #### 출력(Outputs)
  ```
  Sentence embeddings:
  tensor([[ 0.2712, -0.5837,  0.0074,  ..., -0.0038,  0.0180, -0.0965],
          [ 0.2765, -0.5754,  0.0079,  ...,  0.0766, -0.0783, -0.0612]])
  *cosine_score:0.9513546228408813
  ```
  
  ## Evaluation Results
  
  <!--- Describe how your model was evaluated -->
  
  - 성능 측정을 위한 말뭉치는,  아래 한국어 (kor), 영어(en)  평가 말뭉치를 이용함
  <br> 한국어 : **korsts(1,379쌍문장)** 와 **klue-sts(519쌍문장)** 
  <br> 영어 : [stsb_multi_mt](https://huggingface.co/datasets/stsb_multi_mt)(1,376쌍문장) 와 [glue:stsb](https://huggingface.co/datasets/glue/viewer/stsb/validation) (1,500쌍문장)
  - 성능 지표는 **cosin.spearman/max**(cosine,eculidean,manhatten,doc중 max값)
  - 평가 측정 코드는 [여기](https://github.com/kobongsoo/BERT/blob/master/sbert/sbert-test.ipynb) 참조
  
  |모델     |korsts|klue-sts|glue(stsb)|stsb_multi_mt(en)|
  |:--------|------:|--------:|--------------:|------------:|
  |distiluse-base-multilingual-cased-v2   |0.7475/0.7556    |0.7855/0.7862    |0.8193           |0.8075/0.8168|
  |paraphrase-multilingual-mpnet-base-v2  |0.8201           |0.7993    