Flow#

General flow object#

class pocomc.flow.Flow(n_dim, flow=None)#

Normalizing flow model.

Parameters:
  • n_dim (int) – Number of dimensions of the distribution to be modeled.

  • flow (zuko.flows.Flow) – Normalizing flow model. Default: zuko.flows.MAF.

n_dim#

Number of dimensions of the distribution to be modeled.

Type:

int

flow#

Normalizing flow model.

Type:

zuko.flows.Flow

transform#

Transformation object.

Type:

zuko.transforms.Transform

Examples

>>> import torch
>>> import pocomc
>>> flow = pocomc.Flow(2)
>>> x = torch.randn(100, 2)
>>> u, logdetj = flow(x)
>>> x_, logdetj_ = flow.inverse(u)
>>> log_prob = flow.log_prob(x)
>>> x_, log_prob_ = flow.sample(100)
>>> history = flow.fit(x)
fit(x, weights=None, validation_split=0.0, epochs=1000, batch_size=1000, patience=20, learning_rate=0.001, weight_decay=0, laplace_scale=None, gaussian_scale=None, annealing=True, noise=None, shuffle=True, clip_grad_norm=1.0, verbose=0)#
Parameters:
  • x (torch.Tensor) – Input samples.

  • weights (torch.Tensor, optional) – Weights for each sample. Default: None.

  • validation_split (float, optional) – Fraction of samples to use for validation. Default: 0.0.

  • epochs (int, optional) – Number of epochs. Default: 1000.

  • batch_size (int, optional) – Batch size. Default: 1000.

  • patience (int, optional) – Number of epochs without improvement before early stopping. Default: 20.

  • learning_rate (float, optional) – Learning rate. Default: 1e-3.

  • weight_decay (float, optional) – Weight decay. Default: 0.

  • laplace_scale (float, optional) – Laplace regularization scale. Default: None.

  • gaussian_scale (float, optional) – Gaussian regularization scale. Default: None.

  • annealing (bool, optional) – Whether to use learning rate annealing. Default: True.

  • noise (float, optional) – Noise scale. Default: None.

  • shuffle (bool, optional) – Whether to shuffle samples. Default: True.

  • clip_grad_norm (float, optional) – Maximum gradient norm. Default: 1.0.

  • verbose (int, optional) – Verbosity level. Default: 0.

Returns:

history – Dictionary with loss history.

Return type:

dict

Examples

>>> import torch
>>> import pocomc
>>> flow = pocomc.Flow(2)
>>> x = torch.randn(100, 2)
>>> history = flow.fit(x)
forward(x: Tensor) Tuple[Tensor, Tensor]#

Forward transformation. Inputs are transformed from the original (relating to the distribution to be modeled) to the latent space.

Parameters:

x (torch.Tensor) – Samples to transform.

Returns:

u – Transformed samples in latent space with the same shape as the original space inputs.

Return type:

torch.Tensor

inverse(u: Tensor) Tuple[Tensor, Tensor]#

Inverse transformation. Inputs are transformed from the latent to the original space (relating to the distribution to be modeled).

Parameters:

u (torch.Tensor) – Samples to transform.

Returns:

x – Transformed samples in the original space with the same shape as the latent space inputs.

Return type:

torch.Tensor

log_prob(x: Tensor) Tensor#

Compute log probability of samples.

Parameters:

x (torch.Tensor) – Input samples

Return type:

Log-probability of samples.

sample(size: int = 1) Tuple[Tensor, Tensor]#

Draw random samples from the normalizing flow.

Parameters:

size (int) – Number of samples to generate. Default: 1.

Returns:

samples, log_prob – Samples as a torch.Tensor with shape (size, n_dimensions) and log probability values with shape (size, ).

Return type:

tuple

property transform#

Transformation object.