-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
206 lines (150 loc) · 6.2 KB
/
model.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
import torch
import math
import torch.nn as nn
from torch.nn.modules.module import Module
from torch.nn import functional as F
class GCN(Module):
"""
Graph Convolutional Network
"""
def __init__(self, in_features, out_features, bias=True):
super(GCN, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = nn.Parameter(torch.FloatTensor(in_features, out_features))
if bias:
self.bias = nn.Parameter(torch.FloatTensor(out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input, adj):
support = torch.mm(input, self.weight)
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 Encoder(nn.Module):
"""
Encoder Network
"""
def __init__(self, nfeat, nhid1, nhid2, dropout):
super(Encoder, self).__init__()
self.dropout = dropout
self.gc1 = GCN(nfeat, nhid1)
self.gc2 = GCN(nhid1, nhid2)
def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gc2(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
class Decoder_SR(nn.Module):
"""
Decoder Network for Super-Resolution Brain Evolution Trajectory
"""
def __init__(self, nhid1, nhid2, noutSR, dropout, timepoints):
super(Decoder_SR, self).__init__()
self.dropout = dropout
self.timepoints = timepoints
self.gc1 = GCN(nhid1, nhid2)
self.gc2 = GCN(nhid2, noutSR)
self.gc3 = GCN(noutSR, noutSR)
def forward(self, x, adj):
timepoints_prediction = []
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gc2(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
timepoints_prediction.append(x)
for _ in range(1, self.timepoints):
x = F.relu(self.gc3(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
timepoints_prediction.append(x)
return timepoints_prediction
def extract_features(self, x, adj, device):
features_vector = torch.empty((x.shape[0], 0), device=device)
x = self.gc1(x, adj)
features_vector = torch.cat((features_vector, x), dim=1)
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
x = self.gc2(x, adj)
features_vector = torch.cat((features_vector, x), dim=1)
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
for _ in range(1, self.timepoints):
x = self.gc3(x, adj)
features_vector = torch.cat((features_vector, x), dim=1)
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
return features_vector
def forward_from_t(self, x, adj, start_t, end_t):
for _ in range(start_t, end_t):
x = F.relu(self.gc3(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
def forward_once(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gc2(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
class Decoder_LR(nn.Module):
"""
Decpder Network for Low-Resolution Brain Evolution Trajectory
"""
def __init__(self, nhid1, nhid2, noutLR, dropout, timepoints):
super(Decoder_LR, self).__init__()
self.dropout = dropout
self.timepoints = timepoints
self.gc1 = GCN(nhid1, nhid2)
self.gc2 = GCN(nhid2, noutLR)
self.gc3 = GCN(noutLR, noutLR)
def forward(self, x, adj):
timepoints_prediction = []
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gc2(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
timepoints_prediction.append(x)
for _ in range(1, self.timepoints):
x = F.relu(self.gc3(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
timepoints_prediction.append(x)
return timepoints_prediction
def extract_features(self, x, adj, device):
features_vector = torch.empty((x.shape[0], 0), device=device)
x = self.gc1(x, adj)
features_vector = torch.cat((features_vector, x), dim=1)
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
x = self.gc2(x, adj)
features_vector = torch.cat((features_vector, x), dim=1)
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
for _ in range(1, self.timepoints):
x = self.gc3(x, adj)
features_vector = torch.cat((features_vector, x), dim=1)
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
return features_vector
def forward_from_t(self, x, adj, start_t, end_t):
for _ in range(start_t, end_t):
x = F.relu(self.gc3(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
def forward_once(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gc2(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x