-
Notifications
You must be signed in to change notification settings - Fork 2
/
line_graph_models.py
264 lines (225 loc) · 10.6 KB
/
line_graph_models.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
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 LineGraphGCN(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,
prep_len,
n_head,
node_layer_specs,
edge_layer_specs,
aggregator_class,
mpaggr_class,
edgeupt_class,
prep_class,
sampler_class,
dropout,
batchnorm,
attn_dropout=0,
bias=False,
):
super(LineGraphGCN, self).__init__()
# --
# 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])
# self.edge2node_idx_mp
for i, key in enumerate(problem.edge2node_idxs):
self.register_buffer('edge2node_idx_{}'.format(i),
problem.edge2node_idxs[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 = 512
# 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.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([aggregator_class(
input_dim=self.edge_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=self.edge_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),
])
# 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
output = []
all_ids = torch.arange(self.n_nodes).to(self.feats.device)
for mp in range(self.n_mp):
# import GPUtil
# GPUtil.showUtilization()
all_feats = self.feats[all_ids].detach() if has_feats else None
all_feats = self.prep(all_ids, all_feats, layer_idx=1)
orgin_feats = self.prep(all_ids, all_feats, layer_idx=0)
all_edges = 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))
skip_buffer = []
for layer_idx in range(self.depth):
# ---Update edges---
tmp_edges = []
edge_ids = torch.arange(all_edges.shape[0]).to(self.feats.device).detach()
for chunk_id, chunk in enumerate(torch.split(edge_ids, self.train_batch, dim=0)):
chunk_feat = all_edges[chunk]
neigh_feat = all_edges[edge_neigh[chunk]].view(-1,all_edges.shape[1])
chunk_node = all_feats[edge2node_idx[chunk]].view(-1,all_feats.shape[1])
chunk_result = torch.cat([getattr(self, 'edge_agg_{}_{}'.format(mp, layer_idx))[h] \
(chunk_feat, neigh_feat, chunk_node, 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
# ---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 orgin_feats[chunk]
neigh_feat = all_feats[node_neigh[chunk]].view(-1,all_feats.shape[1])
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, neigh_feat,
chunk_edge, 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
all_feats = torch.cat(tmp_feats, dim=0)
all_edges = torch.cat(tmp_edges, dim=0)
skip_buffer.append(all_feats)
# del all_feats
# del all_edges
# 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)
# 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)
return output, weights