deeprob.flows.layers package

Submodules

deeprob.flows.layers.autoregressive module

class deeprob.flows.layers.autoregressive.AutoregressiveLayer(in_features, depth, units, activation, reverse=False, sequential=True, random_state=None)[source]

Bases: Bijector

Build an autoregressive layer as specified in Masked Autoregressive Flow paper.

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

  • depth (int) – The number of hidden layers of the conditioner.

  • units (int) – The number of units of each hidden layer of the conditioner.

  • activation (str) – The activation used for inner layers of the conditioner.

  • reverse (bool) – Whether to reverse the mask used in the autoregressive layer. Used only if sequential is True.

  • sequential (bool) – Whether to use sequential degrees for inner layers masks.

  • random_state (Optional[RandomState]) – The random state used to generate the masks degrees. Used only if sequential is False.

Raises

ValueError – If a parameter is out of domain.

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(u)[source]

Apply the forward transformation.

Parameters

u (Tensor) – The inputs.

Returns

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

Return type

Tuple[Tensor, Tensor]

build_degrees_sequential(depth, units, reverse)[source]

Build sequential degrees for the linear layers of the autoregressive network.

Parameters
  • depth (int) – The number of hidden layers of the conditioner.

  • units (int) – The number of units of each hidden layer of the conditioner.

  • reverse (bool) – Whether to reverse the mask used in the autoregressive layer. Used only if sequential is True.

Returns

The masks to use for each hidden layer of the autoregressive network.

Return type

List[ndarray]

build_degrees_random(depth, units, random_state)[source]

Create random degrees for the linear layers of the autoregressive network.

Parameters
  • depth (int) – The number of hidden layers of the conditioner.

  • units (int) – The number of units of each hidden layer of the conditioner.

  • random_state (RandomState) – The random state.

Returns

The masks to use for each hidden layer of the autoregressive network.

Return type

List[ndarray]

static build_masks(degrees)[source]

Build masks from degrees.

Parameters

degrees (List[ndarray]) – A sequence of units degrees for each hidden layer.

Returns

The masks to use for each hidden layer of the autoregressive network.

Return type

List[ndarray]

training: bool

deeprob.flows.layers.coupling module

class deeprob.flows.layers.coupling.CouplingLayer1d(in_features, depth, units, affine=True, reverse=False)[source]

Bases: Bijector

Build a RealNVP (or NICE) 1D coupling layer.

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

  • depth (int) – The number of hidden layers of the conditioner.

  • units (int) – The number of units of each hidden layer of the conditioner.

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

  • reverse (bool) – Whether to reverse the mask used in the coupling layer. Useful for alternating masks.

build_alternating_masks()[source]

Build the alterning masks.

Returns

The alterning mask and its inverse.

Return type

Tuple[ndarray, ndarray]

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(u)[source]

Apply the forward transformation.

Parameters

u (Tensor) – The inputs.

Returns

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

Return type

Tuple[Tensor, Tensor]

training: bool
class deeprob.flows.layers.coupling.CouplingLayer2d(in_features, network, n_blocks, channels, affine=True, channelwise=False, reverse=False)[source]

Bases: Bijector

Build a RealNVP (or NICE) 2D coupling layer.

Parameters
  • in_features (Tuple[int, int, int]) – The size of the input.

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

  • 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).

  • channelwise (bool) – Whether to use channel-wise coupling mask. Defaults to False, i.e. checkerboard coupling mask.

  • reverse (bool) – Whether to reverse the mask used in the coupling layer. Useful for alternating masks.

property in_channels: int
property in_height: int
property in_width: int
build_checkerboard_masks()[source]

Build the checkerboard coupling masks.

Returns

The checkerboard mask and its inverse.

Return type

Tuple[ndarray, ndarray]

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(u)[source]

Apply the forward transformation.

Parameters

u (Tensor) – The inputs.

Returns

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

Return type

Tuple[Tensor, Tensor]

training: bool
class deeprob.flows.layers.coupling.CouplingBlock2d(in_features, network, n_blocks, channels, affine=True, last_block=False)[source]

Bases: Bijector

Build a RealNVP (or NICE) 2D coupling block, consisting of check-board/channel-wise couplings and squeeze operation.

Parameters
  • in_features (Tuple[int, int, int]) – The size of the input.

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

  • 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).

  • last_block (bool) – Whether it is the last block (i.e. no channelwise-masked couplings) or not.

property in_channels: int
property in_height: int
property in_width: int
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(u)[source]

Apply the forward transformation.

Parameters

u (Tensor) – The inputs.

Returns

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

Return type

Tuple[Tensor, Tensor]

training: bool

deeprob.flows.layers.densenet module

class deeprob.flows.layers.densenet.DenseLayer(in_channels, out_channels, use_checkpoint=False)[source]

Bases: Module

Initialize a dense layer as in DenseNet.

Parameters
  • in_channels (int) – The number of input channels.

  • out_channels (int) – The number of output channels.

  • use_checkpoint (bool) – Whether to use a checkpoint in order to reduce memory usage (by increasing training time caused by re-computations).

bottleneck(inputs)[source]

Pass through the bottleneck layer.

Parameters

inputs (List[Tensor]) – A list of previous feature maps.

Returns

The outputs of the bottleneck.

Return type

Tensor

checkpoint_bottleneck(inputs)[source]

Pass through the bottleneck layer (by using a checkpoint).

Parameters

inputs (List[Tensor]) – A list of previous feature maps.

Returns

The outputs of the bottleneck.

Return type

Tensor

forward(inputs)[source]

Evaluate the dense layer.

Parameters

inputs (List[Tensor]) – A list of previous feature maps.

Returns

The outputs of the layer.

Return type

Tensor

training: bool
class deeprob.flows.layers.densenet.DenseBlock(n_layers, in_channels, out_channels, use_checkpoint=False)[source]

Bases: Module

Initialize a dense block as in DenseNet.

Parameters
  • n_layers (int) – The number of dense layers.

  • in_channels (int) – The number of input channels.

  • out_channels (int) – The number of output channels.

  • use_checkpoint (bool) – Whether to use a checkpoint in order to reduce memory usage (by increasing training time caused by re-computations).

forward(x)[source]

Evaluate the dense block.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

training: bool
class deeprob.flows.layers.densenet.Transition(in_channels, out_channels, bias=True)[source]

Bases: Module

Initialize a transition layer as in DenseNet.

Parameters
  • in_channels (int) – The number of input channels.

  • out_channels (int) – The number of output channels.

  • bias (bool) – Whether to use bias in the last convolutional layer.

forward(x)[source]

Evaluate the layer.

Parameters

x – The inputs.

Returns

The outputs of the layer.

training: bool
class deeprob.flows.layers.densenet.DenseNetwork(in_channels, mid_channels, out_channels, n_blocks, use_checkpoint=False)[source]

Bases: Module

Initialize a dense network (DenseNet) with only one dense block.

Parameters
  • in_channels (int) – The number of input channels.

  • mid_channels (int) – The number of mid channels.

  • out_channels (int) – The number of output channels.

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

  • use_checkpoint (bool) – Whether to use a checkpoint in order to reduce memory usage (by increasing training time caused by re-computations).

forward(x)[source]

Evaluate the dense network.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

training: bool

deeprob.flows.layers.resnet module

class deeprob.flows.layers.resnet.ResidualBlock(n_channels)[source]

Bases: Module

Build a basic residual block as in ResNet.

Parameters

n_channels (int) – The number of channels.

forward(x)[source]

Evaluate the residual block.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

training: bool
class deeprob.flows.layers.resnet.ResidualNetwork(in_channels, mid_channels, out_channels, n_blocks)[source]

Bases: Module

Initialize a residual network (ResNet) with skip connections.

Parameters
  • in_channels (int) – The number of input channels.

  • mid_channels (int) – The number of mid channels.

  • out_channels (int) – The number of output channels.

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

Raises

ValueError – If a parameter is out of domain.

forward(x)[source]

Evaluate the residual network.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

training: bool

Module contents