-
Notifications
You must be signed in to change notification settings - Fork 25
/
model_text_gnn.py
385 lines (326 loc) · 14 KB
/
model_text_gnn.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from config import FLAGS
import torch.nn.functional as F
import torch.nn as nn
import torch
from torch.nn import Parameter
from torch_scatter import scatter_add
from torch_geometric.nn.conv import MessagePassing
from torch_geometric.utils import remove_self_loops, add_self_loops, softmax
from torch_geometric.nn.inits import glorot, zeros
class TextGNN(nn.Module):
def __init__(self, pred_type, node_embd_type, num_layers, layer_dim_list, act, bn, num_labels, class_weights, dropout):
super(TextGNN, self).__init__()
self.node_embd_type = node_embd_type
self.layer_dim_list = layer_dim_list
self.num_layers = num_layers
self.dropout = dropout
if pred_type == 'softmax':
assert layer_dim_list[-1] == num_labels
elif pred_type == 'mlp':
dims = self._calc_mlp_dims(layer_dim_list[-1], num_labels)
self.mlp = MLP(layer_dim_list[-1], num_labels, num_hidden_lyr=len(dims), hidden_channels=dims, bn=False)
self.pred_type = pred_type
assert len(layer_dim_list) == (num_layers + 1)
self.act = act
self.bn = bn
self.layers = self._create_node_embd_layers()
self.loss = nn.CrossEntropyLoss(weight=class_weights)
def forward(self, pyg_graph, dataset):
acts = [pyg_graph.x]
for i, layer in enumerate(self.layers):
ins = acts[-1]
outs = layer(ins, pyg_graph)
acts.append(outs)
return self._loss(acts[-1], dataset)
def _loss(self, ins, dataset):
pred_inds = dataset.node_ids
if self.pred_type == 'softmax':
y_preds = ins[pred_inds]
elif self.pred_type == 'mlp':
y_preds = self.mlp(ins[pred_inds])
else:
raise NotImplementedError
y_true = torch.tensor(dataset.label_inds[pred_inds], dtype=torch.long, device=FLAGS.device)
loss = self.loss(y_preds, y_true)
return loss, y_preds.cpu().detach().numpy()
def _create_node_embd_layers(self):
layers = nn.ModuleList()
for i in range(self.num_layers):
act = self.act if i < self.num_layers - 1 else 'identity'
layers.append(NodeEmbedding(
type=self.node_embd_type,
in_dim=self.layer_dim_list[i],
out_dim=self.layer_dim_list[i + 1],
act=act,
bn=self.bn,
dropout=self.dropout if i != 0 else False
))
return layers
def _calc_mlp_dims(self, mlp_dim, output_dim=1):
dim = mlp_dim
dims = []
while dim > output_dim:
dim = dim // 2
dims.append(dim)
dims = dims[:-1]
return dims
class NodeEmbedding(nn.Module):
def __init__(self, type, in_dim, out_dim, act, bn, dropout):
super(NodeEmbedding, self).__init__()
self.in_dim = in_dim
self.out_dim = out_dim
self.type = type
if type == 'gcn':
self.conv = GCNConv(in_dim, out_dim)
self.act = create_act(act, out_dim)
elif type == 'gat':
self.conv = GATConv(in_dim, out_dim)
self.act = create_act(act, out_dim)
else:
raise ValueError(
'Unknown node embedding layer type {}'.format(type))
self.bn = bn
if self.bn:
self.bn = torch.nn.BatchNorm1d(out_dim)
self.dropout = dropout
if dropout:
self.dropout = torch.nn.Dropout()
def forward(self, ins, pyg_graph):
if self.dropout:
ins = self.dropout(ins)
if self.type == 'gcn':
if FLAGS.use_edge_weights:
x = self.conv(ins, pyg_graph.edge_index, edge_weight=pyg_graph.edge_attr)
else:
x = self.conv(ins, pyg_graph.edge_index)
else:
x = self.conv(ins, pyg_graph.edge_index)
x = self.act(x)
return x
class MLP(nn.Module):
'''mlp can specify number of hidden layers and hidden layer channels'''
def __init__(self, input_dim, output_dim, activation_type='relu', num_hidden_lyr=2,
hidden_channels=None, bn=False):
super().__init__()
self.out_dim = output_dim
if not hidden_channels:
hidden_channels = [input_dim for _ in range(num_hidden_lyr)]
elif len(hidden_channels) != num_hidden_lyr:
raise ValueError(
"number of hidden layers should be the same as the lengh of hidden_channels")
self.layer_channels = [input_dim] + hidden_channels + [output_dim]
self.activation = create_act(activation_type)
self.layers = nn.ModuleList(list(
map(self.weight_init, [nn.Linear(self.layer_channels[i], self.layer_channels[i + 1])
for i in range(len(self.layer_channels) - 1)])))
self.bn = bn
if self.bn:
self.bn = torch.nn.BatchNorm1d(output_dim)
def weight_init(self, m):
torch.nn.init.xavier_uniform_(m.weight, gain=nn.init.calculate_gain('relu'))
return m
def forward(self, x):
layer_inputs = [x]
for layer in self.layers:
input = layer_inputs[-1]
if layer == self.layers[-1]:
layer_inputs.append(layer(input))
else:
layer_inputs.append(self.activation(layer(input)))
# model.store_layer_output(self, layer_inputs[-1])
if self.bn:
layer_inputs[-1] = self.bn(layer_inputs[-1])
return layer_inputs[-1]
def create_act(act, num_parameters=None):
if act == 'relu':
return nn.ReLU()
elif act == 'prelu':
return nn.PReLU(num_parameters)
elif act == 'sigmoid':
return nn.Sigmoid()
elif act == 'tanh':
return nn.Tanh()
elif act == 'identity':
class Identity(nn.Module):
def forward(self, x):
return x
return Identity()
else:
raise ValueError('Unknown activation function {}'.format(act))
class GCNConv(MessagePassing):
r"""The graph convolutional operator from the `"Semi-supervised
Classfication with Graph Convolutional Networks"
<https://arxiv.org/abs/1609.02907>`_ paper
.. math::
\mathbf{X}^{\prime} = \mathbf{\hat{D}}^{-1/2} \mathbf{\hat{A}}
\mathbf{\hat{D}}^{-1/2} \mathbf{X} \mathbf{\Theta},
where :math:`\mathbf{\hat{A}} = \mathbf{A} + \mathbf{I}` denotes the
adjacency matrix with inserted self-loops and
:math:`\hat{D}_{ii} = \sum_{j=0} \hat{A}_{ij}` its diagonal degree matrix.
Args:
in_channels (int): Size of each input sample.
out_channels (int): Size of each output sample.
improved (bool, optional): If set to :obj:`True`, the layer computes
:math:`\mathbf{\hat{A}}` as :math:`\mathbf{A} + 2\mathbf{I}`.
(default: :obj:`False`)
cached (bool, optional): If set to :obj:`True`, the layer will cache
the computation of :math:`{\left(\mathbf{\hat{D}}^{-1/2}
\mathbf{\hat{A}} \mathbf{\hat{D}}^{-1/2} \right)}`.
(default: :obj:`False`)
bias (bool, optional): If set to :obj:`False`, the layer will not learn
an additive bias. (default: :obj:`True`)
"""
def __init__(self,
in_channels,
out_channels,
improved=False,
cached=False,
bias=True):
super(GCNConv, self).__init__('add')
self.in_channels = in_channels
self.out_channels = out_channels
self.improved = improved
self.cached = cached
self.cached_result = None
self.weight = Parameter(torch.Tensor(in_channels, out_channels))
if bias:
self.bias = Parameter(torch.Tensor(out_channels))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
glorot(self.weight)
zeros(self.bias)
self.cached_result = None
@staticmethod
def norm(edge_index, num_nodes, edge_weight, improved=False, dtype=None):
if edge_weight is None:
edge_weight = torch.ones((edge_index.size(1), ),
dtype=dtype,
device=edge_index.device)
edge_weight = edge_weight.view(-1)
assert edge_weight.size(0) == edge_index.size(1)
edge_index, edge_weight = remove_self_loops(edge_index, edge_weight)
edge_index = add_self_loops(edge_index, num_nodes)
loop_weight = torch.full((num_nodes, ),
1 if not improved else 2,
dtype=edge_weight.dtype,
device=edge_weight.device)
edge_weight = torch.cat([edge_weight, loop_weight], dim=0)
row, col = edge_index
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv_sqrt = deg.pow(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
return edge_index, deg_inv_sqrt[row] * edge_weight * deg_inv_sqrt[col]
def forward(self, x, edge_index, edge_weight=None):
""""""
if x.is_sparse:
x = torch.sparse.mm(x, self.weight)
else:
x = torch.matmul(x, self.weight)
if not self.cached or self.cached_result is None:
edge_index, norm = GCNConv.norm(edge_index, x.size(0), edge_weight,
self.improved, x.dtype)
self.cached_result = edge_index, norm
edge_index, norm = self.cached_result
return self.propagate(edge_index, x=x, norm=norm)
def message(self, x_j, norm):
return norm.view(-1, 1) * x_j
def update(self, aggr_out):
if self.bias is not None:
aggr_out = aggr_out + self.bias
return aggr_out
def __repr__(self):
return '{}({}, {})'.format(self.__class__.__name__, self.in_channels,
self.out_channels)
class GATConv(MessagePassing):
r"""The graph attentional operator from the `"Graph Attention Networks"
<https://arxiv.org/abs/1710.10903>`_ paper
.. math::
\mathbf{x}^{\prime}_i = \alpha_{i,i}\mathbf{\Theta}\mathbf{x}_{j} +
\sum_{j \in \mathcal{N}(i)} \alpha_{i,j}\mathbf{\Theta}\mathbf{x}_{j},
where the attention coefficients :math:`\alpha_{i,j}` are computed as
.. math::
\alpha_{i,j} =
\frac{
\exp\left(\mathrm{LeakyReLU}\left(\mathbf{a}^{\top}
[\mathbf{\Theta}\mathbf{x}_i \, \Vert \, \mathbf{\Theta}\mathbf{x}_j]
\right)\right)}
{\sum_{k \in \mathcal{N}(i) \cup \{ i \}}
\exp\left(\mathrm{LeakyReLU}\left(\mathbf{a}^{\top}
[\mathbf{\Theta}\mathbf{x}_i \, \Vert \, \mathbf{\Theta}\mathbf{x}_k]
\right)\right)}.
Args:
in_channels (int): Size of each input sample.
out_channels (int): Size of each output sample.
heads (int, optional): Number of multi-head-attentions. (default:
:obj:`1`)
concat (bool, optional): If set to :obj:`False`, the multi-head
attentions are averaged instead of concatenated. (default: :obj:`True`)
negative_slope (float, optional): LeakyReLU angle of the negative
slope. (default: :obj:`0.2`)
dropout (float, optional): Dropout probability of the normalized
attention coefficients which exposes each node to a stochastically
sampled neighborhood during training. (default: :obj:`0`)
bias (bool, optional): If set to :obj:`False`, the layer will not learn
an additive bias. (default: :obj:`True`)
"""
def __init__(self,
in_channels,
out_channels,
heads=1,
concat=True,
negative_slope=0.2,
dropout=0,
bias=True):
super(GATConv, self).__init__('add')
self.in_channels = in_channels
self.out_channels = out_channels
self.heads = heads
self.concat = concat
self.negative_slope = negative_slope
self.dropout = dropout
self.weight = Parameter(
torch.Tensor(in_channels, heads * out_channels))
self.att = Parameter(torch.Tensor(1, heads, 2 * out_channels))
if bias and concat:
self.bias = Parameter(torch.Tensor(heads * out_channels))
elif bias and not concat:
self.bias = Parameter(torch.Tensor(out_channels))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
glorot(self.weight)
glorot(self.att)
zeros(self.bias)
def forward(self, x, edge_index):
""""""
edge_index, _ = remove_self_loops(edge_index)
edge_index = add_self_loops(edge_index, num_nodes=x.size(0))
if x.is_sparse:
x = torch.sparse.mm(x, self.weight).view(-1, self.heads, self.out_channels)
else:
x = torch.matmul(x, self.weight).view(-1, self.heads, self.out_channels)
return self.propagate(edge_index, x=x, num_nodes=x.size(0))
def message(self, x_i, x_j, edge_index, num_nodes):
# Compute attention coefficients.
alpha = (torch.cat([x_i, x_j], dim=-1) * self.att).sum(dim=-1)
alpha = F.leaky_relu(alpha, self.negative_slope)
alpha = softmax(alpha, edge_index[0], num_nodes)
# Sample attention coefficients stochastically.
if self.training and self.dropout > 0:
alpha = F.dropout(alpha, p=self.dropout, training=True)
return x_j * alpha.view(-1, self.heads, 1)
def update(self, aggr_out):
if self.concat is True:
aggr_out = aggr_out.view(-1, self.heads * self.out_channels)
else:
aggr_out = aggr_out.mean(dim=1)
if self.bias is not None:
aggr_out = aggr_out + self.bias
return aggr_out
def __repr__(self):
return '{}({}, {}, heads={})'.format(self.__class__.__name__,
self.in_channels,
self.out_channels, self.heads)