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