wrapper scikit-learn เพื่อ finetune model bert ของ Google สำหรับงานข้อความและลำดับโทเค็นตามพอร์ต Pytorch Huggingface
SciBERT และ BioBERT Pretrained สำหรับโดเมนทางวิทยาศาสตร์และชีวการแพทย์ลองใน Google Colab!
ต้องใช้ Python> = 3.5 และ pytorch> = 0.4.1
git clone -b master https://github.com/charles9n/bert-sklearn
cd bert-sklearn
pip install . model.fit(X,y) เช่น finetune BERT
X : รายการ, pandas dataframe หรืออาร์เรย์ numpy ของข้อความ, คู่ข้อความหรือรายการโทเค็น
y : รายการ, pandas dataframe หรืออาร์เรย์ numpy ของป้ายกำกับ/เป้าหมาย
from bert_sklearn import BertClassifier
from bert_sklearn import BertRegressor
from bert_sklearn import load_model
# define model
model = BertClassifier () # text/text pair classification
# model = BertRegressor() # text/text pair regression
# model = BertTokenClassifier() # token sequence classification
# finetune model
model . fit ( X_train , y_train )
# make predictions
y_pred = model . predict ( X_test )
# make probabilty predictions
y_pred = model . predict_proba ( X_test )
# score model on test data
model . score ( X_test , y_test )
# save model to disk
savefile = '/data/mymodel.bin'
model . save ( savefile )
# load model from disk
new_model = load_model ( savefile )
# do stuff with new model
new_model . score ( X_test , y_test )ดูสมุดบันทึกการสาธิต
# try different options...
model . bert_model = 'bert-large-uncased'
model . num_mlp_layers = 3
model . max_seq_length = 196
model . epochs = 4
model . learning_rate = 4e-5
model . gradient_accumulation_steps = 4
# finetune
model . fit ( X_train , y_train )
# do stuff...
model . score ( X_test , y_test )ดูตัวเลือก
from sklearn . model_selection import GridSearchCV
params = { 'epochs' :[ 3 , 4 ], 'learning_rate' :[ 2e-5 , 3e-5 , 5e-5 ]}
# wrap classifier in GridSearchCV
clf = GridSearchCV ( BertClassifier ( validation_fraction = 0 ),
params ,
scoring = 'accuracy' ,
verbose = True )
# fit gridsearch
clf . fit ( X_train , y_train )ดูสมุดบันทึก demo_tuning_hyperparameters
ชุดข้อมูลรถไฟและ dev จากเกณฑ์มาตรฐาน bert-base-uncased
| MNLI (m/mm) | qqp | qnli | SST-2 | โคล่า | STS-B | MRPC | rte | |
|---|---|---|---|---|---|---|---|---|
| Bert Base (ลีดเดอร์บอร์ด) | 84.6/83.4 | 89.2 | 90.1 | 93.5 | 52.1 | 87.1 | 84.8 | 66.4 |
| เบิร์ตสเคลเรีย | 83.7/83.9 | 90.2 | 88.6 | 92.32 | 58.1 | 89.7 | 86.8 | 64.6 |
สามารถพบการวิ่งของแต่ละบุคคลได้ที่นี่
ผลลัพธ์สำหรับงานที่ใช้ร่วมกันของ CoNLL-2003
| Dev F1 | ทดสอบ F1 | |
|---|---|---|
| กระดาษเบิร์ต | 96.4 | 92.4 |
| เบิร์ตสเคลเรีย | 96.04 | 91.97 |
สถิติระดับขยายในการทดสอบ:
processed 46666 tokens with 5648 phrases ; found: 5740 phrases ; correct: 5173.
accuracy: 98.15% ; precision: 90.12% ; recall: 91.59% ; FB1: 90.85
LOC: precision: 92.24% ; recall: 92.69% ; FB1: 92.46 1676
MISC: precision: 78.07% ; recall: 81.62% ; FB1: 79.81 734
ORG: precision: 87.64% ; recall: 90.07% ; FB1: 88.84 1707
PER: precision: 96.00% ; recall: 96.35% ; FB1: 96.17 1623 ดูสมุดบันทึก NER_ENGLISH สำหรับการสาธิตโดยใช้โมเดล 'bert-base-cased'
ผลลัพธ์ที่ใช้โดยใช้ Bert-Sklearn กับ SciBERT และ BioBERT ในงานการจดจำชื่อ NCBI disease Corpus
SOTA ก่อนหน้าสำหรับงานนี้คือ 87.34 สำหรับ F1 ในชุดทดสอบ
| ทดสอบ F1 (Bert-sklearn) | ทดสอบ F1 (จากเอกสาร) | |
|---|---|---|
| ฐานเบิร์ต | 85.09 | 85.49 |
| Scibert basevocab cased | 88.29 | 86.91 |
| Scibert scivocab cased | 87.73 | 86.45 |
| Biobert PubMed_v1.0 | 87.86 | 87.38 |
| Biobert PubMed_PMC_V1.0 | 88.26 | 89.36 |
| Bibert PubMed_v1.1 | 87.26 | นา |
ดู NER_NCBI_DISEASEEASE_BIOBERT_SCIbert Notebook สำหรับการสาธิตโดยใช้โมเดล SciBERT และ BioBERT
ดูกระดาษ Scibert และ Biobert Paper สำหรับข้อมูลเพิ่มเติมเกี่ยวกับรุ่นที่เกี่ยวข้อง
ดูสมุดบันทึก IMDB สำหรับตัวอย่างการจำแนกประเภทข้อความในงานตรวจสอบฐานข้อมูลภาพยนตร์อินเทอร์เน็ต
ดูสมุดบันทึก chunking_english สำหรับการสาธิตเกี่ยวกับการใช้วากยสัมพันธ์โดยใช้ข้อมูลงาน chunking CoNLL-2000
ดูสมุดบันทึก ner_chinese สำหรับการสาธิตโดยใช้ 'bert-base-chinese' สำหรับภาษาจีน ner
เรียกใช้การทดสอบด้วย pytest:
python -m pytest -sv tests/ Google BERT GitHub และ Paper: "Bert: การฝึกอบรมก่อนหน้าของหม้อแปลงสองทิศทางเพื่อทำความเข้าใจภาษา" (10/2018) โดย J. Devlin, M. Chang, K. Lee และ K. Toutanova
HuggingFace pytorch-pretrained-BERT GitHub
SciBERT GitHub and Paper: "Scibert: Enmeddings เชิงบริบทสำหรับข้อความทางวิทยาศาสตร์" (3/2019) โดย I. Beltagy, A. Cohan และ K. Lo
BioBERT GitHub and Paper: "Biobert: รูปแบบการเป็นตัวแทนภาษาชีวการแพทย์ที่ผ่านการฝึกอบรมมาก่อนสำหรับการขุดข้อความชีวการแพทย์" (2/2019) โดย J. Lee, W. Yoon, S. Kim, D. Kim, S. Kim, CH SO และ J. Kang