Stochastic Layers#
- 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 (optionally) samples
Slatent draws via the reparameterization trick during training.The build step infers
Fand lazily constructs the linear layersmuandlog_var.- forward(x: Tensor, S: int = 1) tuple[Tensor, Tensor, Tensor]#
Compute parameters and (optionally) samples from the Gaussian posterior.
During training, this method returns
SMonte Carlo samples using the reparameterization trick:\[z^{(s)} = \mu + \sigma \odot \epsilon^{(s)}, \qquad \epsilon^{(s)} \sim \mathcal{N}(0, I).\]During evaluation (
model.eval()), deterministic output is returned withzequal to the repeated mean.- Parameters:
x (torch.Tensor) – Input tensor of shape
[B, F].S (int, optional) – Number of Monte Carlo samples to generate. Must be
>= 1. Defaults to1(single sample).
- Returns:
(z, mu, log_var), where:z– sampled or repeated latent codes, shape[B, S, latent_dim].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.
- 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]