/
reg.py
355 lines (309 loc) · 15.5 KB
/
reg.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
from __future__ import division
from __future__ import print_function
from functools import partial
import torch
from torch import nn
from torch.nn import functional as F
from lr import LRSchedule
from nn_modules import GraphConvolution, IdEdgeAggregator
class Conch_reg(nn.Module):
"""
No more samplers.
Node feature: [N,d]
Node neighbors: [N,S]
Edge embedding: [E,e]
Node edge mapping: [N,S]->E
Edge neighbors: [E,2S]
"""
def __init__(self,
n_mp,
problem,
cos_problem,
prep_len,
n_head,
node_layer_specs,
edge_layer_specs,
aggregator_class,
mpaggr_class,
edge_aggr_class,
prep_class,
sampler_class,
dropout,
batchnorm,
# coff=1,
attn_dropout=0,
bias=False,
):
super(Conch_reg, self).__init__()
# self.coff = coff
# --
# Input Data
self.edge_dim = problem.edge_dim
self.input_dim = problem.feats_dim
self.n_nodes = problem.n_nodes
self.n_classes = problem.n_classes
self.n_head = n_head
self.bias = bias
# self.feats
self.register_buffer('feats', problem.feats)
# self.edge_neigh_mp
# for i, key in enumerate(problem.edge_neighs):
# self.register_buffer('edge_neigh_{}'.format(i),
# problem.edge_neighs[key])
# # self.node_neigh_mp
# for i, key in enumerate(problem.node_neighs):
# self.register_buffer('node_neigh_{}'.format(i),
# problem.node_neighs[key])
# self.node2edge_idx_mp
for i, key in enumerate(problem.node2edge_idxs):
self.register_buffer('node2edge_idx_{}'.format(i),
problem.node2edge_idxs[key])
# self.edge_emb_mp
for i, key in enumerate(problem.edge_embs):
self.register_buffer('edge_emb_{}'.format(i),
problem.edge_embs[key])
# print(problem.edge_embs[key].shape)
# # self.edge2node_idx_mp
# for i, key in enumerate(problem.edge2node_idxs):
# self.register_buffer('edge2node_idx_{}'.format(i),
# problem.edge2node_idxs[key])
# self.edge_node_adj_mp
for i, key in enumerate(problem.edge_node_adjs):
self.register_buffer('edge_node_adj_{}'.format(i),
problem.edge_node_adjs[key])
#------cos-------
# self.cos_node2edge_idx_mp
for i, key in enumerate(cos_problem.node2edge_idxs):
self.register_buffer('cos_node2edge_idx_{}'.format(i),
cos_problem.node2edge_idxs[key])
# self.cos_edge_emb_mp
for i, key in enumerate(cos_problem.edge_embs):
self.register_buffer('cos_edge_emb_{}'.format(i),
cos_problem.edge_embs[key])
# print(cos_problem.edge_embs[key].shape)
# self.cos_edge_node_adj_mp
for i, key in enumerate(cos_problem.edge_node_adjs):
self.register_buffer('cos_edge_node_adj_{}'.format(i),
cos_problem.edge_node_adjs[key])
# Define network
self.n_mp = n_mp
self.depth = len(node_layer_specs)
self.dropout = dropout
self.attn_dropout = attn_dropout
self.batchnorm = batchnorm
# No more samplers
self.train_batch = 999999
# Prep
# import numpy as np
# with open("{}{}.emb".format('data/freebase/', 'MADW_16')) as f:
# n_nodes, n_feature = map(int, f.readline().strip().split())
# print("number of nodes:{}, embedding size:{}".format(n_nodes, n_feature))
#
# embedding = np.loadtxt("{}{}.emb".format('data/freebase/', 'MADW_16'),
# dtype=np.float32, skiprows=1)
# emd_index = {}
# for i in range(n_nodes):
# emd_index[embedding[i, 0]] = i
# print(emd_index[0])
# features = np.asarray([embedding[emd_index[i], 1:] for i in range(n_nodes)])
#
# assert features.shape[1] == n_feature
# assert features.shape[0] == n_nodes
# print(features[0,:])
# features = torch.FloatTensor(features[:problem.n_nodes,:])
self.prep = prep_class(input_dim=problem.feats_dim, n_nodes=problem.n_nodes,
embedding_dim=prep_len,
# pre_trained=features,
# output_dim=prep_len
)
self.edge_prep = nn.ModuleList([nn.Linear(problem.edge_dim, prep_len, bias=False) for _ in range(self.n_mp)])
self.input_dim = self.prep.output_dim
# Network
for mp in range(self.n_mp):
# agg_layers = []
# edge_layers = []
input_dim = self.input_dim
out_dim = 0
for i in range(len(node_layer_specs)):
node_spec = node_layer_specs[i]
edge_spec = edge_layer_specs[i]
if False:
edge = nn.ModuleList([IdEdgeAggregator(
input_dim=input_dim,
edge_dim=self.edge_dim,
activation=spec['activation'],
dropout=self.dropout,
batchnorm=self.batchnorm,
) for _ in range(n_head)])
else:
edge_agg = nn.ModuleList([edge_aggr_class(
input_dim=input_dim,
edge_dim=input_dim,
activation=edge_spec['activation'],
output_dim=edge_spec['output_dim'],
concat_node=edge_spec['concat_node'],
concat_edge=edge_spec['concat_edge'],
dropout=self.dropout,
attn_dropout=self.attn_dropout,
batchnorm=self.batchnorm,
) for _ in range(n_head)])
node_agg = nn.ModuleList([aggregator_class(
input_dim=input_dim,
edge_dim=edge_spec['output_dim'],
output_dim=node_spec['output_dim'],
activation=node_spec['activation'],
concat_node=node_spec['concat_node'],
concat_edge=node_spec['concat_edge'],
dropout=self.dropout,
attn_dropout=self.attn_dropout,
batchnorm=self.batchnorm,
) for _ in range(n_head)])
# agg_layers.append(agg)
# May not be the same as spec['output_dim']
input_dim = node_agg[0].output_dim * n_head
out_dim += input_dim
# edge_layers.append(edge)
self.add_module('node_agg_{}_{}'.format(mp, i), node_agg)
self.add_module('edge_agg_{}_{}'.format(mp, i), edge_agg)
input_dim = out_dim
self.mp_agg = mpaggr_class(
input_dim, n_head=self.n_mp + int(self.bias), dropout=self.dropout, batchnorm=self.batchnorm, )
self.fc = nn.Sequential(*[
# nn.Linear(self.mp_agg.output_dim, 32, bias=True),
# nn.ReLU(), nn.Dropout(self.dropout),
# nn.Linear(32, problem.n_classes, bias=True),
nn.Linear(self.mp_agg.output_dim, problem.n_classes, bias=True),
])
# We only want to forward IDs to facilitate nn.DataParallelism
def forward(self, train_ids, train=True):
# print("\tIn Model: input size ", ids.shape)
# ids.to(self.feats.device)
has_feats = self.feats is not None
reg_loss = 0
output = []
all_ids = torch.arange(self.n_nodes).to(self.edge_emb_0.device)
for mp in range(self.n_mp):
# import GPUtil
# GPUtil.showUtilization()
all_feats = self.feats[all_ids].detach() if has_feats else None
dummy_feats = self.prep(all_ids, all_feats, layer_idx=0)
all_feats = self.prep(all_ids, all_feats, layer_idx=1)
all_edges = self.edge_prep[mp](getattr(self, 'edge_emb_{}'.format(mp)))
# node_neigh = getattr(self, 'node_neigh_{}'.format(mp)) # row: neighbors of a node
node2edge_idx = getattr(self, 'node2edge_idx_{}'.format(
mp)) # entries: index of edge embedding, correspondding to node_neigh
# edge_neigh = getattr(self, 'edge_neigh_{}'.format(mp)) # row: neighbors of a edge
# edge2node_idx = getattr(self, 'edge2node_idx_{}'.format(mp))
edge_node_adj = getattr(self, 'edge_node_adj_{}'.format(mp))
skip_buffer = []
for layer_idx in range(self.depth):
# ---Update edges---
tmp_edges = []
edge_ids = torch.arange(all_edges.shape[0]).to(self.edge_emb_0.device).detach()
for chunk_id, chunk in enumerate(torch.split(edge_ids, self.train_batch, dim=0)):
chunk_feat = all_edges[chunk]
neigh_feat = None
chunk_node = all_feats[edge_node_adj[chunk]].view(-1,all_feats.shape[1])
chunk_result = torch.cat([getattr(self, 'edge_agg_{}_{}'.format(mp, layer_idx))[h] \
(chunk_feat, chunk_node, neigh_feat, mask=None) \
for h in range(self.n_head)], dim=1)
chunk_result = F.dropout(chunk_result, self.dropout, training=self.training)
# del neigh_feat
# del chunk_node
# del chunk_feat
tmp_edges.append(chunk_result)
# del chunk_result
next_edges = torch.cat(tmp_edges, dim=0)
# ---Update nodes---
# Split all_ids into batches, in case of OOM.
tmp_feats = []
for chunk_id, chunk in enumerate(torch.split(all_ids, self.train_batch, dim=0)):
chunk_feat = all_feats[chunk] if layer_idx != 0 else dummy_feats[chunk]
neigh_feat = None
chunk_edge = all_edges[node2edge_idx[chunk]].view(-1,all_edges.shape[1])
chunk_result = torch.cat([getattr(self, 'node_agg_{}_{}'.format(mp, layer_idx))[h] \
(chunk_feat, chunk_edge,
neigh_feat, mask=None) \
for h in range(self.n_head)], dim=1)
chunk_result = F.dropout(chunk_result, self.dropout, training=self.training)
# del neigh_feat
# del chunk_edge
# del chunk_feat
tmp_feats.append(chunk_result)
# del chunk_result
pass
next_feats = torch.cat(tmp_feats, dim=0)
skip_buffer.append(next_feats)
all_feats = next_feats
all_edges = next_edges
#-------cos-------
cos_all_feats = self.feats[all_ids].detach() if has_feats else None
cos_dummy_feats = self.prep(all_ids, cos_all_feats, layer_idx=0)
cos_all_feats = self.prep(all_ids, cos_all_feats, layer_idx=1)
cos_all_edges = self.edge_prep[mp](getattr(self, 'cos_edge_emb_{}'.format(mp)))
# node_neigh = getattr(self, 'node_neigh_{}'.format(mp)) # row: neighbors of a node
cos_node2edge_idx = getattr(self, 'cos_node2edge_idx_{}'.format(
mp)) # entries: index of edge embedding, correspondding to node_neigh
# edge_neigh = getattr(self, 'edge_neigh_{}'.format(mp)) # row: neighbors of a edge
# edge2node_idx = getattr(self, 'edge2node_idx_{}'.format(mp))
cos_edge_node_adj = getattr(self, 'cos_edge_node_adj_{}'.format(mp))
cos_skip_buffer = []
for layer_idx in range(self.depth):
# ---Update edges---
cos_tmp_edges = []
cos_edge_ids = torch.arange(cos_all_edges.shape[0]).to(self.cos_edge_emb_0.device).detach()
for chunk_id, chunk in enumerate(torch.split(cos_edge_ids, self.train_batch, dim=0)):
chunk_feat = cos_all_edges[chunk]
neigh_feat = None
chunk_node = cos_all_feats[cos_edge_node_adj[chunk]].view(-1,cos_all_feats.shape[1])
chunk_result = torch.cat([getattr(self, 'edge_agg_{}_{}'.format(mp, layer_idx))[h] \
(chunk_feat, chunk_node, neigh_feat, mask=None) \
for h in range(self.n_head)], dim=1)
chunk_result = F.dropout(chunk_result, self.dropout, training=self.training)
# del neigh_feat
# del chunk_node
# del chunk_feat
cos_tmp_edges.append(chunk_result)
# del chunk_result
cos_next_edges = torch.cat(cos_tmp_edges, dim=0)
# ---Update nodes---
# Split all_ids into batches, in case of OOM.
cos_tmp_feats = []
for chunk_id, chunk in enumerate(torch.split(all_ids, self.train_batch, dim=0)):
chunk_feat = cos_all_feats[chunk] if layer_idx != 0 else cos_dummy_feats[chunk]
neigh_feat = None
chunk_edge = cos_all_edges[cos_node2edge_idx[chunk]].view(-1,cos_all_edges.shape[1])
chunk_result = torch.cat([getattr(self, 'node_agg_{}_{}'.format(mp, layer_idx))[h] \
(chunk_feat, chunk_edge,
neigh_feat, mask=None) \
for h in range(self.n_head)], dim=1)
chunk_result = F.dropout(chunk_result, self.dropout, training=self.training)
# del neigh_feat
# del chunk_edge
# del chunk_feat
cos_tmp_feats.append(chunk_result)
# del chunk_result
pass
cos_next_feats = torch.cat(cos_tmp_feats, dim=0)
cos_skip_buffer.append(cos_next_feats)
cos_all_feats = cos_next_feats
cos_all_edges = cos_next_edges
# del all_feats
# del all_edges
reg_loss += ((torch.cat(skip_buffer, dim=-1)-torch.cat(cos_skip_buffer, dim=-1))**2).sum()
# Jumping connections
output.append(torch.cat(skip_buffer, dim=-1)[train_ids].unsqueeze(
0)) # concat skip connections; unsqueeze for metapath aggr.
output = torch.cat(output, dim=0)
# output = F.normalize(output, dim=2) #normalize before attention
# import GPUtil
# GPUtil.showUtilization()
output, weights = self.mp_agg(output)
# output = torch.sum(output,dim=0).squeeze()
# weights = None
# print(weights)
# output = F.normalize(output, dim=-1) # ?? Do we actually want this? ... Sometimes ...
output = F.dropout(output, self.dropout, training=self.training)
output = self.fc(output)
# reg_loss *= self.coff
return output, weights, reg_loss