transformers で学習済みの BERT モデルから固有表現抽出用のモデルインスタンスをつくるまでだけです。
→ 改善版(2020-09-16) → GitHub に移行しました。GitHub - CookieBox26/ML: machine learning
from transformers import ( BertConfig, BertForTokenClassification ) def main(): # 各トークンを以下の13クラスのいずれかに分類するような固有表現抽出をしたい. labels = [ 'B-corporation', 'B-creative-work', 'B-group', 'B-location', 'B-person', 'B-product', 'I-corporation', 'I-creative-work', 'I-group', 'I-location', 'I-person', 'I-product', 'O' ] id2label = {i: label for i, label in enumerate(labels)} label2id = {label: i for i, label in enumerate(labels)} # 利用する学習済みBERTモデルの名前を指定する. model_name = 'bert-large-cased' # 設定オブジェクトをつくる. config = BertConfig.from_pretrained( # 利用する学習済みモデルは必ず教える. pretrained_model_name_or_path=model_name, # 今回は13クラスに分類する固有表現抽出をしたいので以下も教える. id2label=id2label, label2id=label2id, # ただこれは結局渡さなくても大丈夫だった. ) print(config) # 学習済みモデルの名前と設定オブジェクトから各トークン分類用モデルを生成する. model = BertForTokenClassification.from_pretrained( pretrained_model_name_or_path=model_name, config=config ) print(model) # 24層あるのでプリントすると長い. if __name__ == '__main__': main()