-
Notifications
You must be signed in to change notification settings - Fork 83
/
models.py
217 lines (200 loc) · 8.71 KB
/
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
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, global_sort_pool, global_add_pool
from torch_geometric.utils import dropout_adj
from util_functions import *
import pdb
import time
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):
super(IGMC, self).__init__(
dataset, GCNConv, latent_dim, regression, adj_dropout, force_undirected
)
self.multiply_by = multiply_by
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))
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)
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
)
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)
users = data.x[:, 0] == 1
items = data.x[:, 1] == 1
x = torch.cat([concat_states[users], concat_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)