-
Notifications
You must be signed in to change notification settings - Fork 4
/
appendix_J.py
236 lines (184 loc) · 7.35 KB
/
appendix_J.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
from torch_geometric.data import DataLoader,InMemoryDataset
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.data.data import Data
from torch.nn import Sequential, Linear, ReLU
import torch_geometric.transforms as T
from torch_geometric.data import DataLoader
from torch_geometric.utils import normalized_cut
from torch_geometric.nn import (NNConv, graclus, max_pool, max_pool_x,GINConv,
global_mean_pool,GATConv,ChebConv,GCNConv)
import scipy.io as sio
import numpy as np
import matplotlib.pyplot as plt
from utils import BandClassDataset
# read dataset
dataset = BandClassDataset(root='dataset/bandclass', pre_transform=None)
# split dataset
train_loader = DataLoader(dataset[0:3000], batch_size=64, shuffle=True)
val_loader = DataLoader(dataset[3000:4000], batch_size=100, shuffle=False)
test_loader = DataLoader(dataset[4000:5000], batch_size=100, shuffle=False)
class GinNet(nn.Module):
def __init__(self):
super(GinNet, self).__init__()
nn1 = Sequential(Linear(dataset.num_features, 64), ReLU(), Linear(64, 64))
self.conv1 = GINConv(nn1,train_eps=True)
self.bn1 = torch.nn.BatchNorm1d(64)
nn2 = Sequential(Linear(64, 64), ReLU(), Linear(64, 64))
self.conv2 = GINConv(nn2,train_eps=True)
self.bn2 = torch.nn.BatchNorm1d(64)
nn3 = Sequential(Linear(64, 64), ReLU(), Linear(64, 64))
self.conv3 = GINConv(nn3,train_eps=True)
self.bn3 = torch.nn.BatchNorm1d(64)
self.fc1 = torch.nn.Linear(64, 10)
self.fc2 = torch.nn.Linear(10, 1)
def forward(self, data):
x=data.x
edge_index=data.edge_index
x = F.dropout(x, p=0.2, training=self.training)
x = F.relu(self.conv1(x, edge_index))
x = self.bn1(x)
x = F.dropout(x, p=0.2, training=self.training)
x = F.relu(self.conv2(x, edge_index))
x = self.bn2(x)
x = F.dropout(x, p=0.2, training=self.training)
x = F.relu(self.conv3(x, edge_index))
x = self.bn3(x)
x = global_mean_pool(x, data.batch)
x = self.fc1(x)
return self.fc2(x)
class GcnNet(nn.Module):
def __init__(self):
super(GcnNet, self).__init__()
self.conv1 = GCNConv(dataset.num_features, 32*2, cached=False)
self.conv2 = GCNConv(32*2, 64*2, cached=False)
self.conv3 = GCNConv(64*2, 64*2, cached=False)
self.fc1 = torch.nn.Linear(64*2, 10)
self.fc2 = torch.nn.Linear(10, 1)
def forward(self, data):
x=data.x
edge_index=data.edge_index
x = F.dropout(x, p=0.1, training=self.training)
x = F.relu(self.conv1(x, edge_index))
x = F.dropout(x, p=0.1, training=self.training)
x = F.relu(self.conv2(x, edge_index))
x = F.dropout(x, p=0.1, training=self.training)
x = F.relu(self.conv3(x, edge_index))
x = global_mean_pool(x, data.batch)
x = self.fc1(x)
return self.fc2(x)
class MlpNet(nn.Module):
def __init__(self):
super(MlpNet, self).__init__()
self.conv1 = torch.nn.Linear(dataset.num_features, 32)
self.conv2 = torch.nn.Linear(32, 64)
self.conv3 = torch.nn.Linear(64, 64)
self.fc1 = torch.nn.Linear(64, 10)
self.fc2 = torch.nn.Linear(10, 1)
def forward(self, data):
x=data.x
edge_index=data.edge_index
x = F.relu(self.conv1(x))
x = F.dropout(x, p=0.3, training=self.training)
x = F.relu(self.conv2(x))
x = F.dropout(x, p=0.3, training=self.training)
x = F.relu(self.conv3(x))
x = global_mean_pool(x, data.batch)
x = self.fc1(x)
return self.fc2(x)
class ChebNet(nn.Module):
def __init__(self):
super(ChebNet, self).__init__()
S=5
self.conv1 = ChebConv(dataset.num_features, 32,S)
self.conv2 = ChebConv(32, 64, S)
self.conv3 = ChebConv(64, 64, S)
self.fc1 = torch.nn.Linear(64, 10)
self.fc2 = torch.nn.Linear(10, 1)
def forward(self, data):
x=data.x
edge_index=data.edge_index
x = F.dropout(x, p=0.2, training=self.training)
x = F.relu(self.conv1(x, edge_index))
x = F.dropout(x, p=0.2, training=self.training)
x = F.relu(self.conv2(x, edge_index))
x = F.dropout(x, p=0.2, training=self.training)
x = F.relu(self.conv3(x, edge_index))
x = global_mean_pool(x, data.batch)
x = self.fc1(x)
return self.fc2(x)
class GatNet(nn.Module):
def __init__(self):
super(GatNet, self).__init__()
'''number of param (in+3)*head*out
'''
self.conv1 = GATConv(dataset.num_features, 8, heads=8,concat=True, dropout=0.0)
self.conv2 = GATConv(64, 16, heads=8, concat=True, dropout=0.0)
self.conv3 = GATConv(128, 16, heads=8, concat=True, dropout=0.0)
self.fc1 = torch.nn.Linear(128, 10)
self.fc2 = torch.nn.Linear(10, 1)
def forward(self, data):
x=data.x
x = F.dropout(x, p=0.2, training=self.training)
x = F.elu(self.conv1(x, data.edge_index))
x = F.dropout(x, p=0.2, training=self.training)
x = F.elu(self.conv2(x, data.edge_index))
x = F.dropout(x, p=0.2, training=self.training)
x = F.elu(self.conv3(x, data.edge_index))
x = global_mean_pool(x, data.batch)
x = self.fc1(x)
return self.fc2(x)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = ChebNet().to(device) # GatNet ChebNet GcnNet GinNet MlpNet
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
def train(epoch):
model.train()
L=0
correct=0
for data in train_loader:
data = data.to(device)
optimizer.zero_grad()
y_grd= (data.y)
pre=model(data)
pred=F.sigmoid(pre)[:,0]
lss=F.binary_cross_entropy(pred, y_grd,reduction='sum')
lss.backward()
optimizer.step()
correct += torch.round(pred).eq(y_grd).sum().item()
L+=lss.item()
return correct/3000,L/3000
def test():
model.eval()
correct = 0
L=0
for data in test_loader:
data = data.to(device)
pre=model(data)
pred=F.sigmoid(pre)[:,0]
y_grd= (data.y)
correct += torch.round(pred).eq(y_grd).sum().item()
lss=F.binary_cross_entropy(pred, y_grd,reduction='sum')
L+=lss.item()
s1= correct / 1000
correct = 0
Lv=0
for data in val_loader:
data = data.to(device)
pre=model(data)
pred=F.sigmoid(pre)[:,0]
y_grd= (data.y)
correct += torch.round(pred).eq(y_grd).sum().item()
lss=F.binary_cross_entropy(pred, y_grd,reduction='sum')
Lv+=lss.item()
s2= correct / 1000
return s1,L/1000, s2, Lv/1000
bval=1000
btest=0
for epoch in range(1, 101):
tracc,trloss=train(epoch)
test_acc,test_loss,val_acc,val_loss = test()
if bval>val_loss:
bval=val_loss
btest=test_acc
print('Epoch: {:02d}, trloss: {:.4f}, tracc: {:.4f}, Valloss: {:.4f}, Val acc: {:.4f},Testloss: {:.4f}, Test acc: {:.4f},best test acc: {:.4f}'.format(epoch,trloss,tracc,val_loss,val_acc,test_loss,test_acc,btest))