bongsoo commited on
Commit
96c8ba9
1 Parent(s): 2eb8131

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +34 -2
README.md CHANGED
@@ -7,7 +7,7 @@ tags:
7
  - en
8
  - ko
9
  ---
10
- # mdistilbertV2.0
11
 
12
  - bert-base-multilingual-cased 모델에 [moco-corpus-kowiki2022 말뭉치](https://huggingface.co/datasets/bongsoo/moco-corpus-kowiki2022)(kowiki202206 + MOCOMSYS 추출 3.2M 문장)로 vocab 추가하여 학습 시킨 모델
13
  - **vocab: 152,537개**(기존 bert 모델 vocab(119,548개)에 32,989개 vocab 추가)
@@ -18,10 +18,42 @@ tags:
18
  from transformers import AutoTokenizer, AutoModel
19
  import torch
20
 
21
- tokenizer = AutoTokenizer.from_pretrained('bongsoo/mbertV2.0', do_lower_case=False)
 
 
 
 
 
 
 
 
 
 
 
 
22
  model = AutoModel.from_pretrained('bongsoo/mbertV2.0')
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  ```
 
25
  ## Training
26
 
27
  **MLM(Masked Langeuage Model) 훈련**
 
7
  - en
8
  - ko
9
  ---
10
+ # mbertV2.0
11
 
12
  - bert-base-multilingual-cased 모델에 [moco-corpus-kowiki2022 말뭉치](https://huggingface.co/datasets/bongsoo/moco-corpus-kowiki2022)(kowiki202206 + MOCOMSYS 추출 3.2M 문장)로 vocab 추가하여 학습 시킨 모델
13
  - **vocab: 152,537개**(기존 bert 모델 vocab(119,548개)에 32,989개 vocab 추가)
 
18
  from transformers import AutoTokenizer, AutoModel
19
  import torch
20
 
21
+
22
+ #Mean Pooling - Take attention mask into account for correct averaging
23
+ def mean_pooling(model_output, attention_mask):
24
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
25
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
26
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
27
+
28
+
29
+ # Sentences we want sentence embeddings for
30
+ sentences = ['This is an example sentence', 'Each sentence is converted']
31
+
32
+ # Load model from HuggingFace Hub
33
+ tokenizer = AutoTokenizer.from_pretrained('bongsoo/mbertV2.0')
34
  model = AutoModel.from_pretrained('bongsoo/mbertV2.0')
35
 
36
+ # Tokenize sentences
37
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
38
+
39
+ # Compute token embeddings
40
+ with torch.no_grad():
41
+ model_output = model(**encoded_input)
42
+
43
+ # Perform pooling. In this case, mean pooling.
44
+ sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
45
+
46
+ print("Sentence embeddings:")
47
+ print(sentence_embeddings)
48
+
49
+ # sklearn 을 이용하여 cosine_scores를 구함
50
+ # => 입력값 embeddings 은 (1,768) 처럼 2D 여야 함.
51
+ from sklearn.metrics.pairwise import paired_cosine_distances, paired_euclidean_distances, paired_manhattan_distances
52
+ cosine_scores = 1 - (paired_cosine_distances(sentence_embeddings[0].reshape(1,-1), sentence_embeddings[1].reshape(1,-1)))
53
+
54
+ print(f'*cosine_score:{cosine_scores[0]}')
55
  ```
56
+
57
  ## Training
58
 
59
  **MLM(Masked Langeuage Model) 훈련**