alibi.models.pytorch.cfrl_models module

This module contains the Pytorch implementation of models used for the Counterfactual with Reinforcement Learning experiments for both data modalities (image and tabular).

class alibi.models.pytorch.cfrl_models.ADULTDecoder(hidden_dim, output_dims)[source]

Bases: Module

ADULT decoder used in the Counterfactual with Reinforcement Learning experiments. The model consists of of a fully connected layer with ReLU nonlinearity, and a multiheaded layer, one for each categorical feature and a single head for the rest of numerical features. The hidden dimension used in the paper is 128.

__init__(hidden_dim, output_dims)[source]

Constructor.

Parameters:
  • hidden_dim (int) – Hidden dimension.

  • output_dims (List[int]) – List of output dimensions.

forward(x)[source]

Forward pass.

Parameters:

x (Tensor) – Input tensor.

Return type:

List[Tensor]

Returns:

List of reconstruction of the input tensor. First element corresponds to the reconstruction of all the numerical features if they exist, and the rest of the elements correspond to each categorical feature.

class alibi.models.pytorch.cfrl_models.ADULTEncoder(hidden_dim, latent_dim)[source]

Bases: Module

ADULT encoder used in the Counterfactual with Reinforcement Learning experiments. The model consists of two fully connected layers with ReLU and tanh nonlinearities. The tanh nonlinearity clips the embedding in [-1, 1] as required in the DDPG algorithm (e.g., [act_low, act_high]). The layers’ dimensions used in the paper are 128 and 15, although those can vary as they were selected to generalize across many datasets.

__init__(hidden_dim, latent_dim)[source]

Constructor.

Parameters:
  • hidden_dim (int) – Hidden dimension.

  • latent_dim (int) – Latent dimension.

forward(x)[source]

Forward pass.

Parameters:

x (Tensor) – Input tensor.

Return type:

Tensor

Returns:

Encoding representation having each component in the interval [-1, 1]

class alibi.models.pytorch.cfrl_models.MNISTClassifier(output_dim)[source]

Bases: Model

MNIST classifier used in the experiments for Counterfactual with Reinforcement Learning. The model consists of two convolutional layers having 64 and 32 channels and a kernel size of 2 with ReLU nonlinearities, followed by maxpooling of size 2 and dropout of 0.3. The convolutional block is followed by a fully connected layer of 256 with ReLU nonlinearity, and finally a fully connected layer is used to predict the class logits (10 in MNIST case).

__init__(output_dim)[source]

Constructor.

Parameters:

output_dim (int) – Output dimension.

forward(x)[source]

Forward pass.

Parameters:

x (Tensor) – Input tensor.

Return type:

Tensor

Returns:

Classification logits.

class alibi.models.pytorch.cfrl_models.MNISTDecoder(latent_dim)[source]

Bases: Module

MNIST decoder used in the Counterfactual with Reinforcement Learning experiments. The model consists of a fully connected layer of 128 units with ReLU activation followed by a convolutional block. The convolutional block consists fo 4 convolutional layers having 8, 8, 8 and 1 channels and a kernel size of 3. Each convolutional layer, except the last one, has ReLU nonlinearities and is followed by an upsampling layer of size 2. The final layers uses a sigmoid activation to clip the output values in [0, 1].

__init__(latent_dim)[source]

Constructor.

Parameters:

latent_dim (int) – Latent dimension.

forward(x)[source]

Forward pass.

Parameters:

x (Tensor) – Input tensor.

Return type:

Tensor

Returns:

Decoded input having each component in the interval [0, 1].

class alibi.models.pytorch.cfrl_models.MNISTEncoder(latent_dim)[source]

Bases: Module

MNIST encoder used in the experiments for the Counterfactual with Reinforcement Learning. The model consists of 3 convolutional layers having 16, 8 and 8 channels and a kernel size of 3, with ReLU nonlinearities. Each convolutional layer is followed by a maxpooling layer of size 2. Finally, a fully connected layer follows the convolutional block with a tanh nonlinearity. The tanh clips the output between [-1, 1], required in the DDPG algorithm (e.g., [act_low, act_high]). The embedding dimension used in the paper is 32, although this can vary.

__init__(latent_dim)[source]

Constructor.

Parameters:

latent_dim (int) – Latent dimension.

forward(x)[source]

Forward pass.

Parameters:

x (Tensor) – Input tensor.

Return type:

Tensor

Returns:

Encoding representation having each component in the interval [-1, 1]