-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
67 lines (48 loc) · 2.1 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
from typing import Tuple
import pandas as pd
import torch
import numpy as np
from sklearn.metrics import f1_score, classification_report, confusion_matrix as skcm
from torch import nn
from datetime import datetime, timedelta
COMMON_DS_PATH = "/content/drive/MyDrive/input_data/"
def get_file_to_df(filepath, **kwargs):
if filepath.endswith(".tsv"):
return pd.read_csv(filepath, sep="\t", **kwargs)
elif filepath.endswith(".csv"):
return pd.read_csv(filepath, **kwargs)
def get_run_timestr():
now = datetime.now() + timedelta(minutes=330)
date_time = now.strftime("%m-%d-%Y-%Hh%Mm%Ss")
return date_time
def accuracy(true, pred):
acc = np.sum((true == pred.argmax(-1)).astype(np.float32))
return float(100 * acc / len(true))
def f1_loss(y_true: torch.Tensor, y_pred: torch.Tensor) -> torch.Tensor:
f1 = f1_score(y_true, np.argmax(y_pred, axis=-1), average='macro')
return f1
def confusion_matrix(y_true, y_pred):
return skcm(y_true, np.argmax(y_pred, axis=-1), normalize="true")
def get_optimizer(cfg, params):
if cfg.optimizer == "adam":
return torch.optim.Adam(params, lr=cfg.learning_rate)
if cfg.optimizer == "adamw":
return torch.optim.AdamW(params, lr=cfg.learning_rate)
def get_scheduler(cfg, optimizer):
def half_cos_schedule(epoch):
# Taken from pycls/pycls/core/optimizer.py, since not clear from paper.
if epoch < cfg.warmup_epochs:
new_lr = (0.5 * (1.0 + np.cos(np.pi * epoch / cfg.epochs)) *
cfg.learning_rate)
alpha = epoch / cfg.warmup_epochs
warmup_factor = cfg.warmup_factor * (1.0 - alpha) + alpha
return new_lr * warmup_factor
else:
new_lr = (0.5 * (1.0 + np.cos(np.pi * epoch / cfg.epochs)) *
cfg.learning_rate)
return new_lr
return torch.optim.lr_scheduler.LambdaLR(optimizer, half_cos_schedule)
def get_classification_report(y_true, y_pred):
result_dict = classification_report(y_true, y_pred, output_dict=True)
report = pd.DataFrame(result_dict).transpose()
return report