-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
59 lines (40 loc) · 1.38 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import random
import logging
import torch
import numpy as np
from sklearn.metrics import f1_score
from transformers import AlbertConfig, AlbertTokenizer
from model import HashtagClassifier
MODEL_CLASSES = {
'albert': (AlbertConfig, HashtagClassifier, AlbertTokenizer),
}
MODEL_PATH_MAP = {
'albert': 'albert-base-v2'
}
def get_label(args):
return [label.strip() for label in open(os.path.join(args.data_dir, args.label_file), 'r', encoding='utf-8')]
def load_tokenizer(args):
return MODEL_CLASSES[args.model_type][2].from_pretrained(args.model_name_or_path)
def init_logger():
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO)
def set_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if not args.no_cuda and torch.cuda.is_available():
torch.cuda.manual_seed_all(args.seed)
def compute_metrics(preds, labels):
assert len(preds) == len(labels)
return acc_and_f1(preds, labels)
def simple_accuracy(preds, labels):
return (preds == labels).mean()
def acc_and_f1(preds, labels, average='macro'):
acc = simple_accuracy(preds, labels)
f1 = f1_score(y_true=labels, y_pred=preds, average=average)
return {
"acc": acc,
"f1": f1,
}