-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
67 lines (50 loc) · 2.47 KB
/
util.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
60
61
62
63
64
65
66
67
import logging
import random
import numpy as np
from sklearn.metrics import f1_score, accuracy_score
import torch
from transformers import AdamW, get_linear_schedule_with_warmup
from category_id_map import lv2id_to_lv1id
def setup_device(args):
args.device = 'cuda' if torch.cuda.is_available() else 'cpu'
args.n_gpu = torch.cuda.device_count()
def setup_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
def setup_logging(args):
logging.basicConfig(filename=f'{args.savedmodel_path}/train.log',
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO)
logger = logging.getLogger(__name__)
return logger
def build_optimizer(args, model):
# Prepare optimizer and schedule (linear warmup and decay)
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
'weight_decay': args.weight_decay},
{'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=args.warmup_steps,
num_training_steps=args.max_steps)
return optimizer, scheduler
def evaluate(predictions, labels):
# prediction and labels are all level-2 class ids
lv1_preds = [lv2id_to_lv1id(lv2id) for lv2id in predictions]
lv1_labels = [lv2id_to_lv1id(lv2id) for lv2id in labels]
lv2_f1_micro = f1_score(labels, predictions, average='micro')
lv2_f1_macro = f1_score(labels, predictions, average='macro')
lv1_f1_micro = f1_score(lv1_labels, lv1_preds, average='micro')
lv1_f1_macro = f1_score(lv1_labels, lv1_preds, average='macro')
mean_f1 = (lv2_f1_macro + lv1_f1_macro + lv1_f1_micro + lv2_f1_micro) / 4.0
eval_results = {'lv1_acc': accuracy_score(lv1_labels, lv1_preds),
'lv2_acc': accuracy_score(labels, predictions),
'lv1_f1_micro': lv1_f1_micro,
'lv1_f1_macro': lv1_f1_macro,
'lv2_f1_micro': lv2_f1_micro,
'lv2_f1_macro': lv2_f1_macro,
'mean_f1': mean_f1}
return eval_results