-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.py
305 lines (278 loc) · 10.6 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import argparse
import json
import os
import time
from typing import List
import numpy as np
from gensim.models import Word2Vec
from src.finetuning import (
run_finetuning_wkfl2,
run_finetuning_wkfl3,
setup_finetuning_input,
)
from src.pretraining import run_pretraining, setup_pretraining_input
from src.processing.context_generator import ContextGenerator
from src.processing.generic_attributed_graph import GenericGraph
from src.utils.data_utils import load_pretrained_node2vec
from src.utils.evaluation import run_evaluation_main
from src.utils.link_predict import find_optimal_cutoff, link_prediction_eval
from src.utils.utils import get_id_map, load_pickle
def get_set_embeddings_details(args):
if not args.pretrained_embeddings:
if args.pretrained_method == "compgcn":
args.pretrained_embeddings = (
f"{args.emb_dir}/{args.data_name}/"
f"act_{args.data_name}_{args.node_edge_composition_func}_500.out"
)
elif args.pretrained_method == "node2vec":
args.base_embedding_dim = 128
args.pretrained_embeddings = (
f"{args.emb_dir}/{args.data_name}/{args.data_name}.emd"
)
else:
if args.pretrained_method == "compgcn":
args.base_embedding_dim = 200
elif args.pretrained_method == "node2vec":
args.base_embedding_dim = 128
return args.pretrained_embeddings, args.base_embedding_dim
def get_graph(data_path, false_edge_gen):
print("\n Loading graph...")
attr_graph = GenericGraph(data_path, false_edge_gen)
context_gen = ContextGenerator(attr_graph, args.num_walks_per_node)
return attr_graph, context_gen
def get_test_edges(paths: List[str], sep: str):
# edges = set()
edges = []
for path in paths:
with open(path, "r") as f:
for line in f:
tokens = line.strip().split(sep)
etype = tokens[0]
source = tokens[1]
destination = tokens[2]
label = tokens[3]
edge = (etype, source, destination, label)
# edges.add(edge)
edges.append(edge)
return edges
def main(args):
data_path = f"{args.data_path}/{args.data_name}"
attr_graph, context_gen = get_graph(data_path, args.false_edge_gen)
attr_graph.dump_stats()
stime = time.time()
id_maps_dir = data_path
ent2id = get_id_map(f"{id_maps_dir}/ent2id.txt")
rel2id = get_id_map(f"{id_maps_dir}/rel2id.txt")
print(len(ent2id), len(rel2id))
# Load pretrained embedding from CompGCN
if args.pretrained_method == "compgcn":
pretrained_node_embedding = load_pickle(args.pretrained_embeddings)
elif args.pretrained_method == "node2vec":
pretrained_node_embedding = load_pretrained_node2vec(
args.pretrained_embeddings, ent2id, args.base_embedding_dim
)
print(
"No. of nodes with pretrained embedding: ",
len(pretrained_node_embedding),
)
valid_path = data_path + "/valid.txt"
valid_edges_paths = [valid_path]
valid_edges = list(get_test_edges(valid_edges_paths, " "))
test_path = data_path + "/test.txt"
test_edges_paths = [test_path]
test_edges = list(get_test_edges(test_edges_paths, " "))
print("No. edges in test data: ", len(test_edges))
if args.is_pre_trained:
link_prediction_eval(
valid_edges, test_edges, ent2id, rel2id, pretrained_node_embedding
)
else:
print("No pretrained embedding, no need to evaluate workflow 1.\n")
print("***************PRETRAINING***************")
pre_num_batches = setup_pretraining_input(args, attr_graph, context_gen, data_path)
print("\n Run model for pre-training ...")
# Masked nodes prediction
pred_data, true_data = run_pretraining(
args, attr_graph, pre_num_batches, pretrained_node_embedding, ent2id, rel2id
)
print("\n Begin evaluation for node prediction...")
# accu, mse = run_evaluation(pred_data, true_data)
# Link prediction
ft_num_batches = setup_finetuning_input(args, attr_graph, context_gen)
pred_data, true_data = run_finetuning_wkfl2(
args, attr_graph, ft_num_batches, pretrained_node_embedding, ent2id, rel2id
)
print("\n Begin evaluation for link prediction...")
valid_true_data = np.array(true_data["valid"])
threshold = find_optimal_cutoff(valid_true_data, pred_data["valid"])[0]
run_evaluation_main(
test_edges, pred_data["test"], true_data["test"], threshold, header="workflow2"
)
# evaluate_all_epochs(args, attr_graph, ft_num_batches,
# pretrained_node_embedding, ent2id, rel2id, test_edges)
# WORKFLOW_3
print("***************FINETUNING***************")
print("\n Run model for finetuning ...")
(
pred_data_test,
true_data_test,
pred_data_valid,
true_data_valid,
) = run_finetuning_wkfl3(
args, attr_graph, ft_num_batches, pretrained_node_embedding, ent2id, rel2id
)
print("\n Begin evaluation for link prediction...")
valid_true_data = np.array(true_data_valid)
threshold = find_optimal_cutoff(valid_true_data, pred_data_valid)[0]
# save the threshold values for later use
json.dump(threshold, open(args.outdir + "/thresholds.json", "w"))
run_evaluation_main(
test_edges, pred_data_test, true_data_test, threshold, header="workflow3"
)
# evaluate after context inference
etime = time.time()
elapsed = etime - stime
print(f"running time(seconds) on {args.data_name} data: {elapsed}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--data_name", default="amazon_s", help="name of the dataset")
parser.add_argument("--data_path", default="data", help="path to dataset")
parser.add_argument("--outdir", default="output/default", help="path to output dir")
parser.add_argument(
"--pretrained_embeddings", help="absolute path to pretrained embeddings"
)
parser.add_argument(
"--pretrained_method", default="node2vec", help="compgcn|node2vec"
)
# Walks options
parser.add_argument(
"--beam_width",
default=2,
type=int,
help="beam width used for generating random walks",
)
parser.add_argument(
"--num_walks_per_node", default=1, type=int, help="walks per node"
)
parser.add_argument("--walk_type", default="dfs", help="walk type bfs/dfs")
parser.add_argument("--max_length", default=6, type=int, help="max length of walks")
parser.add_argument(
"--n_pred", default=1, help="number of tokens masked to be predicted"
)
parser.add_argument(
"--max_pred", default=1, help="max number of tokens masked to be predicted"
)
# Pretraining options
parser.add_argument("--lr", default=0.0001, type=float, help="learning rate")
parser.add_argument(
"--n_epochs", default=10, type=int, help="number of epochs for training"
)
parser.add_argument(
"--checkpoint", default=20, type=int, help="checkpoint for validation"
)
parser.add_argument(
"--base_embedding_dim",
default=200,
type=int,
help="dimension of base embedding",
)
parser.add_argument(
"--batch_size",
default=128,
type=int,
help="number of data sample in each batch",
)
parser.add_argument(
"--emb_dir",
default="data",
type=str,
help="Used to generate embeddings path if --pretrained_embeddings is not set",
)
parser.add_argument(
"--get_bert_encoder_embeddings",
default=False,
help="indicate if need to get node vectors from BERT encoder output, save code "
"commented out in src/pretraining.py",
)
# BERT Layer options
parser.add_argument(
"--n_layers", default=4, type=int, help="number of encoder layers in bert"
)
parser.add_argument(
"--d_model", default=200, type=int, help="embedding size in bert"
)
parser.add_argument("--d_k", default=64, type=int, help="dimension of K(=Q), V")
parser.add_argument("--d_v", default=64, type=int, help="dimension of K(=Q), V")
parser.add_argument("--n_heads", default=4, type=int, help="number of head in bert")
parser.add_argument(
"--d_ff",
default=200 * 4,
type=int,
help="4*d_model, FeedForward dimension in bert",
)
# GCN Layer options
parser.add_argument(
"--is_pre_trained",
action="store_true",
help="if there is pretrained node embeddings",
)
parser.add_argument(
"--gcn_option",
default="no_gcn",
help="preprocess bert input once or alternate gcn and bert, preprocess|alternate|no_gcn",
)
parser.add_argument(
"--num_gcn_layers", default=2, type=int, help="number of gcn layers before bert"
)
parser.add_argument(
"--node_edge_composition_func",
default="mult",
help="options for node and edge compostion, sub|circ_conv|mult|no_rel",
)
# Finetuning options
parser.add_argument("--ft_lr", default=0.001, type=float, help="learning rate")
parser.add_argument(
"--ft_batch_size",
default=128,
type=int,
help="number of data sample in each batch",
)
parser.add_argument(
"--ft_checkpoint", default=1000, type=int, help="checkpoint for validation"
)
parser.add_argument(
"--ft_d_ff", default=512, type=int, help="feedforward dimension in finetuning"
)
parser.add_argument(
"--ft_layer", default="ffn", help="options for finetune layer: linear|ffn"
)
parser.add_argument(
"--ft_drop_rate", default=0.1, type=float, help="dropout rate in finetuning"
)
parser.add_argument(
"--ft_input_option",
default="last4_cat",
help="which output layer from graphbert will be used for finetuning, last|last4_cat|last4_sum",
)
parser.add_argument(
"--false_edge_gen",
default="double",
help="false edge generation pattern/double/basic",
)
parser.add_argument(
"--ft_n_epochs", default=10, type=int, help="number of epochs for training"
)
parser.add_argument(
"--path_option",
default="shortest",
help="fine tuning context generation: shortest/all/pattern/random",
)
args = parser.parse_args()
# Default values
args.pretrained_embeddings, args.base_embedding_dim = get_set_embeddings_details(
args
)
args.d_model = args.base_embedding_dim
args.d_ff = args.base_embedding_dim * 4
print("Args ", str(args))
main(args)