Views
No views yet
1from transformers import BertForTokenClassification, AutoTokenizer
2
3import torch
4text = "サンプルテキスト"
5model_name = "yseop/SMM4H2024_Task2a_ja"
6with torch.inference_mode():
7 model = BertForTokenClassification.from_pretrained(model_name).eval()
8 tokenizer = AutoTokenizer.from_pretrained(model_name)
9 idx2tag = model.config.id2label
10 vecs = tokenizer(text,
11 padding=True,
12 truncation=True,
13 return_tensors="pt")
14 ner_logits = model(input_ids=vecs["input_ids"],
15 attention_mask=vecs["attention_mask"])
16 idx = torch.argmax(ner_logits.logits, dim=2).detach().cpu().numpy().tolist()[0]
17 token = [tokenizer.convert_ids_to_tokens(v) for v in vecs["input_ids"]][0][1:-1]
18 pred_tag = [idx2tag[x] for x in idx][1:-1]| NE | tp | fp | fn | precision | recall | f1 |
|---|---|---|---|---|---|---|
| DISORDER | 588 | 409 | 330 | 0.5898 | 0.6405 | 0.6141 |
| DRUG | 307 | 143 | 169 | 0.6822 | 0.645 | 0.6631 |
| FUNCTION | 69 | 160 | 170 | 0.3013 | 0.2887 | 0.2949 |
| all | 964 | 712 | 669 | 0.5752 | 0.5903 | 0.5827 |