Stochastic Layers#
Sampling layer of VAE.
A VAE creates its FullyFactorizedGaussian as vae.sampling_layer.
To use the class directly, import it from this module; it is not exported by
pyautoencoder.variational:
from pyautoencoder.variational.stochastic_layers import FullyFactorizedGaussian
- class pyautoencoder.variational.stochastic_layers.FullyFactorizedGaussian(latent_dim: int)#
Bases:
ModuleGaussian posterior head producing a fully factorized \(q(z \mid x)\).
Given input features
xof shape[B, F], this module produces the parameters of a diagonal Gaussian posterior,\[q(z \mid x) = \mathcal{N}(z \mid \mu(x), \operatorname{diag}(\sigma(x)^2)),\]and either
Slatent samples drawn via the reparameterization trick (sample=True, the default) or the posterior mean (sample=False). The choice is explicit and does not depend on train/eval mode.The linear layers
muandlog_vararetorch.nn.LazyLinear: their input sizeFis inferred on the first forward pass, for example the one run byVAE.build.- forward(x: Tensor, S: int = 1, sample: bool = True) tuple[Tensor, Tensor, Tensor]#
Compute the posterior parameters and the latent codes
z.With
sample=True(default),zholdsSMonte Carlo samples drawn with the reparameterization trick (seereparametrize()):\[z^{(s)} = \mu + \sigma \odot \epsilon^{(s)}, \qquad \epsilon^{(s)} \sim \mathcal{N}(0, I).\]With
sample=False,zis the posterior meanmu, with a single sample dimension (Smust be1).The behavior does not depend on train/eval mode.
- Parameters:
x (torch.Tensor) – Input tensor of shape
[B, F].S (int, optional) – Number of Monte Carlo samples to generate. Must be
>= 1, and1whensample=False. Defaults to1.sample (bool, optional) – Whether to sample
zfrom \(q(z \mid x)\) (True) or to return its mean (False). Defaults toTrue.
- Returns:
(z, mu, log_var), where:z– latent codes, shape[B, S, latent_dim]([B, 1, latent_dim]whensample=False).mu– mean of \(q(z \mid x)\), shape[B, latent_dim].log_var– log-variance of \(q(z \mid x)\), shape[B, latent_dim].
- Return type:
tuple[torch.Tensor, torch.Tensor, torch.Tensor]
- Raises:
ValueError – If
S < 1, ifS != 1withsample=False, or ifxis not of shape[B, F].
- get_params(x: Tensor) tuple[Tensor, Tensor]#
Compute posterior parameters without drawing samples.
- Parameters:
x (torch.Tensor) – Input tensor of shape
[B, F].- Returns:
(mu, log_var), each of shape[B, latent_dim].- Return type:
tuple[torch.Tensor, torch.Tensor]
- Raises:
ValueError – If
xis not of shape[B, F].
- reparametrize(mu: Tensor, log_var: Tensor, S: int = 1) Tensor#
Draw
Slatent samples via the reparameterization trick.\[z^{(s)} = \mu + \sigma \odot \epsilon^{(s)}, \qquad \epsilon^{(s)} \sim \mathcal{N}(0, I).\]This method always samples. It is differentiable with respect to
muandlog_var, so it can be used on posterior parameters computed elsewhere (for example the adapted posteriors ofAdaGVAE).- Parameters:
mu (torch.Tensor) – Mean of the posterior, shape
[B, D_z].log_var (torch.Tensor) – Log-variance of the posterior, shape
[B, D_z].S (int, optional) – Number of samples to draw. Must be
>= 1. Defaults to1.
- Returns:
Sampled latent codes of shape
[B, S, D_z].- Return type:
torch.Tensor
- Raises:
ValueError – If
S < 1, or ifmuandlog_varare not both of shape[B, D_z].