README.md
| 1 | --- |
| 2 | tags: |
| 3 | - flair |
| 4 | - token-classification |
| 5 | - sequence-tagger-model |
| 6 | language: de |
| 7 | datasets: |
| 8 | - conll2003 |
| 9 | widget: |
| 10 | - text: "George Washington ging nach Washington" |
| 11 | --- |
| 12 | |
| 13 | ## German NER in Flair (large model) |
| 14 | |
| 15 | This is the large 4-class NER model for German that ships with [Flair](https://github.com/flairNLP/flair/). |
| 16 | |
| 17 | F1-Score: **92,31** (CoNLL-03 German revised) |
| 18 | |
| 19 | Predicts 4 tags: |
| 20 | |
| 21 | | **tag** | **meaning** | |
| 22 | |---------------------------------|-----------| |
| 23 | | PER | person name | |
| 24 | | LOC | location name | |
| 25 | | ORG | organization name | |
| 26 | | MISC | other name | |
| 27 | |
| 28 | Based on document-level XLM-R embeddings and [FLERT](https://arxiv.org/pdf/2011.06993v1.pdf). |
| 29 | |
| 30 | --- |
| 31 | |
| 32 | ### Demo: How to use in Flair |
| 33 | |
| 34 | Requires: **[Flair](https://github.com/flairNLP/flair/)** (`pip install flair`) |
| 35 | |
| 36 | ```python |
| 37 | from flair.data import Sentence |
| 38 | from flair.models import SequenceTagger |
| 39 | |
| 40 | # load tagger |
| 41 | tagger = SequenceTagger.load("flair/ner-german-large") |
| 42 | |
| 43 | # make example sentence |
| 44 | sentence = Sentence("George Washington ging nach Washington") |
| 45 | |
| 46 | # predict NER tags |
| 47 | tagger.predict(sentence) |
| 48 | |
| 49 | # print sentence |
| 50 | print(sentence) |
| 51 | |
| 52 | # print predicted NER spans |
| 53 | print('The following NER tags are found:') |
| 54 | # iterate over entities and print |
| 55 | for entity in sentence.get_spans('ner'): |
| 56 | print(entity) |
| 57 | |
| 58 | ``` |
| 59 | |
| 60 | This yields the following output: |
| 61 | ``` |
| 62 | Span [1,2]: "George Washington" [− Labels: PER (1.0)] |
| 63 | Span [5]: "Washington" [− Labels: LOC (1.0)] |
| 64 | ``` |
| 65 | |
| 66 | So, the entities "*George Washington*" (labeled as a **person**) and "*Washington*" (labeled as a **location**) are found in the sentence "*George Washington ging nach Washington*". |
| 67 | |
| 68 | |
| 69 | --- |
| 70 | |
| 71 | ### Training: Script to train this model |
| 72 | |
| 73 | The following Flair script was used to train this model: |
| 74 | |
| 75 | ```python |
| 76 | import torch |
| 77 | |
| 78 | # 1. get the corpus |
| 79 | from flair.datasets import CONLL_03_GERMAN |
| 80 | |
| 81 | corpus = CONLL_03_GERMAN() |
| 82 | |
| 83 | # 2. what tag do we want to predict? |
| 84 | tag_type = 'ner' |
| 85 | |
| 86 | # 3. make the tag dictionary from the corpus |
| 87 | tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type) |
| 88 | |
| 89 | # 4. initialize fine-tuneable transformer embeddings WITH document context |
| 90 | from flair.embeddings import TransformerWordEmbeddings |
| 91 | |
| 92 | embeddings = TransformerWordEmbeddings( |
| 93 | model='xlm-roberta-large', |
| 94 | layers="-1", |
| 95 | subtoken_pooling="first", |
| 96 | fine_tune=True, |
| 97 | use_context=True, |
| 98 | ) |
| 99 | |
| 100 | # 5. initialize bare-bones sequence tagger (no CRF, no RNN, no reprojection) |
| 101 | from flair.models import SequenceTagger |
| 102 | |
| 103 | tagger = SequenceTagger( |
| 104 | hidden_size=256, |
| 105 | embeddings=embeddings, |
| 106 | tag_dictionary=tag_dictionary, |
| 107 | tag_type='ner', |
| 108 | use_crf=False, |
| 109 | use_rnn=False, |
| 110 | reproject_embeddings=False, |
| 111 | ) |
| 112 | |
| 113 | # 6. initialize trainer with AdamW optimizer |
| 114 | from flair.trainers import ModelTrainer |
| 115 | |
| 116 | trainer = ModelTrainer(tagger, corpus, optimizer=torch.optim.AdamW) |
| 117 | |
| 118 | # 7. run training with XLM parameters (20 epochs, small LR) |
| 119 | from torch.optim.lr_scheduler import OneCycleLR |
| 120 | |
| 121 | trainer.train('resources/taggers/ner-german-large', |
| 122 | learning_rate=5.0e-6, |
| 123 | mini_batch_size=4, |
| 124 | mini_batch_chunk_size=1, |
| 125 | max_epochs=20, |
| 126 | scheduler=OneCycleLR, |
| 127 | embeddings_storage_mode='none', |
| 128 | weight_decay=0., |
| 129 | ) |
| 130 | |
| 131 | ) |
| 132 | ``` |
| 133 | |
| 134 | |
| 135 | |
| 136 | --- |
| 137 | |
| 138 | ### Cite |
| 139 | |
| 140 | Please cite the following paper when using this model. |
| 141 | |
| 142 | ``` |
| 143 | @misc{schweter2020flert, |
| 144 | title={FLERT: Document-Level Features for Named Entity Recognition}, |
| 145 | author={Stefan Schweter and Alan Akbik}, |
| 146 | year={2020}, |
| 147 | eprint={2011.06993}, |
| 148 | archivePrefix={arXiv}, |
| 149 | primaryClass={cs.CL} |
| 150 | } |
| 151 | ``` |
| 152 | |
| 153 | --- |
| 154 | |
| 155 | ### Issues? |
| 156 | |
| 157 | The Flair issue tracker is available [here](https://github.com/flairNLP/flair/issues/). |
| 158 | |