# from transformers import AutoTokenizer, AutoModel
from transformers import BertTokenizerFast, BertModel
model_name_or_path = "jinmang2/kpfbert"
# tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
tokenizer = BertTokenizerFast.from_pretrained(model_name_or_path)
# model = AutoModel.from_pretrained(model_name_or_path, add_pooling_layer=False)
model = BertModel.from_pretrained(model_name_or_path, add_pooling_layer=False)
>>> text = "언론진흥재단 BERT 모델을 공개합니다."
>>> tokenizer.tokenize(text)
['언론', '##진흥', '##재단', 'BE', '##RT', '모델', '##을', '공개', '##합니다', '.']
>>> encoded_input = tokenizer(text)
>>> encoded_input
{'input_ids': [2, 7392, 24220, 16227, 28024, 21924, 7522, 4620, 7247, 15801, 518, 3],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
>>> import torch
>>> model.eval()
>>> pt_encoded_input = tokenizer(text, return_tensors="pt")
>>> model(**pt_encoded_input, return_dict=False)
(tensor([[[ 0.0700, -3.3552, -1.0075, ..., -1.0417, 0.4648, 0.0141],
[ 0.0124, -0.1968, -0.3717, ..., 1.1253, 0.3683, 0.5172],
[-0.1394, -0.3788, 0.7651, ..., 1.0516, -1.5133, -0.5443],
...,
[-0.9051, 0.7249, -0.5570, ..., 1.0029, -1.7010, -1.0771],
[-0.1018, -0.0725, -0.1560, ..., 0.7753, -0.0967, 0.6251],
[ 0.0700, -3.3551, -1.0078, ..., -1.0416, 0.4646, 0.0136]]],
grad_fn=<NativeLayerNormBackward>), None)
| 구분 | NSMC | KLUE-NLI | KLUE-STS |
|---|---|---|---|
| 데이터 특징 및 규격 | 영화 리뷰 감점 분석, 학습 150,000 문장, 평가: 50,000문장 | 자연어 추론, 학습: 24,998 문장 평가: 3,000 문장 (dev셋) | 문장 의미적 유사도 측정, 학습: 11,668 문장 평가: 519 문장 (dev셋) |
| 평가방법 | accuracy | accuracy | Pearson Correlation |
| KPF BERT | 91.29% | 87.67% | 92.95% |
| KLUE BERT | 90.62% | 81.33% | 91.14% |
| KorBERT Tokenizer | 90.46% | 80.56% | 89.85% |
| KoBERT | 89.92% | 79.53% | 86.17% |
| BERT base multilingual | 87.33% | 73.30% | 85.66 % |
| 구분 | KorQuAD v1 | KLUE-MRC |
|---|---|---|
| 데이터 특징 및 규격 | 기계독해, 학습: 60,406 건 평가: 5,774 건 (dev셋) | 기계독해, 학습: 17,554 건 평가: 5,841 건 (dev셋) |
| 평가방법 | Exact Match / F1 | Exact Match / Rouge W |
| KPF BERT | 86.42% / 94.95% | 69.51 / 75.84% |
| KLUE BERT | 83.84% / 93.23% | 61.91% / 68.38% |
| KorBERT Tokenizer | 20.11% / 82.00% | 30.56% / 58.59% |
| KoBERT | 16.85% / 71.36% | 28.56% / 42.06 % |
| BERT base multilingual | 68.10% / 90.02% | 44.58% / 55.92% |
KPFBERTSUM (https://github.com/KPFBERT/kpfbertsum)
YouTube 'BERT란 무엇인가' 설명 링크 https://youtu.be/Pj6563CAnKs
# from transformers import AutoTokenizer, AutoModel
from transformers import BertTokenizerFast, BertModel
model_name_or_path = "jinmang2/kpfbert"
# tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
tokenizer = BertTokenizerFast.from_pretrained(model_name_or_path)
# model = AutoModel.from_pretrained(model_name_or_path, add_pooling_layer=False)
model = BertModel.from_pretrained(model_name_or_path, add_pooling_layer=False)
>>> text = "언론진흥재단 BERT 모델을 공개합니다."
>>> tokenizer.tokenize(text)
['언론', '##진흥', '##재단', 'BE', '##RT', '모델', '##을', '공개', '##합니다', '.']
>>> encoded_input = tokenizer(text)
>>> encoded_input
{'input_ids': [2, 7392, 24220, 16227, 28024, 21924, 7522, 4620, 7247, 15801, 518, 3],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
>>> import torch
>>> model.eval()
>>> pt_encoded_input = tokenizer(text, return_tensors="pt")
>>> model(**pt_encoded_input, return_dict=False)
(tensor([[[ 0.0700, -3.3552, -1.0075, ..., -1.0417, 0.4648, 0.0141],
[ 0.0124, -0.1968, -0.3717, ..., 1.1253, 0.3683, 0.5172],
[-0.1394, -0.3788, 0.7651, ..., 1.0516, -1.5133, -0.5443],
...,
[-0.9051, 0.7249, -0.5570, ..., 1.0029, -1.7010, -1.0771],
[-0.1018, -0.0725, -0.1560, ..., 0.7753, -0.0967, 0.6251],
[ 0.0700, -3.3551, -1.0078, ..., -1.0416, 0.4646, 0.0136]]],
grad_fn=<NativeLayerNormBackward>), None)
| 구분 | NSMC | KLUE-NLI | KLUE-STS |
|---|---|---|---|
| 데이터 특징 및 규격 | 영화 리뷰 감점 분석, 학습 150,000 문장, 평가: 50,000문장 | 자연어 추론, 학습: 24,998 문장 평가: 3,000 문장 (dev셋) | 문장 의미적 유사도 측정, 학습: 11,668 문장 평가: 519 문장 (dev셋) |
| 평가방법 | accuracy | accuracy | Pearson Correlation |
| KPF BERT | 91.29% | 87.67% | 92.95% |
| KLUE BERT | 90.62% | 81.33% | 91.14% |
| KorBERT Tokenizer | 90.46% | 80.56% | 89.85% |
| KoBERT | 89.92% | 79.53% | 86.17% |
| BERT base multilingual | 87.33% | 73.30% | 85.66 % |
| 구분 | KorQuAD v1 | KLUE-MRC |
|---|---|---|
| 데이터 특징 및 규격 | 기계독해, 학습: 60,406 건 평가: 5,774 건 (dev셋) | 기계독해, 학습: 17,554 건 평가: 5,841 건 (dev셋) |
| 평가방법 | Exact Match / F1 | Exact Match / Rouge W |
| KPF BERT | 86.42% / 94.95% | 69.51 / 75.84% |
| KLUE BERT | 83.84% / 93.23% | 61.91% / 68.38% |
| KorBERT Tokenizer | 20.11% / 82.00% | 30.56% / 58.59% |
| KoBERT | 16.85% / 71.36% | 28.56% / 42.06 % |
| BERT base multilingual | 68.10% / 90.02% | 44.58% / 55.92% |
KPFBERTSUM (https://github.com/KPFBERT/kpfbertsum)
YouTube 'BERT란 무엇인가' 설명 링크 https://youtu.be/Pj6563CAnKs