forked from muhanzhang/IGMC
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
287 lines (256 loc) · 11.2 KB
/
models.py
File metadata and controls
287 lines (256 loc) · 11.2 KB
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
import torch
import math
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Linear, Conv1d
from torch_geometric.nn import GCNConv, RGCNConv, SAGEConv, global_sort_pool, global_add_pool
from torch_geometric.utils import dropout_adj
from util_functions import *
import pdb
import time
from graphnorm import GraphNorm
class GNN(torch.nn.Module):
# a base GNN class, GCN message passing + sum_pooling
def __init__(self, dataset, gconv=GCNConv, latent_dim=[32, 32, 32, 1],
regression=False, adj_dropout=0.2, force_undirected=False):
super(GNN, self).__init__()
self.regression = regression
self.adj_dropout = adj_dropout
self.force_undirected = force_undirected
self.convs = torch.nn.ModuleList()
self.convs.append(gconv(dataset.num_features, latent_dim[0]))
for i in range(0, len(latent_dim)-1):
self.convs.append(gconv(latent_dim[i], latent_dim[i+1]))
self.lin1 = Linear(sum(latent_dim), 128)
if self.regression:
self.lin2 = Linear(128, 1)
else:
self.lin2 = Linear(128, dataset.num_classes)
def reset_parameters(self):
for conv in self.convs:
conv.reset_parameters()
self.lin1.reset_parameters()
self.lin2.reset_parameters()
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
if self.adj_dropout > 0:
edge_index, edge_type = dropout_adj(
edge_index, edge_type, p=self.adj_dropout,
force_undirected=self.force_undirected, num_nodes=len(x),
training=self.training
)
concat_states = []
for conv in self.convs:
x = torch.tanh(conv(x, edge_index))
concat_states.append(x)
concat_states = torch.cat(concat_states, 1)
x = global_add_pool(concat_states, batch)
x = F.relu(self.lin1(x))
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin2(x)
if self.regression:
return x[:, 0]
else:
return F.log_softmax(x, dim=-1)
def __repr__(self):
return self.__class__.__name__
class DGCNN(GNN):
# DGCNN from [Zhang et al. AAAI 2018], GCN message passing + SortPooling
def __init__(self, dataset, gconv=GCNConv, latent_dim=[32, 32, 32, 1], k=30,
regression=False, adj_dropout=0.2, force_undirected=False):
super(DGCNN, self).__init__(
dataset, gconv, latent_dim, regression, adj_dropout, force_undirected
)
if k < 1: # transform percentile to number
node_nums = sorted([g.num_nodes for g in dataset])
k = node_nums[int(math.ceil(k * len(node_nums)))-1]
k = max(10, k) # no smaller than 10
self.k = int(k)
print('k used in sortpooling is:', self.k)
conv1d_channels = [16, 32]
conv1d_activation = nn.ReLU()
self.total_latent_dim = sum(latent_dim)
conv1d_kws = [self.total_latent_dim, 5]
self.conv1d_params1 = Conv1d(1, conv1d_channels[0], conv1d_kws[0], conv1d_kws[0])
self.maxpool1d = nn.MaxPool1d(2, 2)
self.conv1d_params2 = Conv1d(conv1d_channels[0], conv1d_channels[1], conv1d_kws[1], 1)
dense_dim = int((k - 2) / 2 + 1)
self.dense_dim = (dense_dim - conv1d_kws[1] + 1) * conv1d_channels[1]
self.lin1 = Linear(self.dense_dim, 128)
def reset_parameters(self):
for conv in self.convs:
conv.reset_parameters()
self.conv1d_params1.reset_parameters()
self.conv1d_params2.reset_parameters()
self.lin1.reset_parameters()
self.lin2.reset_parameters()
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
if self.adj_dropout > 0:
edge_index, edge_type = dropout_adj(
edge_index, edge_type, p=self.adj_dropout,
force_undirected=self.force_undirected, num_nodes=len(x),
training=self.training
)
concat_states = []
for conv in self.convs:
x = torch.tanh(conv(x, edge_index))
concat_states.append(x)
concat_states = torch.cat(concat_states, 1)
x = global_sort_pool(concat_states, batch, self.k) # batch * (k*hidden)
x = x.unsqueeze(1) # batch * 1 * (k*hidden)
x = F.relu(self.conv1d_params1(x))
x = self.maxpool1d(x)
x = F.relu(self.conv1d_params2(x))
x = x.view(len(x), -1) # flatten
x = F.relu(self.lin1(x))
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin2(x)
if self.regression:
return x[:, 0]
else:
return F.log_softmax(x, dim=-1)
class DGCNN_RS(DGCNN):
# A DGCNN model using RGCN convolution to take consideration of edge types.
def __init__(self, dataset, gconv=RGCNConv, latent_dim=[32, 32, 32, 1], k=30,
num_relations=5, num_bases=2, regression=False, adj_dropout=0.2,
force_undirected=False):
super(DGCNN_RS, self).__init__(
dataset,
GCNConv,
latent_dim,
k,
regression,
adj_dropout=adj_dropout,
force_undirected=force_undirected
)
self.convs = torch.nn.ModuleList()
self.convs.append(gconv(dataset.num_features, latent_dim[0], num_relations, num_bases))
for i in range(0, len(latent_dim)-1):
self.convs.append(gconv(latent_dim[i], latent_dim[i+1], num_relations, num_bases))
def forward(self, data):
x, edge_index, edge_type, batch = data.x, data.edge_index, data.edge_type, data.batch
if self.adj_dropout > 0:
edge_index, edge_type = dropout_adj(
edge_index, edge_type, p=self.adj_dropout,
force_undirected=self.force_undirected, num_nodes=len(x),
training=self.training
)
concat_states = []
for conv in self.convs:
x = torch.tanh(conv(x, edge_index, edge_type))
concat_states.append(x)
concat_states = torch.cat(concat_states, 1)
x = global_sort_pool(concat_states, batch, self.k) # batch * (k*hidden)
x = x.unsqueeze(1) # batch * 1 * (k*hidden)
x = F.relu(self.conv1d_params1(x))
x = self.maxpool1d(x)
x = F.relu(self.conv1d_params2(x))
x = x.view(len(x), -1) # flatten
x = F.relu(self.lin1(x))
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin2(x)
if self.regression:
return x[:, 0]
else:
return F.log_softmax(x, dim=-1)
class IGMC(GNN):
# The GNN model of Inductive Graph-based Matrix Completion.
# Use RGCN convolution + center-nodes readout.
def __init__(self, dataset, gconv=RGCNConv, latent_dim=[32, 32, 32, 32],
num_relations=5, num_bases=2, regression=False, adj_dropout=0.2,
force_undirected=False, side_features=False, n_side_features=0,
multiply_by=1, use_graphnorm=False, model_type = 'IGMC', gconv_type = 'GCNConv'):
if gconv_type == 'SAGEConv':
super(IGMC, self).__init__(
dataset, SAGEConv, latent_dim, regression, adj_dropout, force_undirected
)
# Default: GCNConv
else:
super(IGMC, self).__init__(
dataset, GCNConv, latent_dim, regression, adj_dropout, force_undirected
)
self.multiply_by = multiply_by
#convolutions
self.convs = torch.nn.ModuleList()
self.convs.append(gconv(dataset.num_features, latent_dim[0], num_relations, num_bases))
for i in range(0, len(latent_dim)-1):
self.convs.append(gconv(latent_dim[i], latent_dim[i+1], num_relations, num_bases))
#norms
self.use_graphnorm = use_graphnorm
self.model_type = model_type
if use_graphnorm:
self.norms = torch.nn.ModuleList()
for i in range(len(latent_dim)):
self.norms.append(GraphNorm(latent_dim[i]))
if model_type == 'MaxPoolIGMC':
self.maxpool1d = nn.MaxPool1d(4)
self.lin1 = Linear(int(2*sum(latent_dim)/4), 128)
else:
self.lin1 = Linear(2*sum(latent_dim), 128)
self.side_features = side_features
if side_features:
self.lin1 = Linear(2*sum(latent_dim)+n_side_features, 128)
#LSTM Attention params
self.hidden_size = 16
self.bi_lstm = torch.nn.LSTM(input_size = 32, hidden_size = self.hidden_size, bidirectional = True)
self.lin3 = Linear(32, 1).cuda()
self.softmax = torch.nn.Softmax(dim = 0)
self.num_layers = len(latent_dim)
def forward(self, data):
start = time.time()
x, edge_index, edge_type, batch = data.x, data.edge_index, data.edge_type, data.batch
if self.adj_dropout > 0:
edge_index, edge_type = dropout_adj(
edge_index, edge_type, p=self.adj_dropout,
force_undirected=self.force_undirected, num_nodes=len(x),
training=self.training
)
states = []
for i in range(len(self.convs)):
x = self.convs[i](x, edge_index, edge_type)
if self.use_graphnorm:
x = self.norms[i](x, batch)
x = torch.tanh(x)
states.append(x)
if self.model_type == 'MaxPoolIGMC':
x = torch.stack(states, 2)
x = self.maxpool1d(x)
states = x.squeeze(-1)
elif self.model_type == 'LSTMAttentionIGMC':
x = torch.stack(states, 0)
#Bi-lstm
output, _ = self.bi_lstm(x)
batch_size = list(output.shape)[1]
output = output.view(self.num_layers, batch_size, 2, self.hidden_size) # (seq_len, batch_size, num_directions, hidden_size)
output_forward = output[:, :, 0, :] # (seq_len, batch_size, hidden_size)
output_backward = output[:, :, 1, :]
output_concatenated = torch.cat([output_forward, output_backward], dim = 2)
# Attention
# Reshape
dense_input = output_concatenated.permute((1, 0, 2))
# Dense
dense_output = self.lin3(dense_input)
# Average across 0th dimension
dense_output = torch.mean(dense_output, dim = 0).reshape((self.num_layers, ))
# Softmax
softmax_output = self.softmax(dense_output)
# Multiply weights to states
states = [states[i] * softmax_output[i] for i in range(self.num_layers)]
# Concat states
states = torch.cat(states, 1)
# Default is IGMC
else:
states = torch.cat(states, 1)
users = data.x[:, 0] == 1
items = data.x[:, 1] == 1
x = torch.cat([states[users], states[items]], 1)
if self.side_features:
x = torch.cat([x, data.u_feature, data.v_feature], 1)
x = F.relu(self.lin1(x))
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin2(x)
if self.regression:
return x[:, 0] * self.multiply_by
else:
return F.log_softmax(x, dim=-1)