Update README.md
Browse files
README.md
CHANGED
|
@@ -2,27 +2,23 @@
|
|
| 2 |
|
| 3 |
Implemented by [sayef](https://huggingface.co/sayef).
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
The FSNER model was proposed in [Example-Based Named Entity Recognition](https://arxiv.org/abs/2008.10570) by Morteza Ziyadi, Yuting Sun, Abhishek Goswami, Jade Huang, Weizhu Chen. To identify entity spans in a new domain, it uses a train-free few-shot learning approach inspired by question-answering.
|
| 8 |
-
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
## Abstract
|
| 12 |
-
----
|
| 13 |
-
> We present a novel approach to named entity recognition (NER) in the presence of scarce data that we call example-based NER. Our train-free few-shot learning approach takes inspiration from question-answering to identify entity spans in a new and unseen domain. In comparison with the current state-of-the-art, the proposed method performs significantly better, especially when using a low number of support examples.
|
| 14 |
-
|
| 15 |
|
|
|
|
| 16 |
|
| 17 |
## Model Training Details
|
| 18 |
-
-----
|
| 19 |
|
| 20 |
| identifier | epochs | datasets |
|
| 21 |
| ---------- |:------:|:-----------------------------------------------------------------------------------------------:|
|
| 22 |
| [sayef/fsner-bert-base-uncased](https://huggingface.co/sayef/fsner-bert-base-uncased) | 25 | ontonotes5, conll2003, wnut2017, mit_movie_trivia, mit_restaurant and fin (Alvarado et al.). |
|
| 23 |
|
| 24 |
## Installation and Example Usage
|
| 25 |
-
------
|
| 26 |
|
| 27 |
You can use the FSNER model in 3 ways:
|
| 28 |
|
|
@@ -30,18 +26,18 @@ You can use the FSNER model in 3 ways:
|
|
| 30 |
|
| 31 |
or
|
| 32 |
|
| 33 |
-
2. Install from source: `python
|
| 34 |
|
| 35 |
or
|
| 36 |
|
| 37 |
-
3. Clone [repo](https://github.com/sayef/fsner) and add absolute path of `fsner/src` directory to your PYTHONPATH and
|
|
|
|
| 38 |
|
| 39 |
```python
|
| 40 |
import json
|
| 41 |
|
| 42 |
from fsner import FSNERModel, FSNERTokenizerUtils, pretty_embed
|
| 43 |
|
| 44 |
-
|
| 45 |
query_texts = [
|
| 46 |
"Does Luke's serve lunch?",
|
| 47 |
"Chang does not speak Taiwanese very well.",
|
|
@@ -73,7 +69,6 @@ support_texts = {
|
|
| 73 |
|
| 74 |
device = 'cpu'
|
| 75 |
|
| 76 |
-
|
| 77 |
tokenizer = FSNERTokenizerUtils("sayef/fsner-bert-base-uncased")
|
| 78 |
queries = tokenizer.tokenize(query_texts).to(device)
|
| 79 |
supports = tokenizer.tokenize(list(support_texts.values())).to(device)
|
|
@@ -94,11 +89,10 @@ output = tokenizer.extract_entity_from_scores(query_texts, queries, p_starts, p_
|
|
| 94 |
|
| 95 |
print(json.dumps(output, indent=2))
|
| 96 |
|
| 97 |
-
# install
|
| 98 |
pretty_embed(query_texts, output, list(support_texts.keys()))
|
| 99 |
```
|
| 100 |
|
| 101 |
-
|
| 102 |
<!DOCTYPE html>
|
| 103 |
<html lang="en">
|
| 104 |
<head>
|
|
@@ -126,10 +120,12 @@ pretty_embed(query_texts, output, list(support_texts.keys()))
|
|
| 126 |
</body>
|
| 127 |
</html>
|
| 128 |
|
| 129 |
-
|
| 130 |
## Datasets preparation
|
| 131 |
|
| 132 |
1. We need to convert dataset into the following format. Let's say we have a dataset file train.json like following.
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
```json
|
| 135 |
{
|
|
@@ -158,10 +154,10 @@ pretty_embed(query_texts, output, list(support_texts.keys()))
|
|
| 158 |
1. [train](https://gist.githubusercontent.com/sayef/46deaf7e6c6e1410b430ddc8aff9c557/raw/ea7ae2ae933bfc9c0daac1aa52a9dc093d5b36f4/ontonotes5.train.json)
|
| 159 |
2. [dev](https://gist.githubusercontent.com/sayef/46deaf7e6c6e1410b430ddc8aff9c557/raw/ea7ae2ae933bfc9c0daac1aa52a9dc093d5b36f4/ontonotes5.dev.json)
|
| 160 |
|
| 161 |
-
3. Then
|
| 162 |
|
| 163 |
```bash
|
| 164 |
-
|
| 165 |
--train-batch-size 6 --val-batch-size 6 --n-examples-per-entity 10 --neg-example-batch-ratio 1/3 --max-epochs 25 --device gpu \
|
| 166 |
--gpus -1 --strategy ddp
|
| 167 |
```
|
|
|
|
| 2 |
|
| 3 |
Implemented by [sayef](https://huggingface.co/sayef).
|
| 4 |
|
| 5 |
+
# Overview
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
The FSNER model was proposed in [Example-Based Named Entity Recognition](https://arxiv.org/abs/2008.10570) by Morteza
|
| 8 |
+
Ziyadi, Yuting Sun, Abhishek Goswami, Jade Huang, Weizhu Chen. To identify entity spans in a new domain, it uses a
|
| 9 |
+
train-free few-shot learning approach inspired by question-answering.
|
| 10 |
|
| 11 |
## Abstract
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
> We present a novel approach to named entity recognition (NER) in the presence of scarce data that we call example-based NER. Our train-free few-shot learning approach takes inspiration from question-answering to identify entity spans in a new and unseen domain. In comparison with the current state-of-the-art, the proposed method performs significantly better, especially when using a low number of support examples.
|
| 14 |
|
| 15 |
## Model Training Details
|
|
|
|
| 16 |
|
| 17 |
| identifier | epochs | datasets |
|
| 18 |
| ---------- |:------:|:-----------------------------------------------------------------------------------------------:|
|
| 19 |
| [sayef/fsner-bert-base-uncased](https://huggingface.co/sayef/fsner-bert-base-uncased) | 25 | ontonotes5, conll2003, wnut2017, mit_movie_trivia, mit_restaurant and fin (Alvarado et al.). |
|
| 20 |
|
| 21 |
## Installation and Example Usage
|
|
|
|
| 22 |
|
| 23 |
You can use the FSNER model in 3 ways:
|
| 24 |
|
|
|
|
| 26 |
|
| 27 |
or
|
| 28 |
|
| 29 |
+
2. Install from source: `python install .` and import the model as shown in the code example below
|
| 30 |
|
| 31 |
or
|
| 32 |
|
| 33 |
+
3. Clone [repo](https://github.com/sayef/fsner) and add absolute path of `fsner/src` directory to your PYTHONPATH and
|
| 34 |
+
import the model as shown in the code example below
|
| 35 |
|
| 36 |
```python
|
| 37 |
import json
|
| 38 |
|
| 39 |
from fsner import FSNERModel, FSNERTokenizerUtils, pretty_embed
|
| 40 |
|
|
|
|
| 41 |
query_texts = [
|
| 42 |
"Does Luke's serve lunch?",
|
| 43 |
"Chang does not speak Taiwanese very well.",
|
|
|
|
| 69 |
|
| 70 |
device = 'cpu'
|
| 71 |
|
|
|
|
| 72 |
tokenizer = FSNERTokenizerUtils("sayef/fsner-bert-base-uncased")
|
| 73 |
queries = tokenizer.tokenize(query_texts).to(device)
|
| 74 |
supports = tokenizer.tokenize(list(support_texts.values())).to(device)
|
|
|
|
| 89 |
|
| 90 |
print(json.dumps(output, indent=2))
|
| 91 |
|
| 92 |
+
# install displacy for pretty embed
|
| 93 |
pretty_embed(query_texts, output, list(support_texts.keys()))
|
| 94 |
```
|
| 95 |
|
|
|
|
| 96 |
<!DOCTYPE html>
|
| 97 |
<html lang="en">
|
| 98 |
<head>
|
|
|
|
| 120 |
</body>
|
| 121 |
</html>
|
| 122 |
|
|
|
|
| 123 |
## Datasets preparation
|
| 124 |
|
| 125 |
1. We need to convert dataset into the following format. Let's say we have a dataset file train.json like following.
|
| 126 |
+
2. Each list in supports are the examples of one entity type
|
| 127 |
+
3. Wrap entities around with [E] and [/E] in the examples.
|
| 128 |
+
4. Each example should have only one pair of [E] ... [/E].
|
| 129 |
|
| 130 |
```json
|
| 131 |
{
|
|
|
|
| 154 |
1. [train](https://gist.githubusercontent.com/sayef/46deaf7e6c6e1410b430ddc8aff9c557/raw/ea7ae2ae933bfc9c0daac1aa52a9dc093d5b36f4/ontonotes5.train.json)
|
| 155 |
2. [dev](https://gist.githubusercontent.com/sayef/46deaf7e6c6e1410b430ddc8aff9c557/raw/ea7ae2ae933bfc9c0daac1aa52a9dc093d5b36f4/ontonotes5.dev.json)
|
| 156 |
|
| 157 |
+
3. Then trainer script can be used to train/evaluate your fsner model.
|
| 158 |
|
| 159 |
```bash
|
| 160 |
+
fsner trainer --pretrained-model bert-base-uncased --mode train --train-data train.json --val-data val.json \
|
| 161 |
--train-batch-size 6 --val-batch-size 6 --n-examples-per-entity 10 --neg-example-batch-ratio 1/3 --max-epochs 25 --device gpu \
|
| 162 |
--gpus -1 --strategy ddp
|
| 163 |
```
|