File size: 3,374 Bytes
5324d02
 
 
 
 
 
 
 
 
 
3ca367c
 
 
 
 
 
 
 
 
ee235c4
3ca367c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01ec875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af6fdfe
01ec875
 
 
 
 
 
af6fdfe
 
 
 
01ec875
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
license: apache-2.0
datasets:
- eriktks/conll2003
language:
- en
base_model:
- chandar-lab/NeoBERT
tags:
- ner
---

# ✨ NeoBERT for NER

This repository hosts an NeoBERT model that was fine-tuned on the CoNLL-2003 NER dataset.

Please notice the following caveats:

* ⚠️ Work in progress, as e.g. new hyper-parameter changes or bug fixes for the implemented `NeoBERTForTokenClassification` class can occur.
* ⚠️ At the moment, don't expect BERT-like performance, more experiments are needed. (Is RoPE causing this?)

## 📝 Implementation

An own `NeoBERTForTokenClassification` class was implemented to conduct experiments with Transformers.

For all experiments, Transformers in version `4.50.0.dev0` is currently used including a recent built of `xFormers`, as NeoBERT depends on that for the `SwiGLU` implementation.

For following code (based on the [PyTorch Token Classification example](https://github.com/huggingface/transformers/tree/main/examples/pytorch/token-classification)
can be used for fine-tuning:

```bash
python3 run_ner.py \
  --model_name_or_path /home/stefan/Repositories/NeoBERT \
  --dataset_name conll2003 \
  --output_dir ./neobert-conll2003-lr1e-05-e10-bs16-1 \
  --seed 1 \
  --do_train \
  --do_eval \
  --per_device_train_batch_size 16 \
  --num_train_epochs 10 \
  --learning_rate 1e-05 \
  --eval_strategy epoch \
  --save_strategy epoch \
  --overwrite_output_dir \
  --trust_remote_code True \
  --load_best_model_at_end \
  --metric_for_best_model "eval_f1" \
  --greater_is_better True
```

## 📊 Performance

A very basic hyper-parameter search is performanced for five different seeds, with reported averaged micro F1-Score on the development set of CoNLL-2003:

| Configuration         | Run 1     | Run 2 | Run 3 | Run 4 | Run 5 | Avg.  |
|:--------------------- | ---------:| -----:| -----:| -----:| -----:| -----:|
| `bs=16,e=10,lr=1e-05` | **95.71** | 95.42 | 95.53 | 95.56 | 95.43 | 95.53 |
| `bs=16,e=10,lr=2e-05` | 95.25     | 95.33 | 95.28 | 95.35 | 95.26 | 95.29 |
| `bs=16,e=10,lr=3e-05` | 94.98     | 95.22 | 94.86 | 94.72 | 94.93 | 94.94 |
| `bs=16,e=10,lr=4e-05` | 94.61     | 94.39 | 94.57 | 94.65 | 94.87 | 94.61 |
| `bs=16,e=10,lr=5e-05` | 93.82     | 93.94 | 94.36 | 91.14 | 94.38 | 94.15 |

The performance of the current uploaded model is marked in bold.

## 📣 Usage

The following code can be used to test the model and recognize named entities for a given sentence:

```python
from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer

model_name = "stefan-it/neobert-ner-conll03"


model = AutoModelForTokenClassification.from_pretrained(model_name, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)

ner = pipeline(task="ner",
               model=model,
               tokenizer=tokenizer,
               trust_remote_code=True)

print(ner("George Washington went to Washington in the US."))
```

This outputs:

```json
[
 {'entity': 'B-PER', 'score': 0.99981505, 'index': 1, 'word': 'george', 'start': 0, 'end': 6},
 {'entity': 'I-PER', 'score': 0.9997435, 'index': 2, 'word': 'washington', 'start': 7, 'end': 17},
 {'entity': 'B-LOC', 'score': 0.99955124, 'index': 5, 'word': 'washington', 'start': 26, 'end': 36},
 {'entity': 'B-LOC', 'score': 0.99958867, 'index': 8, 'word': 'us', 'start': 44, 'end': 46}
]
```