This repository implements an LSTM-CRF model for named entity recognition. The model is same as the one by Lample et al., (2016) except we do not have the last tanh
layer after the BiLSTM.
We achieve the SOTA performance on both CoNLL-2003 and OntoNotes 5.0 English datasets (check our benchmark with Glove and ELMo, other
and benchmark results with fine-tuning BERT).
Announcements
Model | Dataset | Precision | Recall | F1 |
---|---|---|---|---|
BERT-base-cased + CRF (this repo) | CONLL-2003 | 91.69 | 92.05 | 91.87 |
Roberta-base + CRF (this repo) | CoNLL-2003 | 91.88 | 93.01 | 92.44 |
BERT-base-cased + CRF (this repo) | OntoNotes 5 | 89.57 | 89.45 | 89.51 |
Roberta-base + CRF (this repo) | OntoNotes 5 | 90.12 | 91.25 | 90.68 |
More details
In the documentation below, we present two ways for users to run the code:
Our default argument setup refers to the first one 1
.
Simply replace the embedder_type
argument with the model in HuggingFace. For example, if we are using roberta-large
, we just need to
change the embedder type as roberta-large
.
python transformers_trainer.py --device=cuda:0 --dataset=YourData --model_folder=saved_models --embedder_type=roberta-base
Distributed Training (If necessary)
accelerate
package to enable distributed training. After you set the proper configuration of your distributed environment,
by accelerate config
, you can easily run the following command for distributed training
accelerate launch transformers_trainer_ddp.py --batch_size=30 {YOUR_OTHER_ARGUMENTS}
Note that, this batch size
is actually __batch_size per GPU device__.
(Optional) Using other models in HuggingFace.
Run the main file with modified argument embedder_type
:
python trainer.py --embedder_type=bert-large-cased
The default value for embedder_type
is roberta-base
.
Changing the name to something like bert-base-cased
or roberta-large
, we directly load the model from huggingface.
Note: if you use other models, remember to replace the [tokenization mechanism]() in config/utils.py
.
Our default tokenizer is assumed to be fast_tokenizer
. If your tokenizer does not support fast
mode, try set use_fast=False
:
tokenizer = AutoTokenizer.from_pretrained(conf.embedder_type, add_prefix_space=True, use_fast=False)
config/transformers_util.py
model/embedder/transformers_embedder.py
model/transformers_embedder.py
and uncomment the following:
self.model.requires_grad = False
Using Word embedding or external contextualized embedding (ELMo/BERT) can be found in here.
YourData
under the data directory. train.txt
, dev.txt
and test.txt
files (make sure the format is compatible, i.e. the first column is words and the last column are tags) under this directory. If you have a different format, simply modify the reader in config/reader.py
. dataset
argument to YourData
when you run trainer.py
. import
stuff)