-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
49 lines (38 loc) · 1.2 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
import csv
from random import *
from collections import defaultdict
import pickle
from bigumbel import BiGumbelBox
from os.path import join
import torch
random_seed = 1
def get_vocab(filename):
word2idx = defaultdict()
with open(filename) as inputfile:
lines = inputfile.readlines()
for line in lines:
line = line.strip()
parts = line.split('\t')
word2idx[parts[1]] = parts[0]
return word2idx
def get_new_model(params):
model = BiGumbelBox(params.device, params.VOCAB_SIZE, params.DIM, params.NEG_PER_POS,
[1e-4, 0.01], [-0.1, -0.001], params).to(params.device)
return model
def load_hr_map(data_dir):
file = join(data_dir, 'ndcg_test.pickle')
with open(file, 'rb') as f:
hr_map = pickle.load(f)
return hr_map
def get_subset_of_given_relations(ids, rel_list):
subs = []
for r in rel_list:
sub = ids[(ids[:, 1] == r).nonzero().squeeze(1)] # sub triple set
subs.append(sub)
subset = torch.cat(subs, dim=0)
return subset
def load_hr_map(data_dir):
file = join(data_dir, 'ndcg_test.pickle')
with open(file, 'rb') as f:
hr_map = pickle.load(f)
return hr_map