-
Notifications
You must be signed in to change notification settings - Fork 14
/
model.py
150 lines (126 loc) · 5.7 KB
/
model.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
# Copyright (c) 2020-present, Royal Bank of Canada.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import math
from graph_generator import *
from layers import GCNConv_dense, GCNConv_dgl
class GCN_DAE(nn.Module):
def __init__(self, nlayers, in_dim, hidden_dim, nclasses, dropout, dropout_adj, features, k, knn_metric, i_,
non_linearity, normalization, mlp_h, mlp_epochs, gen_mode, sparse, mlp_act):
super(GCN_DAE, self).__init__()
self.layers = nn.ModuleList()
if sparse:
self.layers.append(GCNConv_dgl(in_dim, hidden_dim))
for _ in range(nlayers - 2):
self.layers.append(GCNConv_dgl(hidden_dim, hidden_dim))
self.layers.append(GCNConv_dgl(hidden_dim, nclasses))
else:
self.layers.append(GCNConv_dense(in_dim, hidden_dim))
for i in range(nlayers - 2):
self.layers.append(GCNConv_dense(hidden_dim, hidden_dim))
self.layers.append(GCNConv_dense(hidden_dim, nclasses))
self.dropout = dropout
self.dropout_adj = nn.Dropout(p=dropout_adj)
self.dropout_adj_p = dropout_adj
self.k = k
self.knn_metric = knn_metric
self.i = i_
self.non_linearity = non_linearity
self.normalization = normalization
self.nnodes = features.shape[0]
self.mlp_h = mlp_h
self.mlp_epochs = mlp_epochs
self.sparse = sparse
if gen_mode == 0:
self.graph_gen = FullParam(features, non_linearity, k, knn_metric, self.i, sparse).cuda()
elif gen_mode == 1:
self.graph_gen = MLP(2, features.shape[1], math.floor(math.sqrt(features.shape[1] * self.mlp_h)),
self.mlp_h, mlp_epochs, k, knn_metric, self.non_linearity, self.i, self.sparse,
mlp_act).cuda()
elif gen_mode == 2:
self.graph_gen = MLP_Diag(2, features.shape[1], k, knn_metric, self.non_linearity, self.i, sparse,
mlp_act).cuda()
def get_adj(self, h):
Adj_ = self.graph_gen(h)
if not self.sparse:
Adj_ = symmetrize(Adj_)
Adj_ = normalize(Adj_, self.normalization, self.sparse)
return Adj_
def forward(self, features, x): # x corresponds to masked_fearures
Adj_ = self.get_adj(features)
if self.sparse:
Adj = Adj_
Adj.edata['w'] = F.dropout(Adj.edata['w'], p=self.dropout_adj_p, training=self.training)
else:
Adj = self.dropout_adj(Adj_)
for i, conv in enumerate(self.layers[:-1]):
x = conv(x, Adj)
x = F.relu(x)
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.layers[-1](x, Adj)
return x, Adj_
class GCN(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels, num_layers, dropout, dropout_adj, Adj, sparse):
super(GCN, self).__init__()
self.layers = nn.ModuleList()
if sparse:
self.layers.append(GCNConv(in_channels, hidden_channels))
for _ in range(num_layers - 2):
self.layers.append(GCNConv(hidden_channels, hidden_channels))
self.layers.append(GCNConv(hidden_channels, out_channels))
else:
self.layers.append(GCNConv_dense(in_channels, hidden_channels))
for i in range(num_layers - 2):
self.layers.append(GCNConv_dense(hidden_channels, hidden_channels))
self.layers.append(GCNConv_dense(hidden_channels, out_channels))
self.dropout = dropout
self.dropout_adj = nn.Dropout(p=dropout_adj)
self.dropout_adj_p = dropout_adj
self.Adj = Adj
self.Adj.requires_grad = False
self.sparse = sparse
def forward(self, x):
if self.sparse:
Adj = self.Adj
Adj.edata['w'] = F.dropout(Adj.edata['w'], p=self.dropout_adj_p, training=self.training)
else:
Adj = self.dropout_adj(self.Adj)
for i, conv in enumerate(self.layers[:-1]):
x = conv(x, Adj)
x = F.relu(x)
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.layers[-1](x, Adj)
return x
class GCN_C(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels, num_layers, dropout, dropout_adj, sparse):
super(GCN_C, self).__init__()
self.layers = nn.ModuleList()
if sparse:
self.layers.append(GCNConv_dgl(in_channels, hidden_channels))
for _ in range(num_layers - 2):
self.layers.append(GCNConv_dgl(hidden_channels, hidden_channels))
self.layers.append(GCNConv_dgl(hidden_channels, out_channels))
else:
self.layers.append(GCNConv_dense(in_channels, hidden_channels))
for i in range(num_layers - 2):
self.layers.append(GCNConv_dense(hidden_channels, hidden_channels))
self.layers.append(GCNConv_dense(hidden_channels, out_channels))
self.dropout = dropout
self.dropout_adj = nn.Dropout(p=dropout_adj)
self.dropout_adj_p = dropout_adj
self.sparse = sparse
def forward(self, x, adj_t):
if self.sparse:
Adj = adj_t
Adj.edata['w'] = F.dropout(Adj.edata['w'], p=self.dropout_adj_p, training=self.training)
else:
Adj = self.dropout_adj(adj_t)
for i, conv in enumerate(self.layers[:-1]):
x = conv(x, Adj)
x = F.relu(x)
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.layers[-1](x, Adj)
return x