forked from sammyj11/Graph-Condensation-Position-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
174 lines (145 loc) · 5.04 KB
/
Copy pathmodels.py
File metadata and controls
174 lines (145 loc) · 5.04 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch_sparse
import torch_geometric.nn as gnn
class GCN(nn.Module):
def __init__(self, indim, outdim, *, hidim=128):
super().__init__()
self.l1 = gnn.GCNConv(indim, hidim)
self.l2 = gnn.GCNConv(hidim, outdim)
def forward(self, x, edge_index):
h = x
h = F.relu(self.l1(h, edge_index))
h = self.l2(h, edge_index)
return h
class GCNE(nn.Module):
def __init__(self, indim, outdim, *, hidim=128):
super().__init__()
self.l1 = gnn.GCNConv(indim, hidim)
self.l2 = gnn.GCNConv(hidim, outdim)
def forward(self, x, edge_index, edge_weight):
h = x
h = F.relu(self.l1(h, edge_index, edge_weight))
h = self.l2(h, edge_index, edge_weight)
return h
class GAT(nn.Module):
def __init__(self, indim, outdim, *, hidim=128):
super().__init__()
self.l1 = gnn.GATConv(indim, hidim)
self.l2 = gnn.GATConv(hidim, outdim)
def forward(self, x, edge_index):
h = x
h = F.relu(self.l1(h, edge_index))
h = self.l2(h, edge_index)
return h
class GIN(nn.Module):
def __init__(self, indim, outdim, *, hidim=128):
super().__init__()
self.l1 = gnn.GINConv(nn.Linear(indim, hidim))
self.l2 = gnn.GINConv(nn.Linear(hidim, outdim))
def forward(self, x, edge_index):
h = x
h = F.relu(self.l1(h, edge_index))
h = self.l2(h, edge_index)
return h
class GraphConvolution(nn.Module):
"""Simple GCN layer, similar to https://github.com/tkipf/pygcn"""
def __init__(self, in_features, out_features, with_bias):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = nn.Parameter(torch.FloatTensor(in_features, out_features))
if with_bias:
self.bias = nn.Parameter(torch.FloatTensor(out_features))
else:
self.bias = None
self.reset_parameters()
def reset_parameters(self):
nn.init.xavier_uniform_(self.weight.data.T)
if self.bias is not None:
self.bias.data.zero_()
def forward(self, x, adj):
"""Graph Convolutional Layer forward function"""
if x.data.is_sparse:
support = torch.spmm(x, self.weight)
else:
support = torch.mm(x, self.weight)
if isinstance(adj, torch_sparse.SparseTensor):
output = torch_sparse.matmul(adj, support)
else:
output = torch.spmm(adj, support)
if self.bias is not None:
return output + self.bias
else:
return output
def __repr__(self):
return (
self.__class__.__name__
+ " ("
+ str(self.in_features)
+ " -> "
+ str(self.out_features)
+ ")"
)
class GCN_GDEM(nn.Module):
def __init__(
self,
indim,
outdim,
hidim,
nlayers=2,
with_relu=True,
with_bias=True,
with_bn=False,
):
super().__init__()
self.with_relu = with_relu
self.layers = nn.ModuleList()
if nlayers == 1:
self.layers.append(
GraphConvolution(indim, outdim, with_bias=with_bias)
)
else:
if with_bn:
self.bns = torch.nn.ModuleList()
self.bns.append(nn.BatchNorm1d(hidim))
self.layers.append(
GraphConvolution(indim, hidim, with_bias=with_bias)
)
for _ in range(nlayers - 2):
self.layers.append(
GraphConvolution(hidim, hidim, with_bias=with_bias)
)
if with_bn:
self.bns.append(nn.BatchNorm1d(hidim))
self.layers.append(
GraphConvolution(hidim, outdim, with_bias=with_bias)
)
def forward(self, x, adj):
for ix, layer in enumerate(self.layers):
x = layer(x, adj)
if ix != len(self.layers) - 1:
if self.with_relu:
x = F.relu(x)
return F.log_softmax(x, dim=1)
class GATE(nn.Module):
def __init__(self, indim, outdim, *, hidim=128):
super().__init__()
self.l1 = gnn.GATConv(indim, hidim, edge_dim=1)
self.l2 = gnn.GATConv(hidim, outdim, edge_dim=1)
def forward(self, x, edge_index, edge_weights=None):
h = x
h = F.relu(self.l1(h, edge_index, edge_weights))
h = self.l2(h, edge_index, edge_weights)
return h
class GINE(nn.Module):
def __init__(self, indim, outdim, *, hidim=128):
super().__init__()
self.l1 = gnn.GINEConv(nn.Linear(indim, hidim), edge_dim=1)
self.l2 = gnn.GINEConv(nn.Linear(hidim, outdim), edge_dim=1)
def forward(self, x, edge_index, edge_weights=None):
h = x
h = F.relu(self.l1(h, edge_index, edge_weights))
h = self.l2(h, edge_index, edge_weights)
return h