Huggingface Pytorchポートに基づいたテキストおよびトークンシーケンスタスク用のGoogleのBERTモデルをFintuneのScikit-Learnラッパー。
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 :テキスト、テキストペア、またはトークンリストのリスト、パンダデータフレーム、またはnumpy配列
y :ラベル/ターゲットのリスト、パンダデータフレーム、または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 Leaderboardで再び比較しました。
| 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 'bert-base-cased'モデルを使用したデモについては、ner_englishノートブックを参照してください。
NERの結果、 NCBI disease Corpus名認識タスクでSciBERTとBioBERTを使用したBert-Sklearnを使用します。
このタスクの以前のSOTAは、テストセットのF1の87.34です。
| テスト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 |
| Biobert PubMed_v1.1 | 87.26 | Na |
SciBERTおよびBioBERTモデルを使用したデモについては、ner_ncbi_disease_biobert_scibertノートブックを参照してください。
それぞれのモデルの詳細については、Scibert PaperとBiobert Paperを参照してください。
インターネットムービーデータベースレビューセンチメントタスクのテキスト分類デモについては、IMDBノートブックを参照してください。
CoNLL-2000チャンキングタスクデータを使用した構文チャンキングに関するデモについては、Chunking_englishノートブックを参照してください。
中国のNERに'bert-base-chinese'を使用したデモについては、NER_CHINESEノートブックを参照してください。
Pytestでテストを実行します:
python -m pytest -sv tests/Google BERT Github and Paper:「Bert:言語理解のための深い双方向変圧器の事前訓練」(2018年10月10日)、J。
Huggingface pytorch-pretrained-BERT -github
SciBERT Github and Paper:「Scibert:科学的テキストのための事前に抑制された文脈化された埋め込み」(3/2019)I。Beltagy、A。Cohan、およびK. Lo
BioBERT Github and Paper:「Biobert:生物医学のテキストマイニングのための事前に訓練された生物医学言語表現モデル」(2/2019)、J。