scimba_torch.neural_nets.structure_preserving_nets.invertible_nn

An invertible neural network made of RealNVP layers.

Classes

InvertibleLayer(in_size, out_size, **kwargs)

An abstract class for an invertible layer.

InvertibleNet(dim, p_dim[, nb_layers, ...])

An invertible neural network made of RealNVP layers.

RealNVPFlowLayer(dim, p_dim[, net_type])

Conservative volumes flow where the type of neural network is given by net.

class InvertibleLayer(in_size, out_size, **kwargs)[source]

Bases: ScimbaModule, ABC

An abstract class for an invertible layer.

abstract backward(inputs, with_last_layer=True)[source]

Abstract method for the backward pass of the invertible layer.

Parameters:
  • inputs (Tensor) – the input tensor

  • with_last_layer (bool) – whether to use the last layer of the network or not (default: True)

class RealNVPFlowLayer(dim, p_dim, net_type=<class 'scimba_torch.neural_nets.coordinates_based_nets.mlp.GenericMLP'>, **kwargs)[source]

Bases: InvertibleLayer

Conservative volumes flow where the type of neural network is given by net.

It is to approximate probability \(p(y\mid x)\).

Flow:

\[\begin{split}z[k:d] &= y[k:d] \exp^{s(y[1:k],x)} + t(y[1:k],x) \\ z[1:k] &= y[1:k]\end{split}\]

with \(s\) the scale and \(t\) the shift/translation term.

Parameters:
  • dim (int) – the dimension of the input x of the flow

  • p_dim (int) – the dimension of the conditional input y of the flow

  • net_type (Module) – the type of neural network used

  • **kwargs

    other arguments for the neural network:

    • parity: parity of the layer

    • scale: to indicate whether we scale or not

    • shift: to indicate whether we shift or not

dim: int

dimension of the input x of the flow

dim_p: int

dimension of the conditional input y of the flow

net_type: nn.Module

type of neural network used

parity: bool

parity of the layer

scale: bool

scale the layer

shift: bool

shift the layer

forward(y, p, with_last_layer=True)[source]

Compute the flow.

Parameters:
  • y (Tensor) – the tensor of the data y

  • p (Tensor) – the tensor of the conditional data x

  • with_last_layer (bool) – whether to use the last layer of the network or not (default: True)

Return type:

Tensor

Returns:

the tensor containing the result

backward(z, p, with_last_layer=True)[source]

Compute the backward flow.

Parameters:
  • z (Tensor) – the tensor of the data z

  • p (Tensor) – the tensor of the conditional data x

  • with_last_layer (bool) – whether to use the last layer of the network or not (default: True)

Return type:

Tensor

Returns:

the tensor containing the result

class InvertibleNet(dim, p_dim, nb_layers=2, layer_type=<class 'scimba_torch.neural_nets.structure_preserving_nets.invertible_nn.RealNVPFlowLayer'>, net_type=<class 'scimba_torch.neural_nets.coordinates_based_nets.mlp.GenericMLP'>, **kwargs)[source]

Bases: ScimbaModule

An invertible neural network made of RealNVP layers.

Parameters:
  • dim (int) – dimension of the input data

  • p_dim (int) – dimension of the conditional input data

  • nb_layers (int) – number of invertible layers

  • layer_type (InvertibleLayer) – type of invertible layer (default: RealNVPFlowLayer)

  • net_type (Module) – type of neural network used in each layer (default: GenericMLP)

  • **kwargs – other arguments for the invertible layers.

forward(inputs, with_last_layer=True)[source]

Applies the forward pass of the invertible network.

Parameters:
  • inputs (Tensor) – the input tensor of shape (batch_size, dim + p_dim).

  • with_last_layer (bool) – whether to use the last layer of the network or not (default: True)

Return type:

Tensor

Returns:

The output tensor of shape (batch_size, dim + p_dim) after applying all layers.

backward(inputs, with_last_layer=True)[source]

Applies the backward pass of the invertible network.

Parameters:
  • inputs (Tensor) – the input tensor of shape (batch_size, dim + p_dim).

  • with_last_layer (bool) – whether to use the last layer of the network or not (default: True)

Return type:

Tensor

Returns:

the output tensor of shape (batch_size, dim + p_dim) after applying

all layers in reverse order.