Views
No views yet
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.
| identifier | epochs | datasets |
|---|---|---|
| sayef/fsner-bert-base-uncased | 25 | ontonotes5, conll2003, wnut2017, mit_movie_trivia, mit_restaurant and fin (Alvarado et al.). |
pip install fsner and import the model as shown in the code example belowpython install . and import the model as shown in the code example belowfsner/src directory to your PYTHONPATH and
import the model as shown in the code example below1import json
2
3from fsner import FSNERModel, FSNERTokenizerUtils, pretty_embed
4
5query_texts = [
6 "Does Luke's serve lunch?",
7 "Chang does not speak Taiwanese very well.",
8 "I like Berlin."
9]
10
11# Each list in supports are the examples of one entity type
12# Wrap entities around with [E] and [/E] in the examples.
13# Each sentence should have only one pair of [E] ... [/E]
14
15support_texts = {
16 "Restaurant": [
17 "What time does [E] Subway [/E] open for breakfast?",
18 "Is there a [E] China Garden [/E] restaurant in newark?",
19 "Does [E] Le Cirque [/E] have valet parking?",
20 "Is there a [E] McDonalds [/E] on main street?",
21 "Does [E] Mike's Diner [/E] offer huge portions and outdoor dining?"
22 ],
23 "Language": [
24 "Although I understood no [E] French [/E] in those days , I was prepared to spend the whole day with Chien - chien .",
25 "like what the hell 's that called in [E] English [/E] ? I have to register to be here like since I 'm a foreigner .",
26 "So , I 'm also working on an [E] English [/E] degree because that 's my real interest .",
27 "Al - Jazeera TV station , established in November 1996 in Qatar , is an [E] Arabic - language [/E] news TV station broadcasting global news and reports nonstop around the clock .",
28 "They think it 's far better for their children to be here improving their [E] English [/E] than sitting at home in front of a TV . \"",
29 "The only solution seemed to be to have her learn [E] French [/E] .",
30 "I have to read sixty pages of [E] Russian [/E] today ."
31 ]
32}
33
34device = 'cpu'
35
36tokenizer = FSNERTokenizerUtils("sayef/fsner-bert-base-uncased")
37queries = tokenizer.tokenize(query_texts).to(device)
38supports = tokenizer.tokenize(list(support_texts.values())).to(device)
39
40model = FSNERModel("sayef/fsner-bert-base-uncased")
41model.to(device)
42
43p_starts, p_ends = model.predict(queries, supports)
44
45# One can prepare supports once and reuse multiple times with different queries
46# ------------------------------------------------------------------------------
47# start_token_embeddings, end_token_embeddings = model.prepare_supports(supports)
48# p_starts, p_ends = model.predict(queries, start_token_embeddings=start_token_embeddings,
49# end_token_embeddings=end_token_embeddings)
50
51output = tokenizer.extract_entity_from_scores(query_texts, queries, p_starts, p_ends,
52 entity_keys=list(support_texts.keys()), thresh=0.50)
53
54print(json.dumps(output, indent=2))
55
56# install displacy for pretty embed
57pretty_embed(query_texts, output, list(support_texts.keys()))1{
2 "CARDINAL_NUMBER": [
3 "Washington , cloudy , [E] 2 [/E] to 6 degrees .",
4 "New Dehli , sunny , [E] 6 [/E] to 19 degrees .",
5 "Well this is number [E] two [/E] .",
6 "....."
7 ],
8 "LANGUAGE": [
9 "They do n't have the Quicken [E] Dutch [/E] version ?",
10 "they learned a lot of [E] German [/E] .",
11 "and then [E] Dutch [/E] it 's Mifrau",
12 "...."
13 ],
14 "MONEY": [
15 "Per capita personal income ranged from $ [E] 11,116 [/E] in Mississippi to $ 23,059 in Connecticut ... .",
16 "The trade surplus was [E] 582 million US dollars [/E] .",
17 "It settled with a loss of 4.95 cents at $ [E] 1.3210 [/E] a pound .",
18 "...."
19 ]
20}1fsner trainer --pretrained-model bert-base-uncased --mode train --train-data train.json --val-data val.json \
2 --train-batch-size 6 --val-batch-size 6 --n-examples-per-entity 10 --neg-example-batch-ratio 1/3 --max-epochs 25 --device gpu \
3 --gpus -1 --strategy ddp