-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen_emb.py
365 lines (341 loc) · 14.3 KB
/
gen_emb.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import argparse
import pickle
from pathlib import Path
import dgl
import torch as th
import numpy as np
from model.MECCH import MECCH, khopMECCH
from model.baselines.RGCN import RGCN
from model.baselines.HGT import HGT
from model.baselines.HAN import HAN, HAN_lp
from model.modules import LinkPrediction_minibatch, LinkPrediction_fullbatch
from utils import metapath2str, get_metapath_g, get_khop_g, load_base_config, load_model_config, load_data_nc, load_data_lp
def main_nc(args):
# load data
g, in_dim_dict, out_dim, train_nid_dict, val_nid_dict, test_nid_dict = load_data_nc(args.dataset)
print("Loaded data from dataset: {}".format(args.dataset))
# check cuda
use_cuda = args.gpu >= 0 and th.cuda.is_available()
if use_cuda:
args.device = th.device('cuda', args.gpu)
else:
args.device = th.device('cpu')
# create model + model-specific data preprocessing
if args.model == "MECCH":
if args.ablation:
g = get_khop_g(g, args)
model = khopMECCH(
g,
in_dim_dict,
args.hidden_dim,
out_dim,
args.n_layers,
dropout=args.dropout,
residual=args.residual,
layer_norm=args.layer_norm
)
else:
g, selected_metapaths = get_metapath_g(g, args)
n_heads_list = [args.n_heads] * args.n_layers
model = MECCH(
g,
selected_metapaths,
in_dim_dict,
args.hidden_dim,
out_dim,
args.n_layers,
n_heads_list,
dropout=args.dropout,
context_encoder=args.context_encoder,
use_v=args.use_v,
metapath_fusion=args.metapath_fusion,
residual=args.residual,
layer_norm=args.layer_norm
)
minibatch_flag = True
elif args.model == "RGCN":
assert args.n_layers >= 2
model = RGCN(
g,
in_dim_dict,
args.hidden_dim,
out_dim,
num_bases=-1,
num_hidden_layers=args.n_layers - 2,
dropout=args.dropout,
use_self_loop=args.use_self_loop
)
minibatch_flag = False
elif args.model == "HGT":
model = HGT(
g,
in_dim_dict,
args.hidden_dim,
out_dim,
args.n_layers,
args.n_heads
)
minibatch_flag = False
elif args.model == "HAN":
# assume the target node type has attributes
assert args.hidden_dim % args.n_heads == 0
target_ntype = list(g.ndata["y"].keys())[0]
n_heads_list = [args.n_heads] * args.n_layers
model = HAN(
args.metapaths,
target_ntype,
in_dim_dict[target_ntype],
args.hidden_dim // args.n_heads,
out_dim,
num_heads=n_heads_list,
dropout=args.dropout
)
minibatch_flag = False
else:
raise NotImplementedError
state_dict = th.load(str(Path(args.save) / 'checkpoint.pt'))
model.load_state_dict(state_dict)
model.to(args.device)
model.eval()
g = g.to(args.device)
if minibatch_flag:
nid_dict = {ntype: g.nodes(ntype).to(args.device) for ntype in g.ntypes}
# Use GPU-based neighborhood sampling if possible
num_workers = 4 if args.device.type == "CPU" else 0
if args.n_neighbor_samples <= 0:
sampler = dgl.dataloading.MultiLayerFullNeighborSampler(args.n_layers)
else:
sampler = dgl.dataloading.MultiLayerNeighborSampler([{
etype: args.n_neighbor_samples for etype in g.canonical_etypes}] * args.n_layers)
dataloader = dgl.dataloading.NodeDataLoader(
g,
nid_dict,
sampler,
batch_size=args.batch_size,
shuffle=False,
drop_last=False,
num_workers=num_workers,
device=args.device
)
with th.no_grad():
h_dict = {ntype: [] for ntype in nid_dict}
for input_nodes, output_nodes, blocks in dataloader:
input_features = blocks[0].srcdata["x"]
_, h_dict_temp = model.get_embs(blocks, input_features)
if not h_dict:
h_dict = {k: [v] for k, v in h_dict_temp.items()}
else:
for k, v in h_dict_temp.items():
h_dict[k].append(v)
h_dict = {k: th.cat(v, dim=0).cpu().numpy() for k, v in h_dict.items()}
else:
with th.no_grad():
_, h_dict = model.get_embs(g, g.ndata['x'])
h_dict = {k: v.cpu().numpy() for k, v in h_dict.items()}
# save embeddings
np.savez(Path(args.save) / 'embeddings.npz', **h_dict)
def main_lp(args):
# load data
(g_train, g_val, g_test), in_dim_dict, (train_eid_dict, val_eid_dict, test_eid_dict), (val_neg_uv, test_neg_uv) = load_data_lp(args.dataset)
# check cuda
use_cuda = args.gpu >= 0 and th.cuda.is_available()
if use_cuda:
args.device = th.device('cuda', args.gpu)
else:
args.device = th.device('cpu')
target_etype = list(train_eid_dict.keys())[0]
# create model + model-specific preprocessing
if args.model == 'MECCH':
if args.ablation:
# Note: here we assume there is only one edge type between users and items
train_eid_dict = {(g_train.to_canonical_etype(k)[0], '1-hop', g_train.to_canonical_etype(k)[2]): v for
k, v in train_eid_dict.items()}
val_eid_dict = {(g_val.to_canonical_etype(k)[0], '1-hop', g_val.to_canonical_etype(k)[2]): v for k, v
in val_eid_dict.items()}
test_eid_dict = {(g_test.to_canonical_etype(k)[0], '1-hop', g_test.to_canonical_etype(k)[2]): v for k, v
in test_eid_dict.items()}
target_etype = list(train_eid_dict.keys())[0]
g_train = get_khop_g(g_train, args)
g_val = get_khop_g(g_val, args)
g_test = get_khop_g(g_test, args)
model = khopMECCH(
g_train,
in_dim_dict,
args.hidden_dim,
args.hidden_dim,
args.n_layers,
dropout=args.dropout,
residual=args.residual,
layer_norm=args.layer_norm
)
else:
train_eid_dict = {metapath2str([g_train.to_canonical_etype(k)]): v for k, v in train_eid_dict.items()}
val_eid_dict = {metapath2str([g_val.to_canonical_etype(k)]): v for k, v in val_eid_dict.items()}
test_eid_dict = {metapath2str([g_test.to_canonical_etype(k)]): v for k, v in test_eid_dict.items()}
target_etype = list(train_eid_dict.keys())[0]
# cache metapath_g
load_path = Path('./data') / args.dataset / 'metapath_g-max_mp={}'.format(args.max_mp_length)
if load_path.is_dir():
g_list, _ = dgl.load_graphs(str(load_path / 'graph.bin'))
g_train, g_val, g_test = g_list
with open(load_path / 'selected_metapaths.pkl', 'rb') as in_file:
selected_metapaths = pickle.load(in_file)
else:
g_train, _ = get_metapath_g(g_train, args)
g_val, _ = get_metapath_g(g_val, args)
g_test, selected_metapaths = get_metapath_g(g_test, args)
load_path.mkdir()
dgl.save_graphs(str(load_path / 'graph.bin'), [g_train, g_val, g_test])
with open(load_path / 'selected_metapaths.pkl', 'wb') as out_file:
pickle.dump(selected_metapaths, out_file)
n_heads_list = [args.n_heads] * args.n_layers
model = MECCH(
g_train,
selected_metapaths,
in_dim_dict,
args.hidden_dim,
args.hidden_dim,
args.n_layers,
n_heads_list,
dropout=args.dropout,
context_encoder=args.context_encoder,
use_v=args.use_v,
metapath_fusion=args.metapath_fusion,
residual=args.residual,
layer_norm=args.layer_norm
)
model_lp = LinkPrediction_minibatch(model, args.hidden_dim, target_etype)
minibatch_flag = True
elif args.model == 'RGCN':
assert args.n_layers >= 2
model = RGCN(
g_train,
in_dim_dict,
args.hidden_dim,
args.hidden_dim,
num_bases=-1,
num_hidden_layers=args.n_layers - 2,
dropout=args.dropout,
use_self_loop=args.use_self_loop
)
if hasattr(args, 'batch_size'):
model_lp = LinkPrediction_minibatch(model, args.hidden_dim, target_etype)
minibatch_flag = True
else:
srctype, _, dsttype = g_train.to_canonical_etype(target_etype)
model_lp = LinkPrediction_fullbatch(model, args.hidden_dim, srctype, dsttype)
minibatch_flag = False
elif args.model == 'HGT':
model = HGT(
g_train,
in_dim_dict,
args.hidden_dim,
args.hidden_dim,
args.n_layers,
args.n_heads
)
if hasattr(args, 'batch_size'):
model_lp = LinkPrediction_minibatch(model, args.hidden_dim, target_etype)
minibatch_flag = True
else:
srctype, _, dsttype = g_train.to_canonical_etype(target_etype)
model_lp = LinkPrediction_fullbatch(model, args.hidden_dim, srctype, dsttype)
minibatch_flag = False
elif args.model == 'HAN':
# assume the target node type has attributes
# Note: this HAN version from DGL conducts full-batch training with online metapath_reachable_graph,
# preprocessing needed for the PubMed dataset
assert args.hidden_dim % args.n_heads == 0
n_heads_list = [args.n_heads] * args.n_layers
model_lp = HAN_lp(
g_train,
args.metapaths_u,
args.metapaths_u[0][0][0],
-1,
args.metapaths_v,
args.metapaths_v[0][0][0],
-1,
args.hidden_dim // args.n_heads,
args.hidden_dim,
num_heads=n_heads_list,
dropout=args.dropout
)
minibatch_flag = False
else:
raise NotImplementedError
state_dict = th.load(str(Path(args.save) / 'checkpoint.pt'))
model_lp.load_state_dict(state_dict)
model_lp.to(args.device)
model_lp.eval()
g_test = g_test.to(args.device)
if args.model == 'HAN':
# should we use g_val or g_test as the input graph?
with th.no_grad():
# set initial node embeddings
if hasattr(model_lp, 'feats_u'):
x_dict_u = {model_lp.target_ntype_u: model_lp.feats_u}
else:
x_dict_u = {model_lp.target_ntype_u: g_test.ndata['x'][model_lp.target_ntype_u]}
if hasattr(model_lp, 'feats_v'):
x_dict_v = {model_lp.target_ntype_v: model_lp.feats_v}
else:
x_dict_v = {model_lp.target_ntype_v: g_test.ndata['x'][model_lp.target_ntype_v]}
h_u = model_lp.model_u(g_test, x_dict_u)[model_lp.target_ntype_u]
h_v = model_lp.model_v(g_test, x_dict_v)[model_lp.target_ntype_v]
h_dict = {model_lp.target_ntype_u: h_u.cpu().numpy(), model_lp.target_ntype_v: h_v.cpu().numpy()}
else:
if minibatch_flag:
nid_dict = {ntype: g_test.nodes(ntype).to(args.device) for ntype in g_test.ntypes}
# Use GPU-based neighborhood sampling if possible
num_workers = 4 if args.device.type == "CPU" else 0
if args.n_neighbor_samples <= 0:
sampler = dgl.dataloading.MultiLayerFullNeighborSampler(args.n_layers)
else:
sampler = dgl.dataloading.MultiLayerNeighborSampler([{
etype: args.n_neighbor_samples for etype in g_test.canonical_etypes}] * args.n_layers)
dataloader = dgl.dataloading.NodeDataLoader(
g_test,
nid_dict,
sampler,
batch_size=args.batch_size,
shuffle=False,
drop_last=False,
num_workers=num_workers,
device=args.device
)
with th.no_grad():
h_dict = {ntype: [] for ntype in nid_dict}
for input_nodes, output_nodes, blocks in dataloader:
input_features = blocks[0].srcdata["x"]
h_dict_temp = model_lp.emb_model(blocks, input_features)
if not h_dict:
h_dict = {k: [v] for k, v in h_dict_temp.items()}
else:
for k, v in h_dict_temp.items():
h_dict[k].append(v)
h_dict = {k: th.cat(v, dim=0).cpu().numpy() for k, v in h_dict.items()}
else:
with th.no_grad():
h_dict = model_lp.emb_model(g_test, g_test.ndata['x'])
h_dict = {k: v.cpu().numpy() for k, v in h_dict.items()}
# save embeddings
np.savez(Path(args.save) / 'embeddings.npz', **h_dict)
if __name__ == "__main__":
parser = argparse.ArgumentParser("My HGNNs")
parser.add_argument('--model', '-m', type=str, required=True, help='name of model')
parser.add_argument('--dataset', '-d', type=str, required=True, help='name of dataset')
parser.add_argument('--task', '-t', type=str, default='node_classification', help='type of task')
parser.add_argument("--gpu", '-g', type=int, default=-1, help="which gpu to use, specify -1 to use CPU")
parser.add_argument('--save', '-s', type=str, required=True, help='which save dir to use')
args = parser.parse_args()
configs = load_base_config()
configs.update(load_model_config(Path(args.save) / '{}.json'.format(args.model), args.dataset))
configs.update(vars(args))
args = argparse.Namespace(**configs)
print(args)
if args.task == 'node_classification':
main_nc(args)
elif args.task == 'link_prediction':
main_lp(args)
else:
raise NotImplementedError