-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.py
executable file
·67 lines (56 loc) · 1.84 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
import random
import numpy as np
def plot_config(args):
out_str = "\noptim:{} lr:{} lamb:{}, d:{}, decay_rate:{}, batch_size:{}\n".format(
args.optim, args.lr, args.lamb, args.n_dim, args.decay_rate, args.n_batch)
print(out_str)
with open(args.perf_file, 'a') as f:
f.write(out_str)
def inplace_shuffle(*lists):
idx = []
for i in range(len(lists[0])):
idx.append(random.randint(0, i))
for ls in lists:
j = idx[i]
ls[i], ls[j] = ls[j], ls[i]
def batch_by_num(n_batch, *lists, n_sample=None):
if n_sample is None:
n_sample = len(lists[0])
for i in range(n_batch):
start = int(n_sample * i / n_batch)
end = int(n_sample * (i+1) / n_batch)
ret = [ls[start:end] for ls in lists]
if len(ret) > 1:
yield ret
else:
yield ret[0]
def batch_by_size(batch_size, *lists, n_sample=None):
if n_sample is None:
n_sample = len(lists[0])
start = 0
while(start < n_sample):
end = min(n_sample, start + batch_size)
ret = [ls[start:end] for ls in lists]
start += batch_size
if len(ret) > 1:
yield ret
else:
yield ret[0]
def gen_struct(num):
struct = []
for i in range(num):
struct.append(random.randint(0,3))
struct.append(random.randint(0,3))
struct.append(2*random.randint(0,1)-1)
return struct
def cal_ranks(probs, label):
sorted_idx = np.argsort(probs, axis=1)[:,::-1]
find_target = sorted_idx == np.expand_dims(label, 1)
ranks = np.nonzero(find_target)[1] + 1
return ranks
def cal_performance(ranks):
mrr = (1. / ranks).sum() / len(ranks)
m_r = sum(ranks) * 1.0 / len(ranks)
h_1 = sum(ranks<=1) * 1.0 / len(ranks)
h_10 = sum(ranks<=10) * 1.0 / len(ranks)
return mrr, m_r, h_1, h_10