-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodels.py
217 lines (166 loc) · 6.85 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 torch.nn as nn
import torch.nn.functional as F
import vae_models as torch_maven
import numpy
# from pygcn.layers import GraphConvolution
import logging
import numpy as np
class PartialNLL(nn.Module):
def __init__(self):
super(PartialNLL, self).__init__()
def forward(self, theta, R, censored):
# theta = theta.double()
# R = R.double()
# censored = censored.double()
exp_theta = torch.exp(theta)
# observed = (censored.size(0) - torch.sum(censored)).cuda()
observed = 1 - censored
# observed = ipcw_weights
num_observed = torch.sum(observed).cuda()
loss = -(torch.sum((theta.reshape(-1)- torch.log(torch.sum((exp_theta * R.t()), 0))) * observed) / num_observed)
# loss = loss.float()
if np.isnan(loss.data.tolist()):
for a,b in zip(theta, exp_theta):
print(a,b)
return loss
class CoxRegression(nn.Module):
def __init__(self, nfeat):
super(CoxRegression, self).__init__()
self.fc1 = nn.Linear(nfeat, 1)
self.init_hidden()
def forward(self, x, coo=None):
x = self.fc1(x)
return x
def init_hidden(self):
nn.init.xavier_normal_(self.fc1.weight)
class Coxnnet(nn.Module):
def __init__(self, nfeat):
super(Coxnnet, self).__init__()
self.fc1 = nn.Linear(nfeat, int(numpy.ceil(nfeat ** 0.5)))
self.fc2 = nn.Linear(int(numpy.ceil(nfeat ** 0.5)), 1)
def forward(self, x, coo=None):
x = torch.tanh(self.fc1(x))
x = self.fc2(x)
return x
def init_hidden(self):
nn.init.xavier_normal_(self.fc1.weight)
nn.init.xavier_normal_(self.fc2.weight)
class CoxMLP(nn.Module):
def __init__(self, nfeat, nhid, dropout):
super(CoxMLP, self).__init__()
self.fc1 = nn.Linear(nfeat, nhid)
self.fc2 = nn.Linear(nhid, 1)
self.dropout = dropout
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.dropout(x, self.dropout, training=self.training)
x = self.fc2(x)
return x
def init_hidden(self):
nn.init.xavier_normal_(self.fc1.weight)
nn.init.xavier_normal_(self.fc2.weight)
class VAECox(nn.Module):
def __init__(self, config, logger, dropout, gcn_mode, use_gpu, pretrained):
super(VAECox, self).__init__()
# Check number of features!
# torch.load(" <file_path> ")["model_state_dict"])
num_features = 20502
if config.gcn_mode: num_features = 12906
#self.vae = torch_maven.VAE(num_features=num_features, gcn_mode=gcn_mode)
#self.vae = torch_maven.VAE(config, logger, gcn num_features=20531)
#self.vae = torch_maven.VAE(config, logger, num_features, gcn_mode)
self.vae = torch_maven.VAE(config, logger, num_features)
gpu_load = torch.load(pretrained, map_location=torch.device('cpu'))
if use_gpu: gpu_load = torch.load(pretrained)
self.vae.load_state_dict(gpu_load["model_state_dict"])
for param in self.vae.parameters():
param.requires_grad = True
# self.cox = CoxRegression(256, 0.5)
#self.cox = CoxMLP(256, 100, dropout)
self.cox = Coxnnet(128)
def forward(self, x, coo=None):
#x = self.vae.dimension_reduction(x, coo)
#x = self.cox(x)
h = self.vae.encode(x)
xm = self.vae.encode_mu(h)
x = self.cox(xm)
return x
class DAECox(nn.Module):
def __init__(self, config, logger, dropout, gcn_mode, use_gpu, pretrained):
super(DAECox, self).__init__()
# Check number of features!
# torch.load(" <file_path> ")["model_state_dict"])
num_features = 20502
if config.gcn_mode: num_features = 12906
# self.ae = torch_maven.AE(num_features=num_features, gcn_mode=gcn_mode)
# self.ae = torch_maven.AE(config, logger, num_features, gcn_mode)
self.dae = torch_maven.DAE(config, logger, num_features)
gpu_load = torch.load(pretrained, map_location=torch.device('cpu'))
if use_gpu: gpu_load = torch.load(pretrained)
self.dae.load_state_dict(gpu_load["model_state_dict"])
# self.cox = CoxRegression(256, 0.5)
#self.cox = CoxMLP(256, 100, dropout)
# self.cox = CoxMLP(128, 100, dropout)
self.cox = Coxnnet(128)
for param in self.dae.parameters():
param.requires_grad = True
def forward(self, x, coo=None):
x = self.dae.encode(x)
#x = self.ae.dimension_reduction(x, coo)
x = self.cox(x)
return x
class AECox(nn.Module):
def __init__(self, config, logger, dropout, gcn_mode, use_gpu, pretrained):
super(AECox, self).__init__()
# Check number of features!
# torch.load(" <file_path> ")["model_state_dict"])
num_features = 20502
if config.gcn_mode: num_features = 12906
# self.ae = torch_maven.AE(num_features=num_features, gcn_mode=gcn_mode)
# self.ae = torch_maven.AE(config, logger, num_features, gcn_mode)
self.ae = torch_maven.AE(config, logger, num_features)
gpu_load = torch.load(pretrained, map_location=torch.device('cpu'))
if use_gpu: gpu_load = torch.load(pretrained)
self.ae.load_state_dict(gpu_load["model_state_dict"])
# self.cox = CoxRegression(256, 0.5)
#self.cox = CoxMLP(256, 100, dropout)
self.cox = CoxMLP(128, 100, dropout)
for param in self.ae.parameters():
param.requires_grad = True
def forward(self, x, coo=None):
x = self.ae.encode(x)
#x = self.ae.dimension_reduction(x, coo)
x = self.cox(x)
return x
class GCN(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout):
super(GCN, self).__init__()
self.gc1 = GraphConvolution(nfeat, nhid)
self.gc2 = GraphConvolution(nhid, nclass)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = self.gc2(x, adj)
#return F.log_softmax(x, dim=1)
return x
class VAECox_test(nn.Module):
def __init__(self, config, logger, dropout, pretrained):
super(VAECox_test, self).__init__()
num_features = 15574
if config.gcn_mode: num_features = 12906
self.vae = torch_maven.VAE(config, logger, num_features)
for param in self.vae.parameters():
param.requires_grad = True
self.cox = CoxMLP(128, 100, dropout)
self.load_state_dict(torch.load(pretrained))
def forward(self, x, coo=None):
h = self.vae.encode(x)
mu = self.vae.encode_mu(h)
x = self.cox(mu)
return x, mu
if __name__ == "__main__":
vae = torch_maven.VAE(num_features=15574)
vae.load_state_dict(torch.load("../data/vae_models/pan_cancer_mRNA_15%/model_600")["model_state_dict"])
print(vae)