-
Notifications
You must be signed in to change notification settings - Fork 1
/
GNN_pyG.py
171 lines (140 loc) · 5.04 KB
/
GNN_pyG.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
import os.path as osp
import torch
import torch.nn.functional as F
from torch.nn import BatchNorm1d as BatchNorm
from torch.nn import Linear, ReLU, Sequential, Flatten, Conv1d, MaxPool1d
from torch_geometric.datasets import TUDataset
from torch_geometric.loader import DataLoader
from torch_geometric.nn import GINConv, global_add_pool
from torch_geometric.nn import GCNConv
from torch_geometric.transforms import OneHotDegree
from torch_geometric.data import Data
import numpy as np
import mne
from mne.decoding import CSP
import random
data_all = np.load('healthy_EEG_EMG_pull_push_data.npy')
label_all = np.load('healthy_EEG_EMG_pull_push_label.npy')
graph_data = np.load('SPMI_healthy_data.npy')
#print(label_all)
#CSP_ncomponents = 40
#
#sample_size = data_all.shape[2]
#
#feature = CSP(n_components=CSP_ncomponents, reg=None, norm_trace=True)
#
##scp_model = feature.get_params()
##np.save('scp_model.npy', scp_model)
#
#print("feature")
#
#feature_fited = feature.fit(data_all, label_all)
#features = feature.transform(data_all)
#
#print(features.shape)
# Load and preprocess data
# Here we randomly generate several graphs for simplicity as an example
graphs = []
for i in range(label_all.shape[0]):
x = torch.tensor(data_all[i], dtype=torch.float).reshape(40,-1)
# x = torch.tensor(features[i], dtype=torch.float).reshape(40,-1)
edges = []
edge_weight = []
label = torch.tensor([label_all[i]]).reshape(1,)
for j in range(40):
for k in range(40):
if graph_data[i,j,k] > 0.5:
edges.append([j, k])
edges = torch.tensor(edges, dtype=torch.long).t().contiguous()
g = Data(x=x, edge_index=edges, y=label)
print(g)
graphs.append(g)
random.shuffle(graphs)
test_dataset = graphs[:len(graphs) // 10]
train_dataset = graphs[len(graphs) // 10:]
test_loader = DataLoader(test_dataset, batch_size=128)
train_loader = DataLoader(train_dataset, batch_size=128)
class Reshape(torch.nn.Module):
def __init__(self, *args):
super(Reshape, self).__init__()
self.shape = args
def forward(self, x):
return x.view((x.size(0),)+self.shape)
class Net(torch.nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels, num_layers):
super().__init__()
self.convs = torch.nn.ModuleList()
self.batch_norms = torch.nn.ModuleList()
for i in range(num_layers):
mlp = Sequential(
Linear(in_channels, 2 * hidden_channels),
BatchNorm(2 * hidden_channels),
ReLU(),
Linear(2 * hidden_channels, hidden_channels),
)
# mlp = Sequential(
# Conv1d(1, 1, kernel_size=64, stride=2, padding=1),
# MaxPool1d(2),
# Linear(360, hidden_channels)
# )
conv = GINConv(mlp, train_eps=True).jittable()
self.convs.append(conv)
self.batch_norms.append(BatchNorm(hidden_channels))
in_channels = hidden_channels
self.lin1 = Linear(hidden_channels, hidden_channels)
self.batch_norm1 = BatchNorm(hidden_channels)
self.lin2 = Linear(hidden_channels, out_channels)
def forward(self, x, edge_index, batch):
for conv, batch_norm in zip(self.convs, self.batch_norms):
x = F.relu(batch_norm(conv(x, edge_index)))
x = global_add_pool(x, batch)
x = F.relu(self.batch_norm1(self.lin1(x)))
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin2(x)
return F.log_softmax(x, dim=-1)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net(1501, 64*2, 2, num_layers=3)
model = model.to(device)
model = torch.jit.script(model)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
def train():
model.train()
total_loss = 0.
for data in train_loader:
data = data.to(device)
optimizer.zero_grad()
# print(data.x.shape)
out = model(data.x, data.edge_index, data.batch)
loss = F.nll_loss(out, data.y)
# print(data.y)
# print(out)
# break
# loss = torch.nn.CrossEntropyLoss(out, data.y)
loss.backward()
total_loss += loss.item() * data.num_graphs
optimizer.step()
return total_loss / len(train_dataset)
@torch.no_grad()
def test(loader):
model.eval()
total_correct = 0
for data in loader:
data = data.to(device)
out = model(data.x, data.edge_index, data.batch)
pred = out.max(dim=1)[1]
total_correct += pred.eq(data.y).sum().item()
return total_correct / len(loader.dataset)
for epoch in range(1, 101):
loss = train()
train_acc = test(train_loader)
test_acc = test(test_loader)
print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}, '
f'Train: {train_acc:.4f}, Test: {test_acc:.4f}')
model.eval()
for data in test_loader:
data = data.to(device)
out = model(data.x, data.edge_index, data.batch)
pred = out.max(dim=1)[1]
print(out)
print(pred)
print(data.y)