campa.tl.BaseAEModel

class BaseAEModel(**kwargs)[source]

Base class for AE and VAE models.

This model can have neighbours, conditions (concatenated to input + decoder), and and adversarial head. The class defines initialisation functions for setting up AE with encoder and decoder. In addition, encoder and decoder input layers are defined (can be overwritten in subclassed functions).

Subclassed models can define:

Default architecture of this model:

  • Encoder: (noise) - conv layers - fc layers - linear layer to latent_dim

  • Decoder: fc_layers - linear (regularized) layer to num_output_channels

Conditional models additionally output latent, the latent space (for KL loss computation).

Adversarial models additionally output adv_head, the output of the adversarial head (for adversarial loss computation). (adv_latent - reverse_gradients - adversarial_layers - linear layer to num_conditions).

TODO adversarial models are not tested in this version of the code.

Parameters
  • name (Optional[str]) – Model name.

  • num_neighbors (str) – Number of neighbours used in input data.

  • num_channels (int) – Number of channels for input and output. Can be split up in num_input_channels and num_output_channels.

  • num_output_channels (Optional[int]) – Number of output channels

  • num_input_channels (Optional[int]) – Number of input channels

  • num_conditions (int) – Number of conditions. If > 0, the model is conditional and an additional input w/ conditions is assumed. Conditions are appended to the input and the latent representation. They are assumed to be 1d.

  • encode_condition (Optional[Union[int, Iterable[int]]) – If number or list, the condition is encoded using dense layers with this number of nodes.

  • condition_injection_layers (Iterable[int]) – Which layers of encoder and decoder to apply condition to. Give index of layer in encoder and decoder.

  • input_noise (Optional[str]) – One of gaussian, dropout, adds noise to encoder input.

  • noise_scale (int) – Scale of Gaussian noise.

  • encoder_conv_layers (Iterable[int]) – Size of convolutional layers for encoder.

  • encoder_conv_kernel_size (Iterable[int]) – Kernel size for each encoder convolutional layer.

  • encoder_fc_layers (Iterable[int]) – Size of fully connected encoder layers. From last encoder layer, a linear fully connected layer to latent_dim is applied.

  • latent_dim (int) – Number of nodes in latent space (for some models == number of classes).

  • decoder_fc_layers (Iterable[int]) – Decoder architecture. From last decoder later, a linear fully connected layer to num_output_channels is applied.

  • decoder_regularizer (Optional[str]) – Regularizer for decoder, l1 or l2.

  • decoder_regularizer_weight (float) – Weight of regularizer.

  • adversarial_layers – For adversarial models, add adversarial layers. Only works with categorical conditions.

Attributes

default_config

Default config used in every model.

is_adversarial

Model is adversarial if is is conditional and adversarial layers are defined.

is_conditional

Flag set based on num_conditions.

Methods

add_noise(X)

Add noise to X.

create_adversarial_head()

Create adversarial head: reverse_gradient - adversarial_layers - num_conditions.

create_decoder()

Create decoder.

create_encoder()

Create encoder.

create_model()

Create tf.keras.Model.

encode_condition(C)

Apply condition encoder to C.