Views
No views yet
hidden_dim=128, yielding a 256-dimensional concatenated sequence representation).ignore_index=-100 ensuring pad tokens do not affect loss or performance evaluations.
| Class / Tag ID | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| 0 (e.g., O) | 0.96 | 0.99 | 0.98 | 81,082 |
| 1 | 0.89 | 0.73 | 0.80 | 3,459 |
| 2 | 0.90 | 0.78 | 0.83 | 2,463 |
| 3 | 0.83 | 0.66 | 0.74 | 3,002 |
| 4 | 0.81 | 0.69 | 0.75 | 1,586 |
| 5 | 0.84 | 0.78 | 0.81 | 3,505 |
| 6 | 0.62 | 0.68 | 0.65 | 514 |
| 7 | 0.82 | 0.68 | 0.74 | 1,624 |
| 8 | 0.66 | 0.63 | 0.65 | 562 |
| Macro Average | 0.82 | 0.74 | 0.77 | 97,797 |
| Weighted Average | 0.94 | 0.94 | 0.94 | 97,797 |

ORG for organizations, LOC for locations, or PER for persons).displacy.render specifically highlights the structural Named Entity Recognition (NER) tag segments extracted by the token prediction head, these entity boundaries are intrinsically anchored by the shared hidden representations refined by the parallel POS and Chunk tagging heads during the joint optimization process.trust_remote_code=True when loading via AutoModel.1import torch
2import json
3import urllib.request
4from transformers import AutoConfig, AutoModel
5
6# 1. Define repository identifier and vocabulary URL
7repo_id = "ILoveBacteria/bilstm-sequence-labeler"
8vocab_url = "https://huggingface.co/ILoveBacteria/bilstm-sequence-labeler/resolve/main/vocab.json"
9
10# 2. Load custom model architecture and weights
11model = AutoModel.from_pretrained(repo_id, trust_remote_code=True)
12model.eval()
13
14# 3. Download and load the vocabulary file directly from the link
15with urllib.request.urlopen(vocab_url) as response:
16 vocab = json.loads(response.read().decode("utf-8"))
17
18token2id = vocab["token2id"]
19
20# Simple inference example
21text = "EU rejects German call to boycott British lamb ."
22tokens = text.split()
23input_ids = [token2id.get(token, token2id["<unk>"]) for token in tokens]
24input_tensor = torch.tensor([input_ids]) # Add batch dimension
25
26with torch.no_grad():
27 outputs = model(input_tensor)
28
29# Extract predictions for tasks
30ner_predictions = outputs["ner"].argmax(dim=-1)[0]
31print("Predicted NER IDs:", ner_predictions.tolist())displacy engine.1from spacy import displacy
2
3# 1. Map for CoNLL-2003 NER indices to human-readable labels
4ner_labels = {
5 0: "O", 1: "B-PER", 2: "I-PER", 3: "B-ORG", 4: "I-ORG",
6 5: "B-LOC", 6: "I-LOC", 7: "B-MISC", 8: "I-MISC"
7}
8
9# 2. Convert predicted tensor to a flat NumPy array or list
10preds = ner_predictions.cpu().numpy()
11
12# 3. Reconstruct the string text and track exact character offsets for displaCy
13text = " ".join(tokens)
14ents = []
15char_offsets = []
16cursor = 0
17
18for tok in tokens:
19 char_offsets.append((cursor, cursor + len(tok)))
20 cursor += len(tok) + 1 # +1 accounts for the space between tokens
21
22# 4. Construct the entity metadata required by displaCy
23for i, pred_idx in enumerate(preds):
24 label = ner_labels.get(int(pred_idx), "O")
25 if label != "O":
26 start, end = char_offsets[i]
27 ents.append({"start": start, "end": end, "label": label})
28
29# 5. Pack the document structural payload
30doc_data = {
31 "text": text,
32 "ents": ents
33}
34
35# 6. Render the visualization inline (Jupyter Notebook / Google Colab)
36displacy.render(doc_data, style="ent", manual=True, jupyter=True)