Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Named Entity Recognition (NER) of Information Extraction
|
| 2 |
+
|
| 3 |
+
## π Overview
|
| 4 |
+
This repository hosts the quantized version of the `bert-base-cased` model for Named Entity Recognition (NER) using the CoNLL-2003 dataset. The model is specifically designed to recognize entities related to **Person (PER), Organization (ORG), and Location (LOC)**. The model has been optimized for efficient deployment while maintaining high accuracy, making it suitable for resource-constrained environments.
|
| 5 |
+
|
| 6 |
+
## π Model Details
|
| 7 |
+
- **Model Architecture**: BERT Base Cased
|
| 8 |
+
- **Task**: Named Entity Recognition (NER) of Information Extraction
|
| 9 |
+
- **Dataset**: Hugging Face's CoNLL-2003
|
| 10 |
+
- **Quantization**: BrainFloat16
|
| 11 |
+
- **Fine-tuning Framework**: Hugging Face Transformers
|
| 12 |
+
|
| 13 |
+
---
|
| 14 |
+
## π Usage
|
| 15 |
+
|
| 16 |
+
### Installation
|
| 17 |
+
```bash
|
| 18 |
+
pip install transformers torch
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
### Loading the Model
|
| 22 |
+
```python
|
| 23 |
+
from transformers import BertTokenizerFast, BertForTokenClassification
|
| 24 |
+
import torch
|
| 25 |
+
|
| 26 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
+
|
| 28 |
+
model_name = "AventIQ-AI/named-entity-recognition-for-information-extraction"
|
| 29 |
+
model = BertForTokenClassification.from_pretrained(model_name).to(device)
|
| 30 |
+
tokenizer = BertTokenizerFast.from_pretrained(model_name)
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
### Named Entity Recognition Inference
|
| 34 |
+
```python
|
| 35 |
+
label_list = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "B-MISC", "I-MISC"]
|
| 36 |
+
```
|
| 37 |
+
### **πΉ Labeling Scheme (BIO Format)**
|
| 38 |
+
|
| 39 |
+
- **B-XYZ (Beginning)**: Indicates the beginning of an entity of type XYZ (e.g., B-PER for the beginning of a personβs name).
|
| 40 |
+
- **I-XYZ (Inside)**: Represents subsequent tokens inside an entity (e.g., I-PER for the second part of a personβs name).
|
| 41 |
+
- **O (Outside)**: Denotes tokens that are not part of any named entity.
|
| 42 |
+
|
| 43 |
+
```
|
| 44 |
+
def predict_entities(text, model):
|
| 45 |
+
|
| 46 |
+
tokens = tokenizer(text, return_tensors="pt", truncation=True)
|
| 47 |
+
tokens = {key: val.to(device) for key, val in tokens.items()} # Move to CUDA
|
| 48 |
+
|
| 49 |
+
with torch.no_grad():
|
| 50 |
+
outputs = model(**tokens)
|
| 51 |
+
|
| 52 |
+
logits = outputs.logits # Extract logits
|
| 53 |
+
predictions = torch.argmax(logits, dim=2) # Get highest probability labels
|
| 54 |
+
|
| 55 |
+
tokens_list = tokenizer.convert_ids_to_tokens(tokens["input_ids"][0])
|
| 56 |
+
predicted_labels = [label_list[pred] for pred in predictions[0].cpu().numpy()]
|
| 57 |
+
|
| 58 |
+
final_tokens = []
|
| 59 |
+
final_labels = []
|
| 60 |
+
for token, label in zip(tokens_list, predicted_labels):
|
| 61 |
+
if token.startswith("##"):
|
| 62 |
+
final_tokens[-1] += token[2:] # Merge subword
|
| 63 |
+
else:
|
| 64 |
+
final_tokens.append(token)
|
| 65 |
+
final_labels.append(label)
|
| 66 |
+
|
| 67 |
+
for token, label in zip(final_tokens, final_labels):
|
| 68 |
+
if token not in ["[CLS]", "[SEP]"]:
|
| 69 |
+
print(f"{token}: {label}")
|
| 70 |
+
|
| 71 |
+
# π Test Example
|
| 72 |
+
sample_text = "Elon Musk is the CEO of Tesla, which is based in California."
|
| 73 |
+
predict_entities(sample_text, model)
|
| 74 |
+
```
|
| 75 |
+
---
|
| 76 |
+
## π Evaluation Results for Quantized Model
|
| 77 |
+
|
| 78 |
+
### **πΉ Overall Performance**
|
| 79 |
+
|
| 80 |
+
- **Accuracy**: **97.10%** β
|
| 81 |
+
- **Precision**: **89.52%**
|
| 82 |
+
- **Recall**: **90.67%**
|
| 83 |
+
- **F1 Score**: **90.09%**
|
| 84 |
+
|
| 85 |
+
---
|
| 86 |
+
|
| 87 |
+
### **πΉ Performance by Entity Type**
|
| 88 |
+
|
| 89 |
+
| Entity Type | Precision | Recall | F1 Score | Number of Entities |
|
| 90 |
+
|------------|-----------|--------|----------|--------------------|
|
| 91 |
+
| **LOC** (Location) | **91.46%** | **92.07%** | **91.76%** | 3,000 |
|
| 92 |
+
| **MISC** (Miscellaneous) | **71.25%** | **72.83%** | **72.03%** | 1,266 |
|
| 93 |
+
| **ORG** (Organization) | **89.83%** | **93.02%** | **91.40%** | 3,524 |
|
| 94 |
+
| **PER** (Person) | **95.16%** | **94.04%** | **94.60%** | 2,989 |
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
#### β³ **Inference Speed Metrics**
|
| 98 |
+
- **Total Evaluation Time**: 15.89 sec
|
| 99 |
+
- **Samples Processed per Second**: 217.26
|
| 100 |
+
- **Steps per Second**: 27.18
|
| 101 |
+
- **Epochs Completed**: 3
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
## Fine-Tuning Details
|
| 105 |
+
### Dataset
|
| 106 |
+
The Hugging Face's `CoNLL-2003` dataset was used, containing texts and their ner tags.
|
| 107 |
+
|
| 108 |
+
## π Training Details
|
| 109 |
+
- **Number of epochs**: 3
|
| 110 |
+
- **Batch size**: 8
|
| 111 |
+
- **Evaluation strategy**: epoch
|
| 112 |
+
- **Learning Rate**: 2e-5
|
| 113 |
+
|
| 114 |
+
### β‘ Quantization
|
| 115 |
+
Post-training quantization was applied using PyTorch's built-in quantization framework to reduce the model size and improve inference efficiency.
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
## π Repository Structure
|
| 119 |
+
```
|
| 120 |
+
.
|
| 121 |
+
βββ model/ # Contains the quantized model files
|
| 122 |
+
βββ tokenizer_config/ # Tokenizer configuration and vocabulary files
|
| 123 |
+
βββ model.safetensors/ # Quantized Model
|
| 124 |
+
βββ README.md # Model documentation
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
---
|
| 128 |
+
## β οΈ Limitations
|
| 129 |
+
- The model may not generalize well to domains outside the fine-tuning dataset.
|
| 130 |
+
- Quantization may result in minor accuracy degradation compared to full-precision models.
|
| 131 |
+
|
| 132 |
+
---
|
| 133 |
+
## π€ Contributing
|
| 134 |
+
Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions or improvements.
|