forked from JasonLee-22/TR-Rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn.py
127 lines (109 loc) · 4.31 KB
/
learn.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import time
import argparse
import numpy as np
from datetime import datetime
from joblib import Parallel, delayed
from grapher import Grapher
from temporal_walk import Temporal_Walk
from rule_learning import Rule_Learner, rules_statistics
import scipy.sparse as sp
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", "-d", default="", type=str)
parser.add_argument("--rule_lengths", "-l", default="3", type=int, nargs="+")
parser.add_argument("--num_walks", "-n", default="100", type=int)
parser.add_argument("--transition_distr", default="exp", type=str)
parser.add_argument("--num_processes", "-p", default=1, type=int)
parser.add_argument("--seed", "-s", default=None, type=int)
parser.add_argument("--acyclic_walks", "-aw", default=5000, type=int)
parsed = vars(parser.parse_args())
dataset = parsed["dataset"]
rule_lengths = parsed["rule_lengths"]
rule_lengths = [rule_lengths] if (type(rule_lengths) == int) else rule_lengths
num_walks = parsed["num_walks"]
transition_distr = parsed["transition_distr"]
num_processes = parsed["num_processes"]
seed = parsed["seed"]
acyclic_walks = parsed["acyclic_walks"]
dataset_dir = "../data/" + dataset + "/"
data = Grapher(dataset_dir)
data.prepro()
temporal_walk = Temporal_Walk(data.train_idx, data.inv_relation_id, transition_distr, dataset_dir, data.train_times)
rl = Rule_Learner(temporal_walk.edges, data.id2relation, data.inv_relation_id, dataset)
all_relations = sorted(temporal_walk.edges) # Learn for all relations
def learn_rules(i, num_relations):
"""
Learn rules (multiprocessing possible).
Parameters:
i (int): process number
num_relations (int): minimum number of relations for each process
Returns:
rl.rules_dict (dict): rules dictionary
"""
if seed:
np.random.seed(seed)
num_rest_relations = len(all_relations) - (i + 1) * num_relations
if num_rest_relations >= num_relations:
relations_idx = range(i * num_relations, (i + 1) * num_relations)
else:
relations_idx = range(i * num_relations, len(all_relations))
num_rules = [0]
for k in relations_idx:
rel = all_relations[k]
for length in rule_lengths:
it_start = time.time()
for _ in range(num_walks):
walk_successful, walk = temporal_walk.sample_walk(length + 1, rel)
if walk_successful:
rl.create_rule(walk)
it_end = time.time()
it_time = round(it_end - it_start, 6)
num_rules.append(sum([len(v) for k, v in rl.rules_dict.items()]) // 2)
num_new_rules = num_rules[-1] - num_rules[-2]
print(
"Process {0}: relation {1}/{2}, length {3}: {4} sec, {5} rules".format(
i,
k - relations_idx[0] + 1,
len(relations_idx),
length,
it_time,
num_new_rules,
)
)
#Acyclic sampling
it_start = time.time()
for _ in range(acyclic_walks):
walk_successful, walk = temporal_walk.Acyclic_sample(rel)
if walk_successful:
rl.create_acyclic(walk)
it_end = time.time()
it_time = round(it_end - it_start, 6)
num_rules.append(sum([len(v) for k, v in rl.rules_dict.items()]) // 2)
num_new_rules = num_rules[-1] - num_rules[-2]
print(
"Process {0}: relation {1}/{2}, Acyclic: {3} sec, {4} rules".format(
i,
k - relations_idx[0] + 1,
len(relations_idx),
it_time,
num_new_rules,
)
)
return rl.rules_dict
start = time.time()
num_relations = len(all_relations) // num_processes
output = Parallel(n_jobs=num_processes)(
delayed(learn_rules)(i, num_relations) for i in range(num_processes)
)
end = time.time()
all_rules = output[0]
for i in range(1, num_processes):
all_rules.update(output[i])
total_time = round(end - start, 6)
print("Learning finished in {} seconds.".format(total_time))
rl.rules_dict = all_rules
rl.sort_rules_dict()
dt = datetime.now()
dt = dt.strftime("%d%m%y%H%M%S")
rl.save_rules(dt, rule_lengths, num_walks, transition_distr, seed)
rl.save_rules_verbalized(dt, rule_lengths, num_walks, transition_distr, seed)
rules_statistics(rl.rules_dict)