Views
No views yet
O: Outside of any entityB-PER: Beginning of a person entityI-PER: Inside a person entityB-ORG: Beginning of an organization entityI-ORG: Inside an organization entityB-LOC: Beginning of a location entityI-LOC: Inside a location entity1from transformers import pipeline
2
3# Load the NER pipeline
4nlp = pipeline("ner", model="yiwenX/bert-finetuned-ner-accelerate")
5
6# Example text
7text = "Apple Inc. was founded by Steve Jobs in Cupertino, California."
8
9# Get predictions
10results = nlp(text)
11print(results)1from transformers import AutoTokenizer, AutoModelForTokenClassification
2import torch
3
4# Load model and tokenizer
5tokenizer = AutoTokenizer.from_pretrained("yiwenX/bert-finetuned-ner-accelerate")
6model = AutoModelForTokenClassification.from_pretrained("yiwenX/bert-finetuned-ner-accelerate")
7
8# Example text
9text = "Apple Inc. was founded by Steve Jobs in Cupertino, California."
10
11# Tokenize and predict
12inputs = tokenizer(text, return_tensors="pt")
13with torch.no_grad():
14 outputs = model(**inputs)
15
16# Get predictions
17predictions = torch.argmax(outputs.logits, dim=-1)
18tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
19
20# Map predictions to labels
21label_list = model.config.id2label
22entities = []
23for token, pred in zip(tokens, predictions[0]):
24 if pred != 0: # 0 is 'O' (outside)
25 entities.append((token, label_list[pred.item()]))
26
27print(entities)| Epoch | Training Loss | Validation Loss | Precision | Recall | F1 | Accuracy |
|---|---|---|---|---|---|---|
| 1.0 | 0.3066 | 0.2636 | 78.23% | 81.86% | 80.00% | 91.89% |
| 2.0 | 0.2059 | 0.2566 | 79.60% | 83.37% | 81.44% | 92.42% |
| 3.0 | 0.1455 | 0.2777 | 80.92% | 84.07% | 82.47% | 92.50% |
1@misc{bert-finetuned-ner-accelerate,
2 author = {yiwenX},
3 title = {BERT Fine-tuned for Named Entity Recognition},
4 year = {2024},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/yiwenX/bert-finetuned-ner-accelerate}
7}