Upload 7 files
Browse files- README_NER_Model.md +144 -0
- config.json +48 -0
- model.safetensors +3 -0
- special_tokens_map.json +7 -0
- tokenizer.json +0 -0
- tokenizer_config.json +59 -0
- vocab.txt +0 -0
README_NER_Model.md
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# BERT-Based Named Entity Recognition (NER) Model
|
3 |
+
|
4 |
+
This repository contains a fine-tuned BERT-based model for Named Entity Recognition (NER) using the WNUT-17 dataset. The model is trained using the Hugging Face Transformers and Datasets libraries, and supports inference and quantization for deployment in resource-constrained environments.
|
5 |
+
|
6 |
+
---
|
7 |
+
|
8 |
+
## Model Details
|
9 |
+
|
10 |
+
- **Model Name:** BERT-Base-Cased NER
|
11 |
+
- **Model Architecture:** BERT Base
|
12 |
+
- **Task:** Named Entity Recognition (NER)
|
13 |
+
- **Dataset:** WNUT-17 (from Hugging Face Datasets)
|
14 |
+
- **Quantization:** Float16
|
15 |
+
- **Fine-tuning Framework:** Hugging Face Transformers
|
16 |
+
|
17 |
+
---
|
18 |
+
|
19 |
+
## Usage
|
20 |
+
|
21 |
+
### Installation
|
22 |
+
|
23 |
+
```bash
|
24 |
+
pip install transformers datasets evaluate seqeval scikit-learn torch
|
25 |
+
```
|
26 |
+
|
27 |
+
### Training the Model
|
28 |
+
|
29 |
+
```python
|
30 |
+
from transformers import Trainer
|
31 |
+
|
32 |
+
trainer = Trainer(
|
33 |
+
model=model,
|
34 |
+
args=training_args,
|
35 |
+
train_dataset=tokenized_datasets["train"],
|
36 |
+
eval_dataset=tokenized_datasets["validation"],
|
37 |
+
tokenizer=tokenizer,
|
38 |
+
data_collator=data_collator,
|
39 |
+
compute_metrics=compute_metrics
|
40 |
+
)
|
41 |
+
|
42 |
+
trainer.train()
|
43 |
+
```
|
44 |
+
|
45 |
+
### Saving the Model
|
46 |
+
|
47 |
+
```python
|
48 |
+
model.save_pretrained("./saved_model")
|
49 |
+
tokenizer.save_pretrained("./saved_model")
|
50 |
+
```
|
51 |
+
|
52 |
+
### Testing the Saved Model
|
53 |
+
|
54 |
+
```python
|
55 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassification
|
56 |
+
|
57 |
+
model = AutoModelForTokenClassification.from_pretrained("./saved_model")
|
58 |
+
tokenizer = AutoTokenizer.from_pretrained("./saved_model")
|
59 |
+
ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
|
60 |
+
|
61 |
+
sample_sentences = [
|
62 |
+
"Barack Obama visited Microsoft headquarters in Redmond.",
|
63 |
+
"Nancy Gautam lives in Faridabad and studies at J.C. Bose University.",
|
64 |
+
"Google is launching a new AI product in California."
|
65 |
+
]
|
66 |
+
|
67 |
+
for sentence in sample_sentences:
|
68 |
+
print(f"Sentence: {sentence}")
|
69 |
+
print(ner_pipeline(sentence))
|
70 |
+
```
|
71 |
+
|
72 |
+
### Quantizing the Model
|
73 |
+
|
74 |
+
```python
|
75 |
+
import torch
|
76 |
+
|
77 |
+
quantized_model = model.to(dtype=torch.float16, device="cuda" if torch.cuda.is_available() else "cpu")
|
78 |
+
quantized_model.save_pretrained("quantized-model")
|
79 |
+
tokenizer.save_pretrained("quantized-model")
|
80 |
+
```
|
81 |
+
|
82 |
+
### Testing the Quantized Model
|
83 |
+
|
84 |
+
```python
|
85 |
+
model = AutoModelForTokenClassification.from_pretrained("quantized-model", torch_dtype=torch.float16)
|
86 |
+
tokenizer = AutoTokenizer.from_pretrained("quantized-model")
|
87 |
+
ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
|
88 |
+
```
|
89 |
+
|
90 |
+
---
|
91 |
+
|
92 |
+
## Performance Metrics
|
93 |
+
|
94 |
+
- **Accuracy:** Evaluated using seqeval on the validation split
|
95 |
+
- **Precision, Recall, F1 Score:** Computed using label-wise predictions excluding ignored indices
|
96 |
+
|
97 |
+
---
|
98 |
+
|
99 |
+
## Fine-Tuning Details
|
100 |
+
|
101 |
+
### Dataset
|
102 |
+
|
103 |
+
The model was fine-tuned on the WNUT-17 dataset, a benchmark dataset for emerging and rare named entities. The preprocessing includes:
|
104 |
+
- Tokenization using BERT tokenizer
|
105 |
+
- Label alignment for wordpiece tokens
|
106 |
+
|
107 |
+
### Training Configuration
|
108 |
+
|
109 |
+
- **Epochs:** 3
|
110 |
+
- **Batch Size:** 16
|
111 |
+
- **Learning Rate:** 2e-5
|
112 |
+
- **Max Length:** 128 tokens (implicitly handled by tokenizer)
|
113 |
+
- **Evaluation Strategy:** Per epoch
|
114 |
+
|
115 |
+
### Quantization
|
116 |
+
|
117 |
+
The model was quantized using PyTorch's half-precision (float16) support to reduce memory footprint and inference time.
|
118 |
+
|
119 |
+
---
|
120 |
+
|
121 |
+
## Repository Structure
|
122 |
+
|
123 |
+
```
|
124 |
+
.
|
125 |
+
├── saved_model/ # Fine-Tuned BERT Model and Tokenizer
|
126 |
+
├── quantized-model/ # Quantized Model for Deployment
|
127 |
+
├── ner_output/ # Training Logs and Checkpoints
|
128 |
+
├── README.md # Documentation
|
129 |
+
```
|
130 |
+
|
131 |
+
---
|
132 |
+
|
133 |
+
## Limitations
|
134 |
+
|
135 |
+
- May not generalize well to domains outside WNUT-17 entities
|
136 |
+
- Quantized model may slightly reduce accuracy for faster performance
|
137 |
+
|
138 |
+
---
|
139 |
+
|
140 |
+
## Contributing
|
141 |
+
|
142 |
+
Contributions are welcome! Please raise an issue or PR for improvements, bug fixes, or feature additions.
|
143 |
+
|
144 |
+
---
|
config.json
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_num_labels": 9,
|
3 |
+
"architectures": [
|
4 |
+
"BertForTokenClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"classifier_dropout": null,
|
8 |
+
"hidden_act": "gelu",
|
9 |
+
"hidden_dropout_prob": 0.1,
|
10 |
+
"hidden_size": 768,
|
11 |
+
"id2label": {
|
12 |
+
"0": "O",
|
13 |
+
"1": "B-MISC",
|
14 |
+
"2": "I-MISC",
|
15 |
+
"3": "B-PER",
|
16 |
+
"4": "I-PER",
|
17 |
+
"5": "B-ORG",
|
18 |
+
"6": "I-ORG",
|
19 |
+
"7": "B-LOC",
|
20 |
+
"8": "I-LOC"
|
21 |
+
},
|
22 |
+
"initializer_range": 0.02,
|
23 |
+
"intermediate_size": 3072,
|
24 |
+
"label2id": {
|
25 |
+
"B-LOC": 7,
|
26 |
+
"B-MISC": 1,
|
27 |
+
"B-ORG": 5,
|
28 |
+
"B-PER": 3,
|
29 |
+
"I-LOC": 8,
|
30 |
+
"I-MISC": 2,
|
31 |
+
"I-ORG": 6,
|
32 |
+
"I-PER": 4,
|
33 |
+
"O": 0
|
34 |
+
},
|
35 |
+
"layer_norm_eps": 1e-12,
|
36 |
+
"max_position_embeddings": 512,
|
37 |
+
"model_type": "bert",
|
38 |
+
"num_attention_heads": 12,
|
39 |
+
"num_hidden_layers": 12,
|
40 |
+
"output_past": true,
|
41 |
+
"pad_token_id": 0,
|
42 |
+
"position_embedding_type": "absolute",
|
43 |
+
"torch_dtype": "float16",
|
44 |
+
"transformers_version": "4.51.1",
|
45 |
+
"type_vocab_size": 2,
|
46 |
+
"use_cache": true,
|
47 |
+
"vocab_size": 28996
|
48 |
+
}
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5c5d159e14df95b39629e99909509193807b026b0efa977704d7068833fce608
|
3 |
+
size 215476426
|
special_tokens_map.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": "[CLS]",
|
3 |
+
"mask_token": "[MASK]",
|
4 |
+
"pad_token": "[PAD]",
|
5 |
+
"sep_token": "[SEP]",
|
6 |
+
"unk_token": "[UNK]"
|
7 |
+
}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {
|
3 |
+
"0": {
|
4 |
+
"content": "[PAD]",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false,
|
9 |
+
"special": true
|
10 |
+
},
|
11 |
+
"100": {
|
12 |
+
"content": "[UNK]",
|
13 |
+
"lstrip": false,
|
14 |
+
"normalized": false,
|
15 |
+
"rstrip": false,
|
16 |
+
"single_word": false,
|
17 |
+
"special": true
|
18 |
+
},
|
19 |
+
"101": {
|
20 |
+
"content": "[CLS]",
|
21 |
+
"lstrip": false,
|
22 |
+
"normalized": false,
|
23 |
+
"rstrip": false,
|
24 |
+
"single_word": false,
|
25 |
+
"special": true
|
26 |
+
},
|
27 |
+
"102": {
|
28 |
+
"content": "[SEP]",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false,
|
33 |
+
"special": true
|
34 |
+
},
|
35 |
+
"103": {
|
36 |
+
"content": "[MASK]",
|
37 |
+
"lstrip": false,
|
38 |
+
"normalized": false,
|
39 |
+
"rstrip": false,
|
40 |
+
"single_word": false,
|
41 |
+
"special": true
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"clean_up_tokenization_spaces": true,
|
45 |
+
"cls_token": "[CLS]",
|
46 |
+
"do_basic_tokenize": true,
|
47 |
+
"do_lower_case": false,
|
48 |
+
"extra_special_tokens": {},
|
49 |
+
"mask_token": "[MASK]",
|
50 |
+
"max_len": 512,
|
51 |
+
"model_max_length": 512,
|
52 |
+
"never_split": null,
|
53 |
+
"pad_token": "[PAD]",
|
54 |
+
"sep_token": "[SEP]",
|
55 |
+
"strip_accents": null,
|
56 |
+
"tokenize_chinese_chars": true,
|
57 |
+
"tokenizer_class": "BertTokenizer",
|
58 |
+
"unk_token": "[UNK]"
|
59 |
+
}
|
vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|