-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
41 lines (33 loc) · 1.22 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
import torch
import numpy as np
import random
def set_seed(seed):
torch.manual_seed(seed)
np.random.seed(seed)
torch.cuda.manual_seed_all(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def random_perturb(feature_len, length):
r = np.linspace(0, feature_len, length + 1, dtype = np.uint16)
return r
# def norm(data):
# l2 = torch.norm(data, p = 2, dim = -1, keepdim = True)
# return torch.div(data, l2)
def save_best_record(test_info, file_path):
fo = open(file_path, "w")
fo.write("Step: {}\n".format(test_info["step"][-1]))
fo.write("auc: {:.4f}\n".format(test_info["auc"][-1]))
fo.write("ap: {:.4f}\n".format(test_info["ap"][-1]))
fo.write("loss: {:.4f}\n".format(test_info["loss"][-1]))
fo.write("accuracy: {:.4f}\n".format(test_info["accuracy"][-1]))
fo.close()
def process_feat(feat, length):
new_feat = np.zeros((length, feat.shape[1])).astype(np.float32)
r = np.linspace(0, len(feat), length+1, dtype=np.int32)
for i in range(length):
if r[i]!=r[i+1]:
new_feat[i,:] = np.mean(feat[r[i]:r[i+1],:], 0)
else:
new_feat[i,:] = feat[r[i], :]
return new_feat