-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
426 lines (339 loc) · 17.6 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# setup environment
import os
from . import preprocess
# various import statements
import torch
import torch.nn as nn
from torch.nn.functional import softplus, softmax
from torch.distributions import constraints
import pyro
import pyro.distributions as dist
import pyro.poutine as poutine
from pyro.distributions.util import broadcast_shape
from pyro.optim import MultiStepLR
from pyro.infer import SVI, config_enumerate, TraceEnum_ELBO, RenyiELBO
from torch.utils.data import DataLoader
import numpy as np
import time
from . import utils
####################### 构建编码解码器的函数 ###########################
# Helper for making fully-connected neural networks
def make_fc(dims):
layers = []
for in_dim, out_dim in zip(dims, dims[1:]):
layers.append(nn.Linear(in_dim, out_dim))
layers.append(nn.BatchNorm1d(out_dim))
layers.append(nn.ReLU())
return nn.Sequential(*layers[:-1]) # Exclude final ReLU non-linearity
# Helper for making fully-connected neural networks
def make_fc2(dims):
layers = []
for in_dim, out_dim in zip(dims, dims[1:]):
layers.append(nn.Linear(in_dim, out_dim))
layers.append(nn.ReLU())
return nn.Sequential(*layers[:-1]) # Exclude final ReLU non-linearity
# Splits a tensor in half along the final dimension
def split_in_half(t):
return t.reshape(t.shape[:-1] + (2, -1)).unbind(-2)
# Helper for broadcasting inputs to neural net
def broadcast_inputs(input_args):
shape = broadcast_shape(*[s.shape[:-1] for s in input_args]) + (-1,)
input_args = [s.expand(shape) for s in input_args]
return input_args
####################### 完成需要的编码-解码器 ###########################
# decoder:
# total_special_decoder(z_total,z_special)
# x_decoder(z)
# encode:
# z_encoder(x)
# total_special_encoder(z)
######################## Decoder Part #################################
# Used in parameterizing p(z |z_total,z_special)
class ZSDecoder(nn.Module):
def __init__(self, num_batch, z_special_dim, z_total_dim, hidden_dims1, hidden_dims2):
super().__init__()
dims = [num_batch + z_total_dim] + hidden_dims1 + [2 * z_special_dim]
self.fc = make_fc(dims)
def forward(self, t ,z_total):
z_t = broadcast_inputs([z_total, t])
z_t = torch.cat(z_t, dim=-1)
# We reshape the input to be two-dimensional so that nn.BatchNorm1d behaves correctly
_z_t = z_t.reshape(-1, z_t.size(-1))
hidden = self.fc(_z_t)
# If the input was three-dimensional we now restore the original shape
hidden = hidden.reshape(z_t.shape[:-1] + hidden.shape[-1:])
loc, scale = split_in_half(hidden)
scale = softplus(scale)
return loc, scale
# Used in parameterizing p(x |z)
class XDecoder(nn.Module):
# This __init__ statement is executed once upon construction of the neural network.
# Here we specify that the neural network has input dimension z2_dim
# and output dimension 2 * num_genes.
def __init__(self, num_genes, z_dim, hidden_dims1, hidden_dims2):
super().__init__()
dims = [z_dim] + hidden_dims1 + [2 * num_genes]
self.fc = make_fc(dims)
def forward(self, z):
loc, scale = split_in_half(self.fc(z))
# Note that mu is normalized so that total count information is
# encoded by the latent variable ℓ.
scale = softplus(scale)
return loc, scale
# Used in parameterizing q(z_total | z) and q(z_special | z)
class ZDecoder(nn.Module):
def __init__(self, z_dim, z_total_dim, hidden_dims1, hidden_dims2):
super().__init__()
dims = [z_total_dim] + hidden_dims1 + [2 * z_dim]
self.fc = make_fc2(dims)
def forward(self, z_total):
loc, scale = split_in_half(self.fc(z_total))
# Note that mu is normalized so that total count information is
# encoded by the latent variable ℓ.
scale = softplus(scale)
return loc, scale
# Used in parameterizing q(z_total | z) and q(z_special | z)
class Z2Decoder(nn.Module):
def __init__(self, z_dim, z_total_dim, z_special_dim, hidden_dims1, hidden_dims2):
super().__init__()
dims = [z_total_dim + z_special_dim] + hidden_dims1 + [2 * z_dim]
self.fc = make_fc2(dims)
def forward(self, z_total, z_special):
z_total_special = torch.cat([z_total,z_special], dim=-1)
# We reshape the input to be two-dimensional so that nn.BatchNorm1d behaves correctly
_z_total_special = z_total_special.reshape(-1, z_total_special.size(-1))
hidden = self.fc(_z_total_special)
# If the input was three-dimensional we now restore the original shape
hidden = hidden.reshape(_z_total_special.shape[:-1] + hidden.shape[-1:])
loc, scale = split_in_half(hidden)
# Here and elsewhere softplus ensures that scale is positive. Note that we generally
# expect softplus to be more numerically stable than exp.
scale = softplus(scale)
return loc, scale
######################## Encoder Part #################################
class TSEncoder(nn.Module):
def __init__(self, z_dim, z_special_dim, hidden_dims1, hidden_dims2):
super().__init__()
dims = [z_dim] + hidden_dims1 + [2 * z_special_dim]
self.fc = make_fc(dims)
self.z_special_dim = z_special_dim
def forward(self, z):
# Transform the counts x to log space for increased numerical stability.
# Note that we only use this transformation here; in particular the observation
# distribution in the model is a proper count distribution.
loc, scale = split_in_half(self.fc(z))
# Here and elsewhere softplus ensures that scale is positive. Note that we generally
# expect softplus to be more numerically stable than exp.
scale = softplus(scale)
return loc, scale
class ZSEncoder(nn.Module):
def __init__(self, num_batch, z_special_dim, hidden_dims1, hidden_dims2):
super().__init__()
dims = [z_special_dim] + hidden_dims1 + [num_batch]
self.fc = make_fc(dims)
def forward(self, z_special):
# Transform the counts x to log space for increased numerical stability.
# Note that we only use this transformation here; in particular the observation
# distribution in the model is a proper count distribution.
logits = self.fc(z_special)
return logits
class ZS2Encoder(nn.Module):
def __init__(self, num_batch, z_special_dim, z_total_dim, hidden_dims1, hidden_dims2):
super().__init__()
dims = [z_special_dim + num_batch] + hidden_dims1 + [2 * z_total_dim]
self.fc = make_fc(dims)
self.z_special_dim = z_special_dim
def forward(self, z_special, t):
# This broadcasting is necessary since Pyro expands y during enumeration (but not z2)
zs_t = broadcast_inputs([z_special, t])
zs_t = torch.cat(zs_t, dim=-1)
# We reshape the input to be two-dimensional so that nn.BatchNorm1d behaves correctly
_zs_t = zs_t.reshape(-1, zs_t.size(-1))
hidden = self.fc(_zs_t)
# If the input was three-dimensional we now restore the original shape
hidden = hidden.reshape(zs_t.shape[:-1] + hidden.shape[-1:])
loc, scale = split_in_half(hidden)
scale = softplus(scale)
return loc, scale
# Used in parameterizing q(z_total,t | x)
class ZEncoder(nn.Module):
def __init__(self, z_dim, num_genes, hidden_dims1, hidden_dims2):
super().__init__()
dims = [num_genes] + hidden_dims1 + [2 * z_dim]
self.fc = make_fc(dims)
def forward(self, x):
# Transform the counts x to log space for increased numerical stability.
# Note that we only use this transformation here; in particular the observation
# distribution in the model is a proper count distribution.
h1, h2 = split_in_half(self.fc(x))
loc, scale = h1, softplus(h2)
return loc, scale
####################### 构建pyro模型 ###########################
class iPerturb_model_init(nn.Module):
def __init__(self, num_genes, num_batch, latent_dim1=20, latent_dim2=10, latent_dim3=10, alpha=1e-5, scale_factor=1.0):
self.num_genes = num_genes
self.num_batch = num_batch
# This is the latent_dim dimension of both z, z_total, z_special
self.latent_dim1 = latent_dim1
self.latent_dim2 = latent_dim2
self.latent_dim3 = latent_dim3
# This hyperparameter controls the strength of the auxiliary classification loss
self.alpha = alpha
self.scale_factor = scale_factor
super().__init__()
# Setup the various neural networks used in the model and guide
self.z_decoder = ZDecoder(z_dim=self.latent_dim1, z_total_dim=self.latent_dim2,
hidden_dims1=[100], hidden_dims2=[50])
self.z2_decoder = Z2Decoder(z_dim=self.latent_dim1, z_total_dim=self.latent_dim2,
z_special_dim=self.latent_dim3, hidden_dims1=[100], hidden_dims2=[50])
self.zs_decoder = ZSDecoder(num_batch=self.num_batch ,z_special_dim=self.latent_dim3, z_total_dim=self.latent_dim2, hidden_dims1=[100], hidden_dims2=[50])
self.x_decoder = XDecoder(num_genes=num_genes, hidden_dims1=[100], hidden_dims2=[50], z_dim=self.latent_dim1)
self.ts_encoder = TSEncoder(z_dim=self.latent_dim1,
z_special_dim=self.latent_dim3 ,hidden_dims1=[100], hidden_dims2=[50])
self.zs_encoder = ZSEncoder(num_batch=self.num_batch, z_special_dim=self.latent_dim3 ,hidden_dims1=[100], hidden_dims2=[50])
self.z_encoder = ZEncoder(z_dim=self.latent_dim1, num_genes=num_genes, hidden_dims1=[100], hidden_dims2=[50])
self.zs2_encoder = ZS2Encoder( num_batch=self.num_batch,
z_total_dim=self.latent_dim2, z_special_dim=self.latent_dim3 ,hidden_dims1=[100], hidden_dims2=[50])
self.epsilon = 0.006
def model(self, x, one_hot):
# Register various nn.Modules (i.e. the decoder/encoder networks) with Pyro
pyro.module("iPerutrb_model", self)
# This gene-level parameter modulates the variance of the observation distribution
theta = pyro.param("inverse_dispersion", 10.0 * x.new_ones(self.num_genes),
constraint=constraints.positive)
# We scale all sample statements by scale_factor so that the ELBO loss function
# is normalized wrt the number of datapoints and genes.
# This helps with numerical stability during optimization.
with pyro.plate("batch", len(x)), poutine.scale(scale=self.scale_factor):
z_total = pyro.sample("z_total", dist.Normal(0, x.new_ones(self.latent_dim2)).to_event(1))
t_logit = pyro.sample("t_logit", dist.Dirichlet(x.new_ones(self.num_batch)))
t = pyro.sample("t", dist.OneHotCategorical(logits=t_logit), obs=one_hot)
z_special_loc, z_special_scale = self.zs_decoder(z_total, t_logit) # t_logit or t_smooth
z_special = pyro.sample("z_special", dist.Normal(z_special_loc, z_special_scale).to_event(1))
z_loc, z_scale = self.z2_decoder(z_total, z_special)
z = pyro.sample("z", dist.Normal(z_loc, z_scale).to_event(1))
x_loc, x_scale = self.x_decoder(z)
x_scale = softplus(x_scale + theta + self.epsilon)
x = pyro.sample("x", dist.Normal(x_loc, x_scale).to_event(1), obs=x)
# The guide specifies the variational distribution
def guide(self, x, one_hot):
pyro.module("iPerutrb_model", self)
with pyro.plate("batch", len(x)), poutine.scale(scale=self.scale_factor):
z_loc, z_scale = self.z_encoder(x)
z = pyro.sample("z", dist.Normal(z_loc, z_scale).to_event(1))
z_special_loc, z_special_scale = self.ts_encoder(z)
z_special = pyro.sample("z_special", dist.Normal(z_special_loc, z_special_scale).to_event(1))
t_logit = self.zs_encoder(z_special)
t_dist = dist.OneHotCategorical(logits=t_logit)
if one_hot is None:
t = pyro.sample("t", t_dist)
else:
# x is labeled so add a classification loss term
# (this way q(y|z2) learns from both labeled and unlabeled data)
classification_loss = t_dist.log_prob(one_hot)
# Note that the negative sign appears because we're adding this term in the guide
# and the guide log_prob appears in the ELBO as -log q
pyro.factor("classification_loss", -self.alpha * classification_loss, has_rsample=False)
z_total_loc, z_total_scale = self.zs2_encoder(z_special, t_logit)
z_total = pyro.sample("z_total", dist.Normal(z_total_loc, z_total_scale).to_event(1))
# 用来记录pyro模型迭代的重要参数,并进行模型初始化
def model_init_(hyper, latent_dim1, latent_dim2, latent_dim3, optimizer, lr, gamma, milestones, set_seed=123, cuda = False, alpha=0):
num_genes = hyper['num_genes']
num_batch = hyper['num_batch']
scale_factor = 1.0 / (hyper['batch_size'] * hyper['num_genes'])
# Clear Pyro param store so we don't conflict with previous
# # training runs in this session
# pyro.clear_param_store()
# Fix random number seed
pyro.util.set_rng_seed(set_seed)
# Enable optional validation warnings
pyro.enable_validation(True)
# Instantiate instance of model/guide and various neural networks
iPerturb_model = iPerturb_model_init(num_genes=num_genes,num_batch=num_batch,latent_dim1=latent_dim1, latent_dim2=latent_dim2, latent_dim3=latent_dim3, scale_factor=scale_factor,alpha=alpha)
if cuda == True:
iPerturb_model = iPerturb_model.cuda()
# Setup an optimizer (Adam) and learning rate scheduler.
# We start with a moderately high learning rate (0.006) and
# reduce by a factor of 5 after 20 epochs.
scheduler = MultiStepLR({'optimizer': optimizer,
'optim_args': {'lr': lr},
'gamma': gamma, 'milestones': milestones})
# Tell Pyro to enumerate out y when y is unobserved.
# (By default y would be sampled from the guide)
guide = config_enumerate(iPerturb_model.guide, "parallel", expand=True)
# Setup a variational objective for gradient-based learning.
# Note we use TraceEnum_ELBO in order to leverage Pyro's machinery
# for automatic enumeration of the discrete latent variable y.
elbo = TraceEnum_ELBO(strict_enumeration_warning=False)
# elbo = RenyiELBO(strict_enumeration_warning=False)
svi = SVI(iPerturb_model.model, guide, scheduler, elbo)
return svi, scheduler, iPerturb_model
## 利用pyro进行模型迭代
def RUN_model(epochs,svi,scheduler,dataloader):
pyro.clear_param_store()
start_time = time.time()
for epoch in range(epochs):
losses = []
# Take a gradient step for each mini-batch in the dataset
for x,one_hot,_ in dataloader:
if one_hot is not None:
one_hot = one_hot.type_as(x)
loss = svi.step(x,one_hot)
losses.append(loss)
# Tell the scheduler we've done one epoch.
scheduler.step()
print("[Epoch %02d] Loss: %.5f" % (epoch, np.mean(losses)))
end_time = time.time()
execution_time = end_time - start_time
print(f"Finished training! Time:{execution_time}.")
# 总的RUN
def RUN(datasets,iPerturb_model,svi,scheduler,epochs,hyper,raw,cuda,batch_size=100,if_likelihood=False):
sc_datasets = preprocess.scDataset(datasets,cuda=cuda,transform=preprocess.transform_numpy)
batch_sampler = preprocess.BatchSampler(datasets,batch_size=batch_size,cuda=cuda)
dataloader = DataLoader(sc_datasets, batch_sampler=batch_sampler)
dataloader2 = DataLoader(sc_datasets, batch_size=batch_size, shuffle=True)
# training runs in this session
RUN_model(epochs,svi,scheduler,dataloader)
# Put the neural networks in evaluation mode (needed because of batch norm)
iPerturb_model.eval()
if cuda:
# z
z_pred = iPerturb_model.z_encoder(hyper['x'].cuda())[0]
else:
# z
z_pred = iPerturb_model.z_encoder(hyper['x'])[0]
# 重构
x_pred = iPerturb_model.x_decoder(z_pred)[0]
z_s = iPerturb_model.ts_encoder(z_pred)[0]
t_logit = iPerturb_model.zs_encoder(z_s)
z_t = iPerturb_model.zs2_encoder(z_s, t_logit)[0]
x_pred = x_pred.data.cpu().numpy()
z_pred = z_pred.data.cpu().numpy()
zs_pred = z_s.data.cpu().numpy()
zt_pred = z_t.data.cpu().numpy()
# likelihood
if if_likelihood:
RUN_model(epochs,svi,scheduler,dataloader2)
iPerturb_model.eval()
if cuda:
# z
z_pred_ = iPerturb_model.z_encoder(hyper['x'].cuda())[0]
else:
# z
z_pred_ = iPerturb_model.z_encoder(hyper['x'])[0]
zs_pred_ = iPerturb_model.ts_encoder(z_pred_)[0]
t_logits = iPerturb_model.zs_encoder(zs_pred_)
t_logits = softmax(t_logits,dim=-1).data.cpu().numpy()
likelihood = np.argmax(t_logits, axis=-1)
# 创建 AnnData 对象
datasets = utils.construct_anndata(data = x_pred, columns_names = hyper['var_names'], index_names = hyper['index_names'], raw=raw)
datasets.obsm['x_emb'] = z_pred
datasets.obsm['zt_emb'] = zt_pred
datasets.obsm['zs_emb'] = zs_pred
if if_likelihood:
datasets.obsm['t_logits'] = t_logits
datasets.obs['likelihood'] = likelihood
datasets.obs['t_logits'] = datasets.obsm['t_logits'][:,0]
# ari_max,index_i = utils.Find_ari(datasets)
# print(f'第一层重构已完成,它的ari是{ari_max},它的resolution是{index_i}.')
return x_pred,datasets