Scikit-Learn包裝器,用於Finetune Google的BERT模型,用於基於HuggingFace Pytorch端口的文本和令牌序列任務。
SciBERT和BioBERT審慎的科學和生物醫學領域模型。在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) IE 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筆記本。
來自膠水(廣義語言理解評估)基準的列車和開發數據集用於基於bert-base-uncased模型,並再次比較了Google Paper和Glue排行榜中報告的結果。
| mnli(m/mm) | QQP | Qnli | SST-2 | 可樂 | STS-B | MRPC | rte | |
|---|---|---|---|---|---|---|---|---|
| 伯特基地(排行榜) | 84.6/83.4 | 89.2 | 90.1 | 93.5 | 52.1 | 87.1 | 84.8 | 66.4 |
| Bert-Sklearn | 83.7/83.9 | 90.2 | 88.6 | 92.32 | 58.1 | 89.7 | 86.8 | 64.6 |
可以在此處找到個別跑步。
CoNLL-2003共享任務的NER結果
| 開發F1 | 測試F1 | |
|---|---|---|
| 伯特紙 | 96.4 | 92.4 |
| Bert-Sklearn | 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'模型的演示。
NER使用SciBERT和BioBERT的Bert-Sklearn進行NCBI disease Corpus識別任務的結果。
該任務的先前SOTA為測試集的F1為87.34 。
| 測試F1(Bert-Sklearn) | 測試F1(來自論文) | |
|---|---|---|
| Bert Base Cased | 85.09 | 85.49 |
| Scibert basevocab外殼 | 88.29 | 86.91 |
| Scibert scivocab外殼 | 87.73 | 86.45 |
| Biobert PubMed_V1.0 | 87.86 | 87.38 |
| Biobert PubMed_PMC_V1.0 | 88.26 | 89.36 |
| Biobert PubMed_V1.1 | 87.26 | na |
請參閱NER_NCBI_DISEASE_BIOBERT_SCIBERT使用SciBERT和BioBERT模型的演示筆記本。
有關各自模型的更多信息,請參見Scibert Paper和Biobert紙。
有關Internet電影數據庫評論情感任務的文本分類演示,請參見IMDB筆記本。
請參閱CoNLL-2000塊任務數據,請參閱有關句法塊的演示的Chunking_English Notebook。
請參閱NER_CHINESE筆記本,以獲取使用'bert-base-chinese'的演示。
使用Pytest進行測試:
python -m pytest -sv tests/Google BERT Github和Paper:“ Bert:深層雙向變壓器的預培訓,以了解語言理解”(10/2018)J。 Devlin,M。 Chang,K。 Lee和K. Toutanova
擁抱面pytorch-pretrained-BERT github
SciBERT github和紙:“ Scibert:I. Beltagy,A。Cohan和K. Lo的科學文本的上下文化嵌入”(3/2019)
BioBERT Github和Paper:“ Biobert:生物醫學文本挖掘的預培訓的生物醫學語言代表模型”(2/2019)J。 Lee,W。 Yoon,S。 Kim,D。 Kim,D。 Kim,S。 Kim,S。 Kim,CH SO,CH SO和J. Kang