-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
245 lines (210 loc) · 10 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from dgl.nn.pytorch import SortPooling, SumPooling
from dgl.nn.pytorch import GraphConv, SAGEConv
class GCN(nn.Module):
"""
GCN Model
Attributes:
num_layers(int): num of gcn layers
hidden_units(int): num of hidden units
gcn_type(str): type of gcn layer, 'gcn' for GraphConv and 'sage' for SAGEConv
pooling_type(str): type of graph pooling to get subgraph representation
'sum' for sum pooling and 'center' for center pooling.
node_attributes(Tensor, optional): node attribute
edge_weights(Tensor, optional): edge weight
node_embedding(Tensor, optional): pre-trained node embedding
use_embedding(bool, optional): whether to use node embedding. Note that if 'use_embedding' is set True
and 'node_embedding' is None, will automatically randomly initialize node embedding.
num_nodes(int, optional): num of nodes
dropout(float, optional): dropout rate
max_z(int, optional): default max vocab size of node labeling, default 1000.
"""
def __init__(self, num_layers, hidden_units, gcn_type='gcn', pooling_type='sum', node_attributes=None,
edge_weights=None, node_embedding=None, use_embedding=False,
num_nodes=None, dropout=0.5, max_z=1000):
super(GCN, self).__init__()
self.num_layers = num_layers
self.dropout = dropout
self.pooling_type = pooling_type
self.use_attribute = False if node_attributes is None else True
self.use_embedding = use_embedding
self.use_edge_weight = False if edge_weights is None else True
self.z_embedding = nn.Embedding(max_z, hidden_units)
if node_attributes is not None:
self.node_attributes_lookup = nn.Embedding.from_pretrained(node_attributes)
self.node_attributes_lookup.weight.requires_grad = False
if edge_weights is not None:
self.edge_weights_lookup = nn.Embedding.from_pretrained(edge_weights)
self.edge_weights_lookup.weight.requires_grad = False
if node_embedding is not None:
self.node_embedding = nn.Embedding.from_pretrained(node_embedding)
self.node_embedding.weight.requires_grad = False
elif use_embedding:
self.node_embedding = nn.Embedding(num_nodes, hidden_units)
initial_dim = hidden_units
if self.use_attribute:
initial_dim += self.node_attributes_lookup.embedding_dim
if self.use_embedding:
initial_dim += self.node_embedding.embedding_dim
self.layers = nn.ModuleList()
if gcn_type == 'gcn':
self.layers.append(GraphConv(initial_dim, hidden_units, allow_zero_in_degree=True))
for _ in range(num_layers - 1):
self.layers.append(GraphConv(hidden_units, hidden_units, allow_zero_in_degree=True))
elif gcn_type == 'sage':
self.layers.append(SAGEConv(initial_dim, hidden_units, aggregator_type='gcn'))
for _ in range(num_layers - 1):
self.layers.append(SAGEConv(hidden_units, hidden_units, aggregator_type='gcn'))
else:
raise ValueError('Gcn type error.')
self.linear_1 = nn.Linear(hidden_units, hidden_units)
self.linear_2 = nn.Linear(hidden_units, 1)
if pooling_type != 'sum':
raise ValueError('Pooling type error.')
self.pooling = SumPooling()
def reset_parameters(self):
for layer in self.layers:
layer.reset_parameters()
def forward(self, g, z, node_id=None, edge_id=None):
"""
Args:
g(DGLGraph): the graph
z(Tensor): node labeling tensor, shape [N, 1]
node_id(Tensor, optional): node id tensor, shape [N, 1]
edge_id(Tensor, optional): edge id tensor, shape [E, 1]
Returns:
x(Tensor): output tensor
"""
z_emb = self.z_embedding(z)
if self.use_attribute:
x = self.node_attributes_lookup(node_id)
x = torch.cat([z_emb, x], 1)
else:
x = z_emb
if self.use_edge_weight:
edge_weight = self.edge_weights_lookup(edge_id)
else:
edge_weight = None
if self.use_embedding:
n_emb = self.node_embedding(node_id)
x = torch.cat([x, n_emb], 1)
for layer in self.layers[:-1]:
x = layer(g, x, edge_weight=edge_weight)
x = F.relu(x)
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.layers[-1](g, x, edge_weight=edge_weight)
x = self.pooling(g, x)
x = F.relu(self.linear_1(x))
F.dropout(x, p=self.dropout, training=self.training)
x = self.linear_2(x)
return x
class DGCNN(nn.Module):
"""
An end-to-end deep learning architecture for graph classification.
paper link: https://muhanzhang.github.io/papers/AAAI_2018_DGCNN.pdf
Attributes:
num_layers(int): num of gcn layers
hidden_units(int): num of hidden units
k(int, optional): The number of nodes to hold for each graph in SortPooling.
gcn_type(str): type of gcn layer, 'gcn' for GraphConv and 'sage' for SAGEConv
node_attributes(Tensor, optional): node attribute
edge_weights(Tensor, optional): edge weight
node_embedding(Tensor, optional): pre-trained node embedding
use_embedding(bool, optional): whether to use node embedding. Note that if 'use_embedding' is set True
and 'node_embedding' is None, will automatically randomly initialize node embedding.
num_nodes(int, optional): num of nodes
dropout(float, optional): dropout rate
max_z(int, optional): default max vocab size of node labeling, default 1000.
"""
def __init__(self, num_layers, hidden_units, k=10, gcn_type='gcn', node_attributes=None,
edge_weights=None, node_embedding=None, use_embedding=False, num_nodes=None, dropout=0.5, max_z=1000):
super(DGCNN, self).__init__()
self.num_layers = num_layers
self.dropout = dropout
self.use_attribute = False if node_attributes is None else True
self.use_embedding = use_embedding
self.use_edge_weight = False if edge_weights is None else True
self.z_embedding = nn.Embedding(max_z, hidden_units)
if node_attributes is not None:
self.node_attributes_lookup = nn.Embedding.from_pretrained(node_attributes)
self.node_attributes_lookup.weight.requires_grad = False
if edge_weights is not None:
self.edge_weights_lookup = nn.Embedding.from_pretrained(edge_weights)
self.edge_weights_lookup.weight.requires_grad = False
if node_embedding is not None:
self.node_embedding = nn.Embedding.from_pretrained(node_embedding)
self.node_embedding.weight.requires_grad = False
elif use_embedding:
self.node_embedding = nn.Embedding(num_nodes, hidden_units)
initial_dim = hidden_units
if self.use_attribute:
initial_dim += self.node_attributes_lookup.embedding_dim
if self.use_embedding:
initial_dim += self.node_embedding.embedding_dim
self.layers = nn.ModuleList()
if gcn_type == 'gcn':
self.layers.append(GraphConv(initial_dim, hidden_units, allow_zero_in_degree=True))
for _ in range(num_layers - 1):
self.layers.append(GraphConv(hidden_units, hidden_units, allow_zero_in_degree=True))
self.layers.append(GraphConv(hidden_units, 1, allow_zero_in_degree=True))
elif gcn_type == 'sage':
self.layers.append(SAGEConv(initial_dim, hidden_units, aggregator_type='gcn'))
for _ in range(num_layers - 1):
self.layers.append(SAGEConv(hidden_units, hidden_units, aggregator_type='gcn'))
self.layers.append(SAGEConv(hidden_units, 1, aggregator_type='gcn'))
else:
raise ValueError('Gcn type error.')
self.pooling = SortPooling(k=k)
conv1d_channels = [16, 32]
total_latent_dim = hidden_units * num_layers + 1
conv1d_kws = [total_latent_dim, 5]
self.conv_1 = nn.Conv1d(1, conv1d_channels[0], conv1d_kws[0],
conv1d_kws[0])
self.maxpool1d = nn.MaxPool1d(2, 2)
self.conv_2 = nn.Conv1d(conv1d_channels[0], conv1d_channels[1],
conv1d_kws[1], 1)
dense_dim = int((k - 2) / 2 + 1)
dense_dim = (dense_dim - conv1d_kws[1] + 1) * conv1d_channels[1]
self.linear_1 = nn.Linear(dense_dim, 128)
self.linear_2 = nn.Linear(128, 1)
def forward(self, g, z, node_id=None, edge_id=None):
"""
Args:
g(DGLGraph): the graph
z(Tensor): node labeling tensor, shape [N, 1]
node_id(Tensor, optional): node id tensor, shape [N, 1]
edge_id(Tensor, optional): edge id tensor, shape [E, 1]
Returns:
x(Tensor): output tensor
"""
z_emb = self.z_embedding(z)
if self.use_attribute:
x = self.node_attributes_lookup(node_id)
x = torch.cat([z_emb, x], 1)
else:
x = z_emb
if self.use_edge_weight:
edge_weight = self.edge_weights_lookup(edge_id)
else:
edge_weight = None
if self.use_embedding:
n_emb = self.node_embedding(node_id)
x = torch.cat([x, n_emb], 1)
xs = [x]
for layer in self.layers:
out = torch.tanh(layer(g, xs[-1], edge_weight=edge_weight))
xs += [out]
x = torch.cat(xs[1:], dim=-1)
# SortPooling
x = self.pooling(g, x)
x = x.unsqueeze(1)
x = F.relu(self.conv_1(x))
x = self.maxpool1d(x)
x = F.relu(self.conv_2(x))
x = x.view(x.size(0), -1)
x = F.relu(self.linear_1(x))
F.dropout(x, p=self.dropout, training=self.training)
x = self.linear_2(x)
return x