-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
72 lines (58 loc) · 1.98 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
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import torch
import random
import logging
import numpy as np
import pandas as pd
from collections import Counter
from sklearn.metrics import f1_score
#https://huggingface.co/j-hartmann/emotion-english-distilroberta-base (emo-distil-roberta)
#https://huggingface.co/arpanghoshal/EmoRoBERTa (emo-roberta)
model_dict = {
"bert": "bert-base-uncased",
"roberta":"roberta-base",
"distilbert":"distilbert-base-uncased",
"distil-roberta": "distilroberta-base",
"emotion":"bhadresh-savani/bert-base-uncased-emotion",
"distil-emotion":"bhadresh-savani/distilbert-base-uncased-emotion",
"distil-roberta-emotion": "j-hartmann/emotion-english-distilroberta-base",
"emo-roberta":"arpanghoshal/EmoRoBERTa",
"psych": "mnaylor/psychbert-cased",
"mental":"mental/mental-bert-base-uncased",
}
NUM_FOLDS = 5
#NUM_FOLDS = 1
def set_seed():
seed = 100
torch.manual_seed(seed)
random.seed(seed)
np.random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.enabled = False
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
def set_logger(log_path):
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if not logger.handlers:
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(logging.Formatter("%(message)s"))
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter("%(message)s"))
logger.addHandler(stream_handler)
def get_weights(labels):
class_num = len(set(labels))
weights = []
for c in range(class_num):
if labels.count(c) > 0:
weights.append(len(labels)/labels.count(c))
else:
weights.append(0)
if sum(weights) > 0:
weights = [float(i) / sum(weights) for i in weights]
else:
weights = [1.0]*len(weights)
return weights
class Object(object):
pass