-
Notifications
You must be signed in to change notification settings - Fork 342
/
_base_components.py
1047 lines (920 loc) · 34.3 KB
/
_base_components.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import collections
from typing import Callable, Iterable, List, Literal, Optional
import torch
from torch import nn
from torch.distributions import Normal
from torch.nn import ModuleList
from ._utils import one_hot
def _identity(x):
return x
class FCLayers(nn.Module):
"""A helper class to build fully-connected layers for a neural network.
Parameters
----------
n_in
The dimensionality of the input
n_out
The dimensionality of the output
n_cat_list
A list containing, for each category of interest,
the number of categories. Each category will be
included using a one-hot encoding.
n_layers
The number of fully-connected hidden layers
n_hidden
The number of nodes per hidden layer
dropout_rate
Dropout rate to apply to each of the hidden layers
use_batch_norm
Whether to have `BatchNorm` layers or not
use_layer_norm
Whether to have `LayerNorm` layers or not
use_activation
Whether to have layer activation or not
bias
Whether to learn bias in linear layers or not
inject_covariates
Whether to inject covariates in each layer, or just the first (default).
activation_fn
Which activation function to use
"""
def __init__(
self,
n_in: int,
n_out: int,
n_cat_list: Iterable[int] = None,
n_layers: int = 1,
n_hidden: int = 128,
dropout_rate: float = 0.1,
use_batch_norm: bool = True,
use_layer_norm: bool = False,
use_activation: bool = True,
bias: bool = True,
inject_covariates: bool = True,
activation_fn: nn.Module = nn.ReLU,
):
super().__init__()
self.inject_covariates = inject_covariates
layers_dim = [n_in] + (n_layers - 1) * [n_hidden] + [n_out]
if n_cat_list is not None:
# n_cat = 1 will be ignored
self.n_cat_list = [n_cat if n_cat > 1 else 0 for n_cat in n_cat_list]
else:
self.n_cat_list = []
cat_dim = sum(self.n_cat_list)
self.fc_layers = nn.Sequential(
collections.OrderedDict(
[
(
f"Layer {i}",
nn.Sequential(
nn.Linear(
n_in + cat_dim * self.inject_into_layer(i),
n_out,
bias=bias,
),
# non-default params come from defaults in original Tensorflow implementation
nn.BatchNorm1d(n_out, momentum=0.01, eps=0.001)
if use_batch_norm
else None,
nn.LayerNorm(n_out, elementwise_affine=False)
if use_layer_norm
else None,
activation_fn() if use_activation else None,
nn.Dropout(p=dropout_rate) if dropout_rate > 0 else None,
),
)
for i, (n_in, n_out) in enumerate(
zip(layers_dim[:-1], layers_dim[1:])
)
]
)
)
def inject_into_layer(self, layer_num) -> bool:
"""Helper to determine if covariates should be injected."""
user_cond = layer_num == 0 or (layer_num > 0 and self.inject_covariates)
return user_cond
def set_online_update_hooks(self, hook_first_layer=True):
"""Set online update hooks."""
self.hooks = []
def _hook_fn_weight(grad):
categorical_dims = sum(self.n_cat_list)
new_grad = torch.zeros_like(grad)
if categorical_dims > 0:
new_grad[:, -categorical_dims:] = grad[:, -categorical_dims:]
return new_grad
def _hook_fn_zero_out(grad):
return grad * 0
for i, layers in enumerate(self.fc_layers):
for layer in layers:
if i == 0 and not hook_first_layer:
continue
if isinstance(layer, nn.Linear):
if self.inject_into_layer(i):
w = layer.weight.register_hook(_hook_fn_weight)
else:
w = layer.weight.register_hook(_hook_fn_zero_out)
self.hooks.append(w)
b = layer.bias.register_hook(_hook_fn_zero_out)
self.hooks.append(b)
def forward(self, x: torch.Tensor, *cat_list: int):
"""Forward computation on ``x``.
Parameters
----------
x
tensor of values with shape ``(n_in,)``
cat_list
list of category membership(s) for this sample
Returns
-------
:class:`torch.Tensor`
tensor of shape ``(n_out,)``
"""
one_hot_cat_list = [] # for generality in this list many indices useless.
if len(self.n_cat_list) > len(cat_list):
raise ValueError(
"nb. categorical args provided doesn't match init. params."
)
for n_cat, cat in zip(self.n_cat_list, cat_list):
if n_cat and cat is None:
raise ValueError("cat not provided while n_cat != 0 in init. params.")
if n_cat > 1: # n_cat = 1 will be ignored - no additional information
if cat.size(1) != n_cat:
one_hot_cat = one_hot(cat, n_cat)
else:
one_hot_cat = cat # cat has already been one_hot encoded
one_hot_cat_list += [one_hot_cat]
for i, layers in enumerate(self.fc_layers):
for layer in layers:
if layer is not None:
if isinstance(layer, nn.BatchNorm1d):
if x.dim() == 3:
x = torch.cat(
[(layer(slice_x)).unsqueeze(0) for slice_x in x], dim=0
)
else:
x = layer(x)
else:
if isinstance(layer, nn.Linear) and self.inject_into_layer(i):
if x.dim() == 3:
one_hot_cat_list_layer = [
o.unsqueeze(0).expand(
(x.size(0), o.size(0), o.size(1))
)
for o in one_hot_cat_list
]
else:
one_hot_cat_list_layer = one_hot_cat_list
x = torch.cat((x, *one_hot_cat_list_layer), dim=-1)
x = layer(x)
return x
# Encoder
class Encoder(nn.Module):
"""Encode data of ``n_input`` dimensions into a latent space of ``n_output`` dimensions.
Uses a fully-connected neural network of ``n_hidden`` layers.
Parameters
----------
n_input
The dimensionality of the input (data space)
n_output
The dimensionality of the output (latent space)
n_cat_list
A list containing the number of categories
for each category of interest. Each category will be
included using a one-hot encoding
n_layers
The number of fully-connected hidden layers
n_hidden
The number of nodes per hidden layer
dropout_rate
Dropout rate to apply to each of the hidden layers
distribution
Distribution of z
var_eps
Minimum value for the variance;
used for numerical stability
var_activation
Callable used to ensure positivity of the variance.
Defaults to :meth:`torch.exp`.
return_dist
Return directly the distribution of z instead of its parameters.
**kwargs
Keyword args for :class:`~scvi.nn.FCLayers`
"""
def __init__(
self,
n_input: int,
n_output: int,
n_cat_list: Iterable[int] = None,
n_layers: int = 1,
n_hidden: int = 128,
dropout_rate: float = 0.1,
distribution: str = "normal",
var_eps: float = 1e-4,
var_activation: Optional[Callable] = None,
return_dist: bool = False,
**kwargs,
):
super().__init__()
self.distribution = distribution
self.var_eps = var_eps
self.encoder = FCLayers(
n_in=n_input,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
**kwargs,
)
self.mean_encoder = nn.Linear(n_hidden, n_output)
self.var_encoder = nn.Linear(n_hidden, n_output)
self.return_dist = return_dist
if distribution == "ln":
self.z_transformation = nn.Softmax(dim=-1)
else:
self.z_transformation = _identity
self.var_activation = torch.exp if var_activation is None else var_activation
def forward(self, x: torch.Tensor, *cat_list: int):
r"""The forward computation for a single sample.
#. Encodes the data into latent space using the encoder network
#. Generates a mean \\( q_m \\) and variance \\( q_v \\)
#. Samples a new value from an i.i.d. multivariate normal \\( \\sim Ne(q_m, \\mathbf{I}q_v) \\)
Parameters
----------
x
tensor with shape (n_input,)
cat_list
list of category membership(s) for this sample
Returns
-------
3-tuple of :py:class:`torch.Tensor`
tensors of shape ``(n_latent,)`` for mean and var, and sample
"""
# Parameters for latent distribution
q = self.encoder(x, *cat_list)
q_m = self.mean_encoder(q)
q_v = self.var_activation(self.var_encoder(q)) + self.var_eps
dist = Normal(q_m, q_v.sqrt())
latent = self.z_transformation(dist.rsample())
if self.return_dist:
return dist, latent
return q_m, q_v, latent
# Decoder
class DecoderSCVI(nn.Module):
"""Decodes data from latent space of ``n_input`` dimensions into ``n_output`` dimensions.
Uses a fully-connected neural network of ``n_hidden`` layers.
Parameters
----------
n_input
The dimensionality of the input (latent space)
n_output
The dimensionality of the output (data space)
n_cat_list
A list containing the number of categories
for each category of interest. Each category will be
included using a one-hot encoding
n_layers
The number of fully-connected hidden layers
n_hidden
The number of nodes per hidden layer
dropout_rate
Dropout rate to apply to each of the hidden layers
inject_covariates
Whether to inject covariates in each layer, or just the first (default).
use_batch_norm
Whether to use batch norm in layers
use_layer_norm
Whether to use layer norm in layers
scale_activation
Activation layer to use for px_scale_decoder
**kwargs
Keyword args for :class:`~scvi.nn.FCLayers`.
"""
def __init__(
self,
n_input: int,
n_output: int,
n_cat_list: Iterable[int] = None,
n_layers: int = 1,
n_hidden: int = 128,
inject_covariates: bool = True,
use_batch_norm: bool = False,
use_layer_norm: bool = False,
scale_activation: Literal["softmax", "softplus"] = "softmax",
**kwargs,
):
super().__init__()
self.px_decoder = FCLayers(
n_in=n_input,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
dropout_rate=0,
inject_covariates=inject_covariates,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
**kwargs,
)
# mean gamma
if scale_activation == "softmax":
px_scale_activation = nn.Softmax(dim=-1)
elif scale_activation == "softplus":
px_scale_activation = nn.Softplus()
self.px_scale_decoder = nn.Sequential(
nn.Linear(n_hidden, n_output),
px_scale_activation,
)
# dispersion: here we only deal with gene-cell dispersion case
self.px_r_decoder = nn.Linear(n_hidden, n_output)
# dropout
self.px_dropout_decoder = nn.Linear(n_hidden, n_output)
def forward(
self,
dispersion: str,
z: torch.Tensor,
library: torch.Tensor,
*cat_list: int,
):
"""The forward computation for a single sample.
#. Decodes the data from the latent space using the decoder network
#. Returns parameters for the ZINB distribution of expression
#. If ``dispersion != 'gene-cell'`` then value for that param will be ``None``
Parameters
----------
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
z :
tensor with shape ``(n_input,)``
library_size
library size
cat_list
list of category membership(s) for this sample
Returns
-------
4-tuple of :py:class:`torch.Tensor`
parameters for the ZINB distribution of expression
"""
# The decoder returns values for the parameters of the ZINB distribution
px = self.px_decoder(z, *cat_list)
px_scale = self.px_scale_decoder(px)
px_dropout = self.px_dropout_decoder(px)
# Clamp to high value: exp(12) ~ 160000 to avoid nans (computational stability)
px_rate = torch.exp(library) * px_scale # torch.clamp( , max=12)
px_r = self.px_r_decoder(px) if dispersion == "gene-cell" else None
return px_scale, px_r, px_rate, px_dropout
class LinearDecoderSCVI(nn.Module):
"""Linear decoder for scVI."""
def __init__(
self,
n_input: int,
n_output: int,
n_cat_list: Iterable[int] = None,
use_batch_norm: bool = False,
use_layer_norm: bool = False,
bias: bool = False,
**kwargs,
):
super().__init__()
# mean gamma
self.factor_regressor = FCLayers(
n_in=n_input,
n_out=n_output,
n_cat_list=n_cat_list,
n_layers=1,
use_activation=False,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
bias=bias,
dropout_rate=0,
**kwargs,
)
# dropout
self.px_dropout_decoder = FCLayers(
n_in=n_input,
n_out=n_output,
n_cat_list=n_cat_list,
n_layers=1,
use_activation=False,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
bias=bias,
dropout_rate=0,
**kwargs,
)
def forward(
self, dispersion: str, z: torch.Tensor, library: torch.Tensor, *cat_list: int
):
"""Forward pass."""
# The decoder returns values for the parameters of the ZINB distribution
raw_px_scale = self.factor_regressor(z, *cat_list)
px_scale = torch.softmax(raw_px_scale, dim=-1)
px_dropout = self.px_dropout_decoder(z, *cat_list)
px_rate = torch.exp(library) * px_scale
px_r = None
return px_scale, px_r, px_rate, px_dropout
# Decoder
class Decoder(nn.Module):
"""Decodes data from latent space to data space.
``n_input`` dimensions to ``n_output``
dimensions using a fully-connected neural network of ``n_hidden`` layers.
Output is the mean and variance of a multivariate Gaussian
Parameters
----------
n_input
The dimensionality of the input (latent space)
n_output
The dimensionality of the output (data space)
n_cat_list
A list containing the number of categories
for each category of interest. Each category will be
included using a one-hot encoding
n_layers
The number of fully-connected hidden layers
n_hidden
The number of nodes per hidden layer
dropout_rate
Dropout rate to apply to each of the hidden layers
kwargs
Keyword args for :class:`~scvi.module._base.FCLayers`
"""
def __init__(
self,
n_input: int,
n_output: int,
n_cat_list: Iterable[int] = None,
n_layers: int = 1,
n_hidden: int = 128,
**kwargs,
):
super().__init__()
self.decoder = FCLayers(
n_in=n_input,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
dropout_rate=0,
**kwargs,
)
self.mean_decoder = nn.Linear(n_hidden, n_output)
self.var_decoder = nn.Linear(n_hidden, n_output)
def forward(self, x: torch.Tensor, *cat_list: int):
"""The forward computation for a single sample.
#. Decodes the data from the latent space using the decoder network
#. Returns tensors for the mean and variance of a multivariate distribution
Parameters
----------
x
tensor with shape ``(n_input,)``
cat_list
list of category membership(s) for this sample
Returns
-------
2-tuple of :py:class:`torch.Tensor`
Mean and variance tensors of shape ``(n_output,)``
"""
# Parameters for latent distribution
p = self.decoder(x, *cat_list)
p_m = self.mean_decoder(p)
p_v = torch.exp(self.var_decoder(p))
return p_m, p_v
class MultiEncoder(nn.Module):
"""MultiEncoder."""
def __init__(
self,
n_heads: int,
n_input_list: List[int],
n_output: int,
n_hidden: int = 128,
n_layers_individual: int = 1,
n_layers_shared: int = 2,
n_cat_list: Iterable[int] = None,
dropout_rate: float = 0.1,
return_dist: bool = False,
**kwargs,
):
super().__init__()
self.encoders = ModuleList(
[
FCLayers(
n_in=n_input_list[i],
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers_individual,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
use_batch_norm=True,
**kwargs,
)
for i in range(n_heads)
]
)
self.encoder_shared = FCLayers(
n_in=n_hidden,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers_shared,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
**kwargs,
)
self.mean_encoder = nn.Linear(n_hidden, n_output)
self.var_encoder = nn.Linear(n_hidden, n_output)
self.return_dist = return_dist
def forward(self, x: torch.Tensor, head_id: int, *cat_list: int):
"""Forward pass."""
q = self.encoders[head_id](x, *cat_list)
q = self.encoder_shared(q, *cat_list)
q_m = self.mean_encoder(q)
q_v = torch.exp(self.var_encoder(q))
dist = Normal(q_m, q_v.sqrt())
latent = dist.rsample()
if self.return_dist:
return dist, latent
return q_m, q_v, latent
class MultiDecoder(nn.Module):
"""MultiDecoder."""
def __init__(
self,
n_input: int,
n_output: int,
n_hidden_conditioned: int = 32,
n_hidden_shared: int = 128,
n_layers_conditioned: int = 1,
n_layers_shared: int = 1,
n_cat_list: Iterable[int] = None,
dropout_rate: float = 0.2,
**kwargs,
):
super().__init__()
n_out = n_hidden_conditioned if n_layers_shared else n_hidden_shared
if n_layers_conditioned:
self.px_decoder_conditioned = FCLayers(
n_in=n_input,
n_out=n_out,
n_cat_list=n_cat_list,
n_layers=n_layers_conditioned,
n_hidden=n_hidden_conditioned,
dropout_rate=dropout_rate,
use_batch_norm=True,
**kwargs,
)
n_in = n_out
else:
self.px_decoder_conditioned = None
n_in = n_input
if n_layers_shared:
self.px_decoder_final = FCLayers(
n_in=n_in,
n_out=n_hidden_shared,
n_cat_list=[],
n_layers=n_layers_shared,
n_hidden=n_hidden_shared,
dropout_rate=dropout_rate,
use_batch_norm=True,
**kwargs,
)
n_in = n_hidden_shared
else:
self.px_decoder_final = None
self.px_scale_decoder = nn.Sequential(
nn.Linear(n_in, n_output), nn.Softmax(dim=-1)
)
self.px_r_decoder = nn.Linear(n_in, n_output)
self.px_dropout_decoder = nn.Linear(n_in, n_output)
def forward(
self,
z: torch.Tensor,
dataset_id: int,
library: torch.Tensor,
dispersion: str,
*cat_list: int,
):
"""Forward pass."""
px = z
if self.px_decoder_conditioned:
px = self.px_decoder_conditioned(px, *cat_list)
if self.px_decoder_final:
px = self.px_decoder_final(px, *cat_list)
px_scale = self.px_scale_decoder(px)
px_dropout = self.px_dropout_decoder(px)
px_rate = torch.exp(library) * px_scale
px_r = self.px_r_decoder(px) if dispersion == "gene-cell" else None
return px_scale, px_r, px_rate, px_dropout
class DecoderTOTALVI(nn.Module):
"""Decodes data from latent space of ``n_input`` dimensions ``n_output`` dimensions.
Uses a linear decoder.
Parameters
----------
n_input
The dimensionality of the input (latent space)
n_output_genes
The dimensionality of the output (gene space)
n_output_proteins
The dimensionality of the output (protein space)
n_cat_list
A list containing the number of categories
for each category of interest. Each category will be
included using a one-hot encoding
use_batch_norm
Whether to use batch norm in layers
use_layer_norm
Whether to use layer norm in layers
scale_activation
Activation layer to use for px_scale_decoder
"""
def __init__(
self,
n_input: int,
n_output_genes: int,
n_output_proteins: int,
n_cat_list: Iterable[int] = None,
n_layers: int = 1,
n_hidden: int = 256,
dropout_rate: float = 0,
use_batch_norm: float = True,
use_layer_norm: float = False,
scale_activation: Literal["softmax", "softplus"] = "softmax",
):
super().__init__()
self.n_output_genes = n_output_genes
self.n_output_proteins = n_output_proteins
linear_args = {
"n_layers": 1,
"use_activation": False,
"use_batch_norm": False,
"use_layer_norm": False,
"dropout_rate": 0,
}
self.px_decoder = FCLayers(
n_in=n_input,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
)
# mean gamma
self.px_scale_decoder = FCLayers(
n_in=n_hidden + n_input,
n_out=n_output_genes,
n_cat_list=n_cat_list,
**linear_args,
)
if scale_activation == "softmax":
self.px_scale_activation = nn.Softmax(dim=-1)
elif scale_activation == "softplus":
self.px_scale_activation = nn.Softplus()
# background mean first decoder
self.py_back_decoder = FCLayers(
n_in=n_input,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
)
# background mean parameters second decoder
self.py_back_mean_log_alpha = FCLayers(
n_in=n_hidden + n_input,
n_out=n_output_proteins,
n_cat_list=n_cat_list,
**linear_args,
)
self.py_back_mean_log_beta = FCLayers(
n_in=n_hidden + n_input,
n_out=n_output_proteins,
n_cat_list=n_cat_list,
**linear_args,
)
# foreground increment decoder step 1
self.py_fore_decoder = FCLayers(
n_in=n_input,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
)
# foreground increment decoder step 2
self.py_fore_scale_decoder = FCLayers(
n_in=n_hidden + n_input,
n_out=n_output_proteins,
n_cat_list=n_cat_list,
n_layers=1,
use_activation=True,
use_batch_norm=False,
use_layer_norm=False,
dropout_rate=0,
activation_fn=nn.ReLU,
)
# dropout (mixture component for proteins, ZI probability for genes)
self.sigmoid_decoder = FCLayers(
n_in=n_input,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
)
self.px_dropout_decoder_gene = FCLayers(
n_in=n_hidden + n_input,
n_out=n_output_genes,
n_cat_list=n_cat_list,
**linear_args,
)
self.py_background_decoder = FCLayers(
n_in=n_hidden + n_input,
n_out=n_output_proteins,
n_cat_list=n_cat_list,
**linear_args,
)
def forward(self, z: torch.Tensor, library_gene: torch.Tensor, *cat_list: int):
"""The forward computation for a single sample.
#. Decodes the data from the latent space using the decoder network
#. Returns local parameters for the ZINB distribution for genes
#. Returns local parameters for the Mixture NB distribution for proteins
We use the dictionary `px_` to contain the parameters of the ZINB/NB for genes.
The rate refers to the mean of the NB, dropout refers to Bernoulli mixing parameters.
`scale` refers to the quanity upon which differential expression is performed. For genes,
this can be viewed as the mean of the underlying gamma distribution.
We use the dictionary `py_` to contain the parameters of the Mixture NB distribution for proteins.
`rate_fore` refers to foreground mean, while `rate_back` refers to background mean. `scale` refers to
foreground mean adjusted for background probability and scaled to reside in simplex.
`back_alpha` and `back_beta` are the posterior parameters for `rate_back`. `fore_scale` is the scaling
factor that enforces `rate_fore` > `rate_back`.
Parameters
----------
z
tensor with shape ``(n_input,)``
library_gene
library size
cat_list
list of category membership(s) for this sample
Returns
-------
3-tuple (first 2-tuple :py:class:`dict`, last :py:class:`torch.Tensor`)
parameters for the ZINB distribution of expression
"""
px_ = {}
py_ = {}
px = self.px_decoder(z, *cat_list)
px_cat_z = torch.cat([px, z], dim=-1)
unnorm_px_scale = self.px_scale_decoder(px_cat_z, *cat_list)
px_["scale"] = self.px_scale_activation(unnorm_px_scale)
px_["rate"] = library_gene * px_["scale"]
py_back = self.py_back_decoder(z, *cat_list)
py_back_cat_z = torch.cat([py_back, z], dim=-1)
py_["back_alpha"] = self.py_back_mean_log_alpha(py_back_cat_z, *cat_list)
py_["back_beta"] = torch.exp(
self.py_back_mean_log_beta(py_back_cat_z, *cat_list)
)
log_pro_back_mean = Normal(py_["back_alpha"], py_["back_beta"]).rsample()
py_["rate_back"] = torch.exp(log_pro_back_mean)
py_fore = self.py_fore_decoder(z, *cat_list)
py_fore_cat_z = torch.cat([py_fore, z], dim=-1)
py_["fore_scale"] = (
self.py_fore_scale_decoder(py_fore_cat_z, *cat_list) + 1 + 1e-8
)
py_["rate_fore"] = py_["rate_back"] * py_["fore_scale"]
p_mixing = self.sigmoid_decoder(z, *cat_list)
p_mixing_cat_z = torch.cat([p_mixing, z], dim=-1)
px_["dropout"] = self.px_dropout_decoder_gene(p_mixing_cat_z, *cat_list)
py_["mixing"] = self.py_background_decoder(p_mixing_cat_z, *cat_list)
protein_mixing = 1 / (1 + torch.exp(-py_["mixing"]))
py_["scale"] = torch.nn.functional.normalize(
(1 - protein_mixing) * py_["rate_fore"], p=1, dim=-1
)
return (px_, py_, log_pro_back_mean)
# Encoder
class EncoderTOTALVI(nn.Module):
"""Encodes data of ``n_input`` dimensions into a latent space of ``n_output`` dimensions.
Uses a fully-connected neural network of ``n_hidden`` layers.
Parameters
----------
n_input
The dimensionality of the input (data space)
n_output
The dimensionality of the output (latent space)
n_cat_list
A list containing the number of categories
for each category of interest. Each category will be
included using a one-hot encoding
n_layers
The number of fully-connected hidden layers
n_hidden
The number of nodes per hidden layer
dropout_rate
Dropout rate to apply to each of the hidden layers
distribution
Distribution of the latent space, one of
* ``'normal'`` - Normal distribution
* ``'ln'`` - Logistic normal
use_batch_norm
Whether to use batch norm in layers
use_layer_norm
Whether to use layer norm
"""
def __init__(
self,
n_input: int,
n_output: int,
n_cat_list: Iterable[int] = None,
n_layers: int = 2,
n_hidden: int = 256,
dropout_rate: float = 0.1,
distribution: str = "ln",
use_batch_norm: bool = True,
use_layer_norm: bool = False,
):
super().__init__()
self.encoder = FCLayers(
n_in=n_input,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=n_layers,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
)
self.z_mean_encoder = nn.Linear(n_hidden, n_output)
self.z_var_encoder = nn.Linear(n_hidden, n_output)
self.l_gene_encoder = FCLayers(
n_in=n_input,
n_out=n_hidden,
n_cat_list=n_cat_list,
n_layers=1,
n_hidden=n_hidden,
dropout_rate=dropout_rate,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
)
self.l_gene_mean_encoder = nn.Linear(n_hidden, 1)
self.l_gene_var_encoder = nn.Linear(n_hidden, 1)
self.distribution = distribution
if distribution == "ln":
self.z_transformation = nn.Softmax(dim=-1)
else:
self.z_transformation = _identity
self.l_transformation = torch.exp
def reparameterize_transformation(self, mu, var):
"""Reparameterization trick to sample from a normal distribution."""
untran_z = Normal(mu, var.sqrt()).rsample()
z = self.z_transformation(untran_z)
return z, untran_z
def forward(self, data: torch.Tensor, *cat_list: int):
r"""The forward computation for a single sample.