deeprob.flows.models package

Submodules

deeprob.flows.models.base module

class deeprob.flows.models.base.NormalizingFlow(in_features, dequantize=False, logit=None, in_base=None)[source]

Bases: ProbabilisticModel

Initialize an abstract Normalizing Flow model.

Parameters
  • in_features – The input size.

  • dequantize (bool) – Whether to apply the dequantization transformation.

  • logit (Optional[float]) – The logit factor to use. Use None to disable the logit transformation.

  • in_base (Optional[Union[ProbabilisticModel, Distribution]]) – The input base distribution to use. If None, the standard Normal distribution is used.

Raises
  • ValueError – If the number of input features is invalid.

  • ValueError – If the logit value is invalid.

has_rsample = True
train(mode=True, base_mode=True)[source]

Set the training mode.

Parameters
  • mode (bool) – The training mode for the flows layers.

  • base_mode (bool) – The training mode for the in_base distribution.

Returns

Itself.

eval()[source]

Turn off the training mode for both the flows layers and the in_base distribution.

Returns

Itself.

preprocess(x)[source]

Preprocess the data batch before feeding it to the probabilistic model (forward mode).

Parameters

x (Tensor) – The input data batch.

Returns

The preprocessed data batch and the inv-log-det-jacobian.

Return type

Tuple[Tensor, Tensor]

unpreprocess(x)[source]

Preprocess the data batch before feeding it to the probabilistic model (backward mode).

Parameters

x (Tensor) – The input data batch.

Returns

The unpreprocessed data batch and the log-det-jacobian.

Return type

Tuple[Tensor, Tensor]

forward(x)[source]

Compute the log-likelihood given complete evidence.

Parameters

x (Tensor) – The inputs.

Returns

The log-likelihoods.

Return type

Tensor

sample(n_samples, y=None)[source]

Sample some values from the modeled distribution.

Parameters
  • n_samples (int) – The number of samples.

  • y (Optional[Tensor]) – The samples labels. It can be None.

Returns

The samples.

Return type

Tensor

rsample(n_samples, y=None)[source]

Sample some values from the modeled distribution by reparametrization. Unlike NormalizingFlow.sample(), this method allows backpropagation.

Parameters
  • n_samples (int) – The number of samples.

  • y (Optional[Tensor]) – The samples labels. It can be None.

Returns

The samples.

Return type

Tensor

apply_backward(x)[source]

Apply the backward transformation.

Parameters

x (Tensor) – The inputs.

Returns

The transformed samples and the backward log-det-jacobian.

Return type

Tuple[Tensor, Tensor]

training: bool
apply_forward(x)[source]

Apply the forward transformation.

Parameters

x (Tensor) – the inputs.

Returns

The transformed samples and the forward log-det-jacobian.

Return type

Tuple[Tensor, Tensor]

loss(x, y=None)[source]

Compute the loss of the model.

Parameters
  • x (Tensor) – The outputs of the model.

  • y (Optional[Tensor]) – The ground-truth. It can be None.

Returns

The loss.

Return type

Tensor

deeprob.flows.models.maf module

class deeprob.flows.models.maf.MAF(in_features, dequantize=False, logit=None, in_base=None, n_flows=5, depth=1, units=128, batch_norm=True, activation='relu', sequential=True, random_state=None)[source]

Bases: NormalizingFlow

Initialize a Masked Autorefressive Flow (MAF) model.

Parameters
  • in_features (int) – The number of input features.

  • dequantize (bool) – Whether to apply the dequantization transformation.

  • logit (Optional[float]) – The logit factor to use. Use None to disable the logit transformation.

  • in_base (Optional[Union[ProbabilisticModel, Distribution]]) – The input base distribution to use. If None, the standard Normal distribution is used.

  • n_flows (int) – The number of sequential autoregressive layers.

  • depth (int) – The number of hidden layers of flows conditioners.

  • units (int) – The number of hidden units per layer of flows conditioners.

  • batch_norm (bool) – Whether to apply batch normalization after each autoregressive layer.

  • activation (str) – The activation function name to use for the flows conditioners hidden layers.

  • sequential (bool) – If True build masks degrees sequentially, otherwise randomly.

  • random_state (Optional[Union[int, RandomState]]) – The random state used to generate the masks degrees. Used only if sequential is False. It can be either a seed integer or a np.random.RandomState instance.

Raises

ValueError – If a parameter is out of domain.

training: bool

deeprob.flows.models.realnvp module

class deeprob.flows.models.realnvp.RealNVP1d(in_features, dequantize=False, logit=None, in_base=None, n_flows=5, depth=1, units=128, batch_norm=True, affine=True)[source]

Bases: NormalizingFlow

Real Non-Volume-Preserving (RealNVP) 1D normalizing flow model.

Parameters
  • in_features (int) – The number of input features.

  • dequantize (bool) – Whether to apply the dequantization transformation.

  • logit (Optional[float]) – The logit factor to use. Use None to disable the logit transformation.

  • in_base (Optional[Union[ProbabilisticModel, Distribution]]) – The input base distribution to use. If None, the standard Normal distribution is used.

  • n_flows (int) – The number of sequential coupling flows.

  • depth (int) – The number of hidden layers of flows conditioners.

  • units (int) – The number of hidden units per layer of flows conditioners.

  • batch_norm (bool) – Whether to apply batch normalization after each coupling layer.

  • affine (bool) – Whether to use affine transformation. If False then use only translation (as in NICE).

Raises

ValueError – If a parameter is out of scope.

training: bool
class deeprob.flows.models.realnvp.RealNVP2d(in_features, dequantize=False, logit=None, in_base=None, network='resnet', n_flows=1, n_blocks=2, channels=32, affine=True)[source]

Bases: NormalizingFlow

Real Non-Volume-Preserving (RealNVP) 2D normalizing flow model.

Parameters
  • in_features (Tuple[int, int, int]) – The input size as a (C, H, W) tuple.

  • dequantize (bool) – Whether to apply the dequantization transformation.

  • logit (Optional[float]) – The logit factor to use. Use None to disable the logit transformation.

  • in_base (Optional[Union[ProbabilisticModel, Distribution]]) – The input base distribution to use. If None, the standard Normal distribution is used.

  • network (str) – The neural network conditioner to use. It can be either ‘resnet’ or ‘densenet’.

  • n_flows (int) – The number of sequential multi-scale architectures.

  • n_blocks (int) – The number of residual blocks or dense blocks.

  • channels (int) – The number of output channels of each convolutional layer.

  • affine (bool) – Whether to use affine transformation. If False then use only translation (as in NICE).

static build_permutation_matrix(channels)[source]

Build the permutation matrix that defines (a non-trivial) variables ordering when downscaling or upscaling as in RealNVP.

Parameters

channels (int) – The number of input channels.

Returns

The permutation matrix tensor.

Return type

Tensor

apply_backward(x)[source]

Apply the backward transformation.

Parameters

x (Tensor) – The inputs.

Returns

The transformed samples and the backward log-det-jacobian.

Return type

Tuple[Tensor, Tensor]

apply_forward(x)[source]

Apply the forward transformation.

Parameters

x (Tensor) – the inputs.

Returns

The transformed samples and the forward log-det-jacobian.

Return type

Tuple[Tensor, Tensor]

training: bool

Module contents