-
Notifications
You must be signed in to change notification settings - Fork 342
/
_vaemixin.py
207 lines (186 loc) · 7.72 KB
/
_vaemixin.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
import logging
from collections.abc import Sequence
from typing import Optional, Union
import numpy as np
import torch
from anndata import AnnData
from scvi.utils import unsupported_if_adata_minified
from ._log_likelihood import compute_elbo, compute_reconstruction_error
logger = logging.getLogger(__name__)
class VAEMixin:
"""Univseral VAE methods."""
@torch.inference_mode()
@unsupported_if_adata_minified
def get_elbo(
self,
adata: Optional[AnnData] = None,
indices: Optional[Sequence[int]] = None,
batch_size: Optional[int] = None,
) -> float:
"""Return the ELBO for the data.
The ELBO is a lower bound on the log likelihood of the data used for optimization
of VAEs. Note, this is not the negative ELBO, higher is better.
Parameters
----------
adata
AnnData object with equivalent structure to initial AnnData. If `None`, defaults to the
AnnData object used to initialize the model.
indices
Indices of cells in adata to use. If `None`, all cells are used.
batch_size
Minibatch size for data loading into model. Defaults to `scvi.settings.batch_size`.
"""
adata = self._validate_anndata(adata)
scdl = self._make_data_loader(adata=adata, indices=indices, batch_size=batch_size)
elbo = compute_elbo(self.module, scdl)
return -elbo
@torch.inference_mode()
@unsupported_if_adata_minified
def get_marginal_ll(
self,
adata: Optional[AnnData] = None,
indices: Optional[Sequence[int]] = None,
n_mc_samples: int = 1000,
batch_size: Optional[int] = None,
return_mean: Optional[bool] = True,
**kwargs,
) -> Union[torch.Tensor, float]:
"""Return the marginal LL for the data.
The computation here is a biased estimator of the marginal log likelihood of the data.
Note, this is not the negative log likelihood, higher is better.
Parameters
----------
adata
AnnData object with equivalent structure to initial AnnData. If `None`, defaults to the
AnnData object used to initialize the model.
indices
Indices of cells in adata to use. If `None`, all cells are used.
n_mc_samples
Number of Monte Carlo samples to use for marginal LL estimation.
batch_size
Minibatch size for data loading into model. Defaults to `scvi.settings.batch_size`.
return_mean
If False, return the marginal log likelihood for each observation.
Otherwise, return the mmean arginal log likelihood.
"""
adata = self._validate_anndata(adata)
if indices is None:
indices = np.arange(adata.n_obs)
scdl = self._make_data_loader(
adata=adata,
indices=indices,
batch_size=batch_size,
shuffle=False,
)
if hasattr(self.module, "marginal_ll"):
log_lkl = []
for tensors in scdl:
log_lkl.append(
self.module.marginal_ll(
tensors,
n_mc_samples=n_mc_samples,
return_mean=return_mean,
**kwargs,
)
)
if not return_mean:
return torch.cat(log_lkl, 0)
else:
return np.mean(log_lkl)
else:
raise NotImplementedError(
"marginal_ll is not implemented for current model. "
"Please raise an issue on github if you need it."
)
@torch.inference_mode()
@unsupported_if_adata_minified
def get_reconstruction_error(
self,
adata: Optional[AnnData] = None,
indices: Optional[Sequence[int]] = None,
batch_size: Optional[int] = None,
) -> float:
r"""Return the reconstruction error for the data.
This is typically written as :math:`p(x \mid z)`, the likelihood term given one posterior sample.
Note, this is not the negative likelihood, higher is better.
Parameters
----------
adata
AnnData object with equivalent structure to initial AnnData. If `None`, defaults to the
AnnData object used to initialize the model.
indices
Indices of cells in adata to use. If `None`, all cells are used.
batch_size
Minibatch size for data loading into model. Defaults to `scvi.settings.batch_size`.
"""
adata = self._validate_anndata(adata)
scdl = self._make_data_loader(adata=adata, indices=indices, batch_size=batch_size)
reconstruction_error = compute_reconstruction_error(self.module, scdl)
return reconstruction_error
@torch.inference_mode()
def get_latent_representation(
self,
adata: Optional[AnnData] = None,
indices: Optional[Sequence[int]] = None,
give_mean: bool = True,
mc_samples: int = 5000,
batch_size: Optional[int] = None,
return_dist: bool = False,
) -> Union[np.ndarray, tuple[np.ndarray, np.ndarray]]:
"""Return the latent representation for each cell.
This is typically denoted as :math:`z_n`.
Parameters
----------
adata
AnnData object with equivalent structure to initial AnnData. If `None`, defaults to the
AnnData object used to initialize the model.
indices
Indices of cells in adata to use. If `None`, all cells are used.
give_mean
Give mean of distribution or sample from it.
mc_samples
For distributions with no closed-form mean (e.g., `logistic normal`), how many Monte Carlo
samples to take for computing mean.
batch_size
Minibatch size for data loading into model. Defaults to `scvi.settings.batch_size`.
return_dist
Return (mean, variance) of distributions instead of just the mean.
If `True`, ignores `give_mean` and `mc_samples`. In the case of the latter,
`mc_samples` is used to compute the mean of a transformed distribution.
If `return_dist` is true the untransformed mean and variance are returned.
Returns
-------
Low-dimensional representation for each cell or a tuple containing its mean and variance.
"""
self._check_if_trained(warn=False)
adata = self._validate_anndata(adata)
scdl = self._make_data_loader(adata=adata, indices=indices, batch_size=batch_size)
latent = []
latent_qzm = []
latent_qzv = []
for tensors in scdl:
inference_inputs = self.module._get_inference_input(tensors)
outputs = self.module.inference(**inference_inputs)
if "qz" in outputs:
qz = outputs["qz"]
else:
qz_m, qz_v = outputs["qz_m"], outputs["qz_v"]
qz = torch.distributions.Normal(qz_m, qz_v.sqrt())
if give_mean:
# does each model need to have this latent distribution param?
if self.module.latent_distribution == "ln":
samples = qz.sample([mc_samples])
z = torch.nn.functional.softmax(samples, dim=-1)
z = z.mean(dim=0)
else:
z = qz.loc
else:
z = outputs["z"]
latent += [z.cpu()]
latent_qzm += [qz.loc.cpu()]
latent_qzv += [qz.scale.square().cpu()]
return (
(torch.cat(latent_qzm).numpy(), torch.cat(latent_qzv).numpy())
if return_dist
else torch.cat(latent).numpy()
)