/
_vae.py
740 lines (663 loc) · 27.4 KB
/
_vae.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
"""Main module."""
import logging
from collections.abc import Iterable
from typing import Callable, Literal, Optional
import numpy as np
import torch
import torch.nn.functional as F
from torch import logsumexp
from torch.distributions import Normal
from torch.distributions import kl_divergence as kl
from scvi import REGISTRY_KEYS
from scvi._types import Tunable
from scvi.data._constants import ADATA_MINIFY_TYPE
from scvi.distributions import NegativeBinomial, Poisson, ZeroInflatedNegativeBinomial
from scvi.module.base import BaseMinifiedModeModuleClass, LossOutput, auto_move_data
from scvi.nn import DecoderSCVI, Encoder, LinearDecoderSCVI, one_hot
torch.backends.cudnn.benchmark = True
logger = logging.getLogger(__name__)
class VAE(BaseMinifiedModeModuleClass):
"""Variational auto-encoder model.
This is an implementation of the scVI model described in :cite:p:`Lopez18`.
Parameters
----------
n_input
Number of input genes
n_batch
Number of batches, if 0, no batch correction is performed.
n_labels
Number of labels
n_hidden
Number of nodes per hidden layer
n_latent
Dimensionality of the latent space
n_layers
Number of hidden layers used for encoder and decoder NNs
n_continuous_cov
Number of continuous covarites
n_cats_per_cov
Number of categories for each extra categorical covariate
dropout_rate
Dropout rate for neural networks
dispersion
One of the following
* ``'gene'`` - dispersion parameter of NB is constant per gene across cells
* ``'gene-batch'`` - dispersion can differ between different batches
* ``'gene-label'`` - dispersion can differ between different labels
* ``'gene-cell'`` - dispersion can differ for every gene in every cell
log_variational
Log(data+1) prior to encoding for numerical stability. Not normalization.
gene_likelihood
One of
* ``'nb'`` - Negative binomial distribution
* ``'zinb'`` - Zero-inflated negative binomial distribution
* ``'poisson'`` - Poisson distribution
latent_distribution
One of
* ``'normal'`` - Isotropic normal
* ``'ln'`` - Logistic normal with normal params N(0, 1)
encode_covariates
Whether to concatenate covariates to expression in encoder
deeply_inject_covariates
Whether to concatenate covariates into output of hidden layers in encoder/decoder. This option
only applies when `n_layers` > 1. The covariates are concatenated to the input of subsequent hidden layers.
use_batch_norm
Whether to use batch norm in layers.
use_layer_norm
Whether to use layer norm in layers.
use_size_factor_key
Use size_factor AnnDataField defined by the user as scaling factor in mean of conditional distribution.
Takes priority over `use_observed_lib_size`.
use_observed_lib_size
Use observed library size for RNA as scaling factor in mean of conditional distribution
library_log_means
1 x n_batch array of means of the log library sizes. Parameterizes prior on library size if
not using observed library size.
library_log_vars
1 x n_batch array of variances of the log library sizes. Parameterizes prior on library size if
not using observed library size.
var_activation
Callable used to ensure positivity of the variational distributions' variance.
When `None`, defaults to `torch.exp`.
extra_encoder_kwargs
Extra keyword arguments passed into :class:`~scvi.nn.Encoder`.
extra_decoder_kwargs
Extra keyword arguments passed into :class:`~scvi.nn.DecoderSCVI`.
"""
def __init__(
self,
n_input: int,
n_batch: int = 0,
n_labels: int = 0,
n_hidden: Tunable[int] = 128,
n_latent: Tunable[int] = 10,
n_layers: Tunable[int] = 1,
n_continuous_cov: int = 0,
n_cats_per_cov: Optional[Iterable[int]] = None,
dropout_rate: Tunable[float] = 0.1,
dispersion: Tunable[Literal["gene", "gene-batch", "gene-label", "gene-cell"]] = "gene",
log_variational: Tunable[bool] = True,
gene_likelihood: Tunable[Literal["zinb", "nb", "poisson"]] = "zinb",
latent_distribution: Tunable[Literal["normal", "ln"]] = "normal",
encode_covariates: Tunable[bool] = False,
deeply_inject_covariates: Tunable[bool] = True,
use_batch_norm: Tunable[Literal["encoder", "decoder", "none", "both"]] = "both",
use_layer_norm: Tunable[Literal["encoder", "decoder", "none", "both"]] = "none",
use_size_factor_key: bool = False,
use_observed_lib_size: Tunable[bool] = True,
library_log_means: Optional[np.ndarray] = None,
library_log_vars: Optional[np.ndarray] = None,
var_activation: Tunable[Callable] = None,
extra_encoder_kwargs: Optional[dict] = None,
extra_decoder_kwargs: Optional[dict] = None,
):
super().__init__()
self.dispersion = dispersion
self.n_latent = n_latent
self.log_variational = log_variational
self.gene_likelihood = gene_likelihood
# Automatically deactivate if useless
self.n_batch = n_batch
self.n_labels = n_labels
self.latent_distribution = latent_distribution
self.encode_covariates = encode_covariates
self.use_size_factor_key = use_size_factor_key
self.use_observed_lib_size = use_size_factor_key or use_observed_lib_size
if not self.use_observed_lib_size:
if library_log_means is None or library_log_vars is None:
raise ValueError(
"If not using observed_lib_size, "
"must provide library_log_means and library_log_vars."
)
self.register_buffer("library_log_means", torch.from_numpy(library_log_means).float())
self.register_buffer("library_log_vars", torch.from_numpy(library_log_vars).float())
if self.dispersion == "gene":
self.px_r = torch.nn.Parameter(torch.randn(n_input))
elif self.dispersion == "gene-batch":
self.px_r = torch.nn.Parameter(torch.randn(n_input, n_batch))
elif self.dispersion == "gene-label":
self.px_r = torch.nn.Parameter(torch.randn(n_input, n_labels))
elif self.dispersion == "gene-cell":
pass
else:
raise ValueError(
"dispersion must be one of ['gene', 'gene-batch',"
" 'gene-label', 'gene-cell'], but input was "
"{}.format(self.dispersion)"
)
use_batch_norm_encoder = use_batch_norm == "encoder" or use_batch_norm == "both"
use_batch_norm_decoder = use_batch_norm == "decoder" or use_batch_norm == "both"
use_layer_norm_encoder = use_layer_norm == "encoder" or use_layer_norm == "both"
use_layer_norm_decoder = use_layer_norm == "decoder" or use_layer_norm == "both"
# z encoder goes from the n_input-dimensional data to an n_latent-d
# latent space representation
n_input_encoder = n_input + n_continuous_cov * encode_covariates
cat_list = [n_batch] + list([] if n_cats_per_cov is None else n_cats_per_cov)
encoder_cat_list = cat_list if encode_covariates else None
_extra_encoder_kwargs = extra_encoder_kwargs or {}
self.z_encoder = Encoder(
n_input_encoder,
n_latent,
n_cat_list=encoder_cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
distribution=latent_distribution,
inject_covariates=deeply_inject_covariates,
use_batch_norm=use_batch_norm_encoder,
use_layer_norm=use_layer_norm_encoder,
var_activation=var_activation,
return_dist=True,
**_extra_encoder_kwargs,
)
# l encoder goes from n_input-dimensional data to 1-d library size
self.l_encoder = Encoder(
n_input_encoder,
1,
n_layers=1,
n_cat_list=encoder_cat_list,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
inject_covariates=deeply_inject_covariates,
use_batch_norm=use_batch_norm_encoder,
use_layer_norm=use_layer_norm_encoder,
var_activation=var_activation,
return_dist=True,
**_extra_encoder_kwargs,
)
# decoder goes from n_latent-dimensional space to n_input-d data
n_input_decoder = n_latent + n_continuous_cov
_extra_decoder_kwargs = extra_decoder_kwargs or {}
self.decoder = DecoderSCVI(
n_input_decoder,
n_input,
n_cat_list=cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
inject_covariates=deeply_inject_covariates,
use_batch_norm=use_batch_norm_decoder,
use_layer_norm=use_layer_norm_decoder,
scale_activation="softplus" if use_size_factor_key else "softmax",
**_extra_decoder_kwargs,
)
def _get_inference_input(
self,
tensors,
):
batch_index = tensors[REGISTRY_KEYS.BATCH_KEY]
cont_key = REGISTRY_KEYS.CONT_COVS_KEY
cont_covs = tensors[cont_key] if cont_key in tensors.keys() else None
cat_key = REGISTRY_KEYS.CAT_COVS_KEY
cat_covs = tensors[cat_key] if cat_key in tensors.keys() else None
if self.minified_data_type is None:
x = tensors[REGISTRY_KEYS.X_KEY]
input_dict = {
"x": x,
"batch_index": batch_index,
"cont_covs": cont_covs,
"cat_covs": cat_covs,
}
else:
if self.minified_data_type == ADATA_MINIFY_TYPE.LATENT_POSTERIOR:
qzm = tensors[REGISTRY_KEYS.LATENT_QZM_KEY]
qzv = tensors[REGISTRY_KEYS.LATENT_QZV_KEY]
observed_lib_size = tensors[REGISTRY_KEYS.OBSERVED_LIB_SIZE]
input_dict = {
"qzm": qzm,
"qzv": qzv,
"observed_lib_size": observed_lib_size,
}
else:
raise NotImplementedError(f"Unknown minified-data type: {self.minified_data_type}")
return input_dict
def _get_generative_input(self, tensors, inference_outputs):
z = inference_outputs["z"]
library = inference_outputs["library"]
batch_index = tensors[REGISTRY_KEYS.BATCH_KEY]
y = tensors[REGISTRY_KEYS.LABELS_KEY]
cont_key = REGISTRY_KEYS.CONT_COVS_KEY
cont_covs = tensors[cont_key] if cont_key in tensors.keys() else None
cat_key = REGISTRY_KEYS.CAT_COVS_KEY
cat_covs = tensors[cat_key] if cat_key in tensors.keys() else None
size_factor_key = REGISTRY_KEYS.SIZE_FACTOR_KEY
size_factor = (
torch.log(tensors[size_factor_key]) if size_factor_key in tensors.keys() else None
)
input_dict = {
"z": z,
"library": library,
"batch_index": batch_index,
"y": y,
"cont_covs": cont_covs,
"cat_covs": cat_covs,
"size_factor": size_factor,
}
return input_dict
def _compute_local_library_params(self, batch_index):
"""Computes local library parameters.
Compute two tensors of shape (batch_index.shape[0], 1) where each
element corresponds to the mean and variances, respectively, of the
log library sizes in the batch the cell corresponds to.
"""
n_batch = self.library_log_means.shape[1]
local_library_log_means = F.linear(one_hot(batch_index, n_batch), self.library_log_means)
local_library_log_vars = F.linear(one_hot(batch_index, n_batch), self.library_log_vars)
return local_library_log_means, local_library_log_vars
@auto_move_data
def _regular_inference(
self,
x,
batch_index,
cont_covs=None,
cat_covs=None,
n_samples=1,
):
"""High level inference method.
Runs the inference (encoder) model.
"""
x_ = x
if self.use_observed_lib_size:
library = torch.log(x.sum(1)).unsqueeze(1)
if self.log_variational:
x_ = torch.log(1 + x_)
if cont_covs is not None and self.encode_covariates:
encoder_input = torch.cat((x_, cont_covs), dim=-1)
else:
encoder_input = x_
if cat_covs is not None and self.encode_covariates:
categorical_input = torch.split(cat_covs, 1, dim=1)
else:
categorical_input = ()
qz, z = self.z_encoder(encoder_input, batch_index, *categorical_input)
ql = None
if not self.use_observed_lib_size:
ql, library_encoded = self.l_encoder(encoder_input, batch_index, *categorical_input)
library = library_encoded
if n_samples > 1:
untran_z = qz.sample((n_samples,))
z = self.z_encoder.z_transformation(untran_z)
if self.use_observed_lib_size:
library = library.unsqueeze(0).expand(
(n_samples, library.size(0), library.size(1))
)
else:
library = ql.sample((n_samples,))
outputs = {"z": z, "qz": qz, "ql": ql, "library": library}
return outputs
@auto_move_data
def _cached_inference(self, qzm, qzv, observed_lib_size, n_samples=1):
if self.minified_data_type == ADATA_MINIFY_TYPE.LATENT_POSTERIOR:
dist = Normal(qzm, qzv.sqrt())
# use dist.sample() rather than rsample because we aren't optimizing the z here
untran_z = dist.sample() if n_samples == 1 else dist.sample((n_samples,))
z = self.z_encoder.z_transformation(untran_z)
library = torch.log(observed_lib_size)
if n_samples > 1:
library = library.unsqueeze(0).expand(
(n_samples, library.size(0), library.size(1))
)
else:
raise NotImplementedError(f"Unknown minified-data type: {self.minified_data_type}")
outputs = {"z": z, "qz_m": qzm, "qz_v": qzv, "ql": None, "library": library}
return outputs
@auto_move_data
def generative(
self,
z,
library,
batch_index,
cont_covs=None,
cat_covs=None,
size_factor=None,
y=None,
transform_batch=None,
):
"""Runs the generative model."""
# TODO: refactor forward function to not rely on y
# Likelihood distribution
if cont_covs is None:
decoder_input = z
elif z.dim() != cont_covs.dim():
decoder_input = torch.cat(
[z, cont_covs.unsqueeze(0).expand(z.size(0), -1, -1)], dim=-1
)
else:
decoder_input = torch.cat([z, cont_covs], dim=-1)
if cat_covs is not None:
categorical_input = torch.split(cat_covs, 1, dim=1)
else:
categorical_input = ()
if transform_batch is not None:
batch_index = torch.ones_like(batch_index) * transform_batch
if not self.use_size_factor_key:
size_factor = library
px_scale, px_r, px_rate, px_dropout = self.decoder(
self.dispersion,
decoder_input,
size_factor,
batch_index,
*categorical_input,
y,
)
if self.dispersion == "gene-label":
px_r = F.linear(
one_hot(y, self.n_labels), self.px_r
) # px_r gets transposed - last dimension is nb genes
elif self.dispersion == "gene-batch":
px_r = F.linear(one_hot(batch_index, self.n_batch), self.px_r)
elif self.dispersion == "gene":
px_r = self.px_r
px_r = torch.exp(px_r)
if self.gene_likelihood == "zinb":
px = ZeroInflatedNegativeBinomial(
mu=px_rate,
theta=px_r,
zi_logits=px_dropout,
scale=px_scale,
)
elif self.gene_likelihood == "nb":
px = NegativeBinomial(mu=px_rate, theta=px_r, scale=px_scale)
elif self.gene_likelihood == "poisson":
px = Poisson(px_rate, scale=px_scale)
# Priors
if self.use_observed_lib_size:
pl = None
else:
(
local_library_log_means,
local_library_log_vars,
) = self._compute_local_library_params(batch_index)
pl = Normal(local_library_log_means, local_library_log_vars.sqrt())
pz = Normal(torch.zeros_like(z), torch.ones_like(z))
return {
"px": px,
"pl": pl,
"pz": pz,
}
def loss(
self,
tensors,
inference_outputs,
generative_outputs,
kl_weight: float = 1.0,
):
"""Computes the loss function for the model."""
x = tensors[REGISTRY_KEYS.X_KEY]
kl_divergence_z = kl(inference_outputs["qz"], generative_outputs["pz"]).sum(dim=-1)
if not self.use_observed_lib_size:
kl_divergence_l = kl(
inference_outputs["ql"],
generative_outputs["pl"],
).sum(dim=1)
else:
kl_divergence_l = torch.tensor(0.0, device=x.device)
reconst_loss = -generative_outputs["px"].log_prob(x).sum(-1)
kl_local_for_warmup = kl_divergence_z
kl_local_no_warmup = kl_divergence_l
weighted_kl_local = kl_weight * kl_local_for_warmup + kl_local_no_warmup
loss = torch.mean(reconst_loss + weighted_kl_local)
kl_local = {
"kl_divergence_l": kl_divergence_l,
"kl_divergence_z": kl_divergence_z,
}
return LossOutput(loss=loss, reconstruction_loss=reconst_loss, kl_local=kl_local)
@torch.inference_mode()
def sample(
self,
tensors: dict[str, torch.Tensor],
n_samples: int = 1,
max_poisson_rate: float = 1e8,
) -> torch.Tensor:
r"""Generate predictive samples from the posterior predictive distribution.
The posterior predictive distribution is denoted as :math:`p(\hat{x} \mid x)`, where
:math:`x` is the input data and :math:`\hat{x}` is the sampled data.
We sample from this distribution by first sampling ``n_samples`` times from the posterior
distribution :math:`q(z \mid x)` for a given observation, and then sampling from the
likelihood :math:`p(\hat{x} \mid z)` for each of these.
Parameters
----------
tensors
Dictionary of tensors passed into :meth:`~scvi.module.VAE.forward`.
n_samples
Number of Monte Carlo samples to draw from the distribution for each observation.
max_poisson_rate
The maximum value to which to clip the ``rate`` parameter of
:class:`~torch.distributions.Poisson`. Avoids numerical sampling
issues when the parameter is very large due to the variance of the
distribution.
Returns
-------
Tensor on CPU with shape ``(n_obs, n_vars)`` if ``n_samples == 1``, else
``(n_obs, n_vars,)``.
"""
inference_kwargs = {"n_samples": n_samples}
_, generative_outputs = self.forward(
tensors, inference_kwargs=inference_kwargs, compute_loss=False
)
dist = generative_outputs["px"]
if self.gene_likelihood == "poisson":
dist = torch.distributions.Poisson(torch.clamp(dist.rate, max=max_poisson_rate))
# (n_obs, n_vars) if n_samples == 1, else (n_samples, n_obs, n_vars)
samples = dist.sample()
# (n_samples, n_obs, n_vars) -> (n_obs, n_vars, n_samples)
samples = torch.permute(samples, (1, 2, 0)) if n_samples > 1 else samples
return samples.cpu()
@torch.inference_mode()
@auto_move_data
def marginal_ll(
self,
tensors,
n_mc_samples,
return_mean=False,
n_mc_samples_per_pass=1,
):
"""Computes the marginal log likelihood of the model.
Parameters
----------
tensors
Dict of input tensors, typically corresponding to the items of the data loader.
n_mc_samples
Number of Monte Carlo samples to use for the estimation of the marginal log likelihood.
return_mean
Whether to return the mean of marginal likelihoods over cells.
n_mc_samples_per_pass
Number of Monte Carlo samples to use per pass. This is useful to avoid memory issues.
"""
batch_index = tensors[REGISTRY_KEYS.BATCH_KEY]
to_sum = []
if n_mc_samples_per_pass > n_mc_samples:
logger.warn(
"Number of chunks is larger than the total number of samples, setting it to the number of samples"
)
n_mc_samples_per_pass = n_mc_samples
n_passes = int(np.ceil(n_mc_samples / n_mc_samples_per_pass))
for _ in range(n_passes):
# Distribution parameters and sampled variables
inference_outputs, _, losses = self.forward(
tensors, inference_kwargs={"n_samples": n_mc_samples_per_pass}
)
qz = inference_outputs["qz"]
ql = inference_outputs["ql"]
z = inference_outputs["z"]
library = inference_outputs["library"]
# Reconstruction Loss
reconst_loss = losses.dict_sum(losses.reconstruction_loss)
# Log-probabilities
p_z = (
Normal(torch.zeros_like(qz.loc), torch.ones_like(qz.scale)).log_prob(z).sum(dim=-1)
)
p_x_zl = -reconst_loss
q_z_x = qz.log_prob(z).sum(dim=-1)
log_prob_sum = p_z + p_x_zl - q_z_x
if not self.use_observed_lib_size:
(
local_library_log_means,
local_library_log_vars,
) = self._compute_local_library_params(batch_index)
p_l = (
Normal(local_library_log_means, local_library_log_vars.sqrt())
.log_prob(library)
.sum(dim=-1)
)
q_l_x = ql.log_prob(library).sum(dim=-1)
log_prob_sum += p_l - q_l_x
to_sum.append(log_prob_sum)
to_sum = torch.cat(to_sum, dim=0)
batch_log_lkl = logsumexp(to_sum, dim=0) - np.log(n_mc_samples)
if return_mean:
batch_log_lkl = torch.mean(batch_log_lkl).item()
else:
batch_log_lkl = batch_log_lkl.cpu()
return batch_log_lkl
class LDVAE(VAE):
"""Linear-decoded Variational auto-encoder model.
Implementation of :cite:p:`Svensson20`.
This model uses a linear decoder, directly mapping the latent representation
to gene expression levels. It still uses a deep neural network to encode
the latent representation.
Compared to standard VAE, this model is less powerful, but can be used to
inspect which genes contribute to variation in the dataset. It may also be used
for all scVI tasks, like differential expression, batch correction, imputation, etc.
However, batch correction may be less powerful as it assumes a linear model.
Parameters
----------
n_input
Number of input genes
n_batch
Number of batches
n_labels
Number of labels
n_hidden
Number of nodes per hidden layer (for encoder)
n_latent
Dimensionality of the latent space
n_layers_encoder
Number of hidden layers used for encoder NNs
dropout_rate
Dropout rate for neural networks
dispersion
One of the following
* ``'gene'`` - dispersion parameter of NB is constant per gene across cells
* ``'gene-batch'`` - dispersion can differ between different batches
* ``'gene-label'`` - dispersion can differ between different labels
* ``'gene-cell'`` - dispersion can differ for every gene in every cell
log_variational
Log(data+1) prior to encoding for numerical stability. Not normalization.
gene_likelihood
One of
* ``'nb'`` - Negative binomial distribution
* ``'zinb'`` - Zero-inflated negative binomial distribution
* ``'poisson'`` - Poisson distribution
use_batch_norm
Bool whether to use batch norm in decoder
bias
Bool whether to have bias term in linear decoder
latent_distribution
One of
* ``'normal'`` - Isotropic normal
* ``'ln'`` - Logistic normal with normal params N(0, 1)
use_observed_lib_size
Use observed library size for RNA as scaling factor in mean of conditional distribution.
**kwargs
"""
def __init__(
self,
n_input: int,
n_batch: int = 0,
n_labels: int = 0,
n_hidden: int = 128,
n_latent: int = 10,
n_layers_encoder: int = 1,
dropout_rate: float = 0.1,
dispersion: str = "gene",
log_variational: bool = True,
gene_likelihood: str = "nb",
use_batch_norm: bool = True,
bias: bool = False,
latent_distribution: str = "normal",
use_observed_lib_size: bool = False,
**kwargs,
):
super().__init__(
n_input=n_input,
n_batch=n_batch,
n_labels=n_labels,
n_hidden=n_hidden,
n_latent=n_latent,
n_layers=n_layers_encoder,
dropout_rate=dropout_rate,
dispersion=dispersion,
log_variational=log_variational,
gene_likelihood=gene_likelihood,
latent_distribution=latent_distribution,
use_observed_lib_size=use_observed_lib_size,
**kwargs,
)
self.use_batch_norm = use_batch_norm
self.z_encoder = Encoder(
n_input,
n_latent,
n_layers=n_layers_encoder,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
distribution=latent_distribution,
use_batch_norm=True,
use_layer_norm=False,
return_dist=True,
)
self.l_encoder = Encoder(
n_input,
1,
n_layers=1,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
use_batch_norm=True,
use_layer_norm=False,
return_dist=True,
)
self.decoder = LinearDecoderSCVI(
n_latent,
n_input,
n_cat_list=[n_batch],
use_batch_norm=use_batch_norm,
use_layer_norm=False,
bias=bias,
)
@torch.inference_mode()
def get_loadings(self) -> np.ndarray:
"""Extract per-gene weights (for each Z, shape is genes by dim(Z)) in the linear decoder."""
# This is BW, where B is diag(b) batch norm, W is weight matrix
if self.use_batch_norm is True:
w = self.decoder.factor_regressor.fc_layers[0][0].weight
bn = self.decoder.factor_regressor.fc_layers[0][1]
sigma = torch.sqrt(bn.running_var + bn.eps)
gamma = bn.weight
b = gamma / sigma
b_identity = torch.diag(b)
loadings = torch.matmul(b_identity, w)
else:
loadings = self.decoder.factor_regressor.fc_layers[0][0].weight
loadings = loadings.detach().cpu().numpy()
if self.n_batch > 1:
loadings = loadings[:, : -self.n_batch]
return loadings