Views
No views yet
1import torch
2import numpy as np
3from transformers import AutoTokenizer, AutoModelForTokenClassification
4
5tokenizer = AutoTokenizer.from_pretrained("psytechlab/wcl-wiki_rudeft__ner-model")
6model = AutoModelForTokenClassification.from_pretrained("psytechlab/wcl-wiki_rudeft__ner-model")
7model.eval()
8
9inputs = tokenizer('оромо — это африканская этническая группа, проживающая в эфиопии и в меньшей степени в кении.', return_tensors="pt")
10with torch.no_grad():
11 outputs = model(**inputs)
12
13logits = outputs.logits
14predictions = torch.argmax(logits, dim=-1)[0].tolist()
15
16tokens = inputs["input_ids"][0]
17word_ids = inputs.word_ids(batch_index=0)
18
19word_to_labels = {}
20for token_id, word_id, label_id in zip(tokens, word_ids, predictions):
21 if word_id is None:
22 continue
23 if word_id not in word_to_labels:
24 word_to_labels[word_id] = []
25 word_to_labels[word_id].append(label_id)
26
27word_level_predictions = [model.config.id2label[labels[0]] for labels in word_to_labels.values()]
28
29print(word_level_predictions)
30# ['B-Term', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']1training_args = TrainingArguments(
2 eval_strategy="epoch",
3 save_strategy="epoch",
4 learning_rate=2e-5,
5 num_train_epochs=7,
6 weight_decay=0.01,
7)psytechlab/rus_rudeft_wcl-wiki:1 precision recall f1-score support
2
3I-Definition 0.75 0.90 0.82 3344
4B-Definition 0.62 0.73 0.67 230
5 I-Term 0.80 0.85 0.82 524
6 O 0.97 0.91 0.94 11359
7 B-Term 0.96 0.93 0.94 2977
8
9 accuracy 0.91 18434
10 macro avg 0.82 0.87 0.84 18434
11weighted avg 0.92 0.91 0.91 18434astromis/ruDEFT:1 precision recall f1-score support
2
3I-Definition 0.90 0.90 0.90 3344
4B-Definition 0.74 0.73 0.74 230
5 I-Term 0.83 0.87 0.85 389
6 O 0.86 0.86 0.86 2222
7 B-Term 0.87 0.85 0.86 638
8
9 accuracy 0.87 6823
10 macro avg 0.84 0.84 0.84 6823
11weighted avg 0.87 0.87 0.87 6823astromis/WCL_Wiki_Ru:1 precision recall f1-score support
2
3I-Definition 0.00 0.00 0.00 0
4B-Definition 0.00 0.00 0.00 0
5 I-Term 0.72 0.78 0.75 135
6 O 1.00 0.93 0.96 9137
7 B-Term 0.99 0.95 0.97 2339
8
9 accuracy 0.93 11611
10 macro avg 0.54 0.53 0.54 11611
11weighted avg 0.99 0.93 0.96 11611