Commit
•
203b2e1
1
Parent(s):
307c322
Upload README.md
Browse files
README.md
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- flair
|
4 |
+
- token-classification
|
5 |
+
- sequence-tagger-model
|
6 |
+
language: en
|
7 |
+
datasets:
|
8 |
+
- ontonotes
|
9 |
+
widget:
|
10 |
+
- text: "On September 1st George won 1 dollar while watching Game of Thrones."
|
11 |
+
---
|
12 |
+
|
13 |
+
## English NER in Flair (Ontonotes large model)
|
14 |
+
|
15 |
+
This is the large 18-class NER model for English that ships with [Flair](https://github.com/flairNLP/flair/).
|
16 |
+
|
17 |
+
F1-Score: **90.93** (Ontonotes)
|
18 |
+
|
19 |
+
Predicts 18 tags:
|
20 |
+
|
21 |
+
| **tag** | **meaning** |
|
22 |
+
|---------------------------------|-----------|
|
23 |
+
| CARDINAL | cardinal value |
|
24 |
+
| DATE | date value |
|
25 |
+
| EVENT | event name |
|
26 |
+
| FAC | building name |
|
27 |
+
| GPE | geo-political entity |
|
28 |
+
| LANGUAGE | language name |
|
29 |
+
| LAW | law name |
|
30 |
+
| LOC | location name |
|
31 |
+
| MONEY | money name |
|
32 |
+
| NORP | affiliation |
|
33 |
+
| ORDINAL | ordinal value |
|
34 |
+
| ORG | organization name |
|
35 |
+
| PERCENT | percent value |
|
36 |
+
| PERSON | person name |
|
37 |
+
| PRODUCT | product name |
|
38 |
+
| QUANTITY | quantity value |
|
39 |
+
| TIME | time value |
|
40 |
+
| WORK_OF_ART | name of work of art |
|
41 |
+
|
42 |
+
Based on document-level XLM-R embeddings and [FLERT](https://arxiv.org/pdf/2011.06993v1.pdf/).
|
43 |
+
|
44 |
+
---
|
45 |
+
|
46 |
+
### Demo: How to use in Flair
|
47 |
+
|
48 |
+
Requires: **[Flair](https://github.com/flairNLP/flair/)** (`pip install flair`)
|
49 |
+
|
50 |
+
```python
|
51 |
+
from flair.data import Sentence
|
52 |
+
from flair.models import SequenceTagger
|
53 |
+
|
54 |
+
# load tagger
|
55 |
+
tagger = SequenceTagger.load("flair/ner-english-ontonotes-large")
|
56 |
+
|
57 |
+
# make example sentence
|
58 |
+
sentence = Sentence("On September 1st George won 1 dollar while watching Game of Thrones.")
|
59 |
+
|
60 |
+
# predict NER tags
|
61 |
+
tagger.predict(sentence)
|
62 |
+
|
63 |
+
# print sentence
|
64 |
+
print(sentence)
|
65 |
+
|
66 |
+
# print predicted NER spans
|
67 |
+
print('The following NER tags are found:')
|
68 |
+
# iterate over entities and print
|
69 |
+
for entity in sentence.get_spans('ner'):
|
70 |
+
print(entity)
|
71 |
+
|
72 |
+
```
|
73 |
+
|
74 |
+
This yields the following output:
|
75 |
+
```
|
76 |
+
Span [2,3]: "September 1st" [− Labels: DATE (1.0)]
|
77 |
+
Span [4]: "George" [− Labels: PERSON (1.0)]
|
78 |
+
Span [6,7]: "1 dollar" [− Labels: MONEY (1.0)]
|
79 |
+
Span [10,11,12]: "Game of Thrones" [− Labels: WORK_OF_ART (1.0)]
|
80 |
+
```
|
81 |
+
|
82 |
+
So, the entities "*September 1st*" (labeled as a **date**), "*George*" (labeled as a **person**), "*1 dollar*" (labeled as a **money**) and "Game of Thrones" (labeled as a **work of art**) are found in the sentence "*On September 1st George Washington won 1 dollar while watching Game of Thrones*".
|
83 |
+
|
84 |
+
|
85 |
+
---
|
86 |
+
|
87 |
+
### Training: Script to train this model
|
88 |
+
|
89 |
+
The following Flair script was used to train this model:
|
90 |
+
|
91 |
+
```python
|
92 |
+
from flair.data import Corpus
|
93 |
+
from flair.datasets import ColumnCorpus
|
94 |
+
from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
|
95 |
+
|
96 |
+
# 1. load the corpus (Ontonotes does not ship with Flair, you need to download and reformat into a column format yourself)
|
97 |
+
corpus: Corpus = ColumnCorpus(
|
98 |
+
"resources/tasks/onto-ner",
|
99 |
+
column_format={0: "text", 1: "pos", 2: "upos", 3: "ner"},
|
100 |
+
tag_to_bioes="ner",
|
101 |
+
)
|
102 |
+
|
103 |
+
# 2. what tag do we want to predict?
|
104 |
+
tag_type = 'ner'
|
105 |
+
|
106 |
+
# 3. make the tag dictionary from the corpus
|
107 |
+
tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
|
108 |
+
|
109 |
+
# 4. initialize fine-tuneable transformer embeddings WITH document context
|
110 |
+
from flair.embeddings import TransformerWordEmbeddings
|
111 |
+
|
112 |
+
embeddings = TransformerWordEmbeddings(
|
113 |
+
model='xlm-roberta-large',
|
114 |
+
layers="-1",
|
115 |
+
subtoken_pooling="first",
|
116 |
+
fine_tune=True,
|
117 |
+
use_context=True,
|
118 |
+
)
|
119 |
+
|
120 |
+
# 5. initialize bare-bones sequence tagger (no CRF, no RNN, no reprojection)
|
121 |
+
from flair.models import SequenceTagger
|
122 |
+
|
123 |
+
tagger = SequenceTagger(
|
124 |
+
hidden_size=256,
|
125 |
+
embeddings=embeddings,
|
126 |
+
tag_dictionary=tag_dictionary,
|
127 |
+
tag_type='ner',
|
128 |
+
use_crf=False,
|
129 |
+
use_rnn=False,
|
130 |
+
reproject_embeddings=False,
|
131 |
+
)
|
132 |
+
|
133 |
+
# 6. initialize trainer with AdamW optimizer
|
134 |
+
from flair.trainers import ModelTrainer
|
135 |
+
|
136 |
+
trainer = ModelTrainer(tagger, corpus, optimizer=torch.optim.AdamW)
|
137 |
+
|
138 |
+
# 7. run training with XLM parameters (20 epochs, small LR)
|
139 |
+
from torch.optim.lr_scheduler import OneCycleLR
|
140 |
+
|
141 |
+
trainer.train('resources/taggers/ner-english-ontonotes-large',
|
142 |
+
learning_rate=5.0e-6,
|
143 |
+
mini_batch_size=4,
|
144 |
+
mini_batch_chunk_size=1,
|
145 |
+
max_epochs=20,
|
146 |
+
scheduler=OneCycleLR,
|
147 |
+
embeddings_storage_mode='none',
|
148 |
+
weight_decay=0.,
|
149 |
+
)
|
150 |
+
```
|
151 |
+
|
152 |
+
|
153 |
+
|
154 |
+
---
|
155 |
+
|
156 |
+
### Cite
|
157 |
+
|
158 |
+
Please cite the following paper when using this model.
|
159 |
+
|
160 |
+
```
|
161 |
+
@misc{schweter2020flert,
|
162 |
+
title={FLERT: Document-Level Features for Named Entity Recognition},
|
163 |
+
author={Stefan Schweter and Alan Akbik},
|
164 |
+
year={2020},
|
165 |
+
eprint={2011.06993},
|
166 |
+
archivePrefix={arXiv},
|
167 |
+
primaryClass={cs.CL}
|
168 |
+
}
|
169 |
+
```
|
170 |
+
|
171 |
+
---
|
172 |
+
|
173 |
+
### Issues?
|
174 |
+
|
175 |
+
The Flair issue tracker is available [here](https://github.com/flairNLP/flair/issues/).
|