BaseAE

Abstract class

This is the base AuteEncoder architecture module from which all future autoencoder based models should inherit.

It contains:

  • a BaseAEConfig instance containing the main model’s parameters (e.g. latent dimension …)
  • a BaseAE instance which creates a BaseAE model having a basic autoencoding architecture
  • The ModelOutput instance used for neural nets outputs and model outputs of the forward method).
class pythae.models.BaseAEConfig(input_dim=None, latent_dim=10, uses_default_encoder=True, uses_default_decoder=True)[source]

This is the base configuration instance of the models deriving from BaseConfig.

Parameters
  • input_dim (tuple) – The input_data dimension (channels X x_dim X y_dim)

  • latent_dim (int) – The latent space dimension. Default: None.

class pythae.models.BaseAE(model_config, encoder=None, decoder=None)[source]

Base class for Autoencoder based models.

Parameters
  • model_config (BaseAEConfig) – An instance of BaseAEConfig in which any model’s parameters is made available.

  • encoder (BaseEncoder) – An instance of BaseEncoder (inheriting from torch.nn.Module which plays the role of encoder. This argument allows you to use your own neural networks architectures if desired. If None is provided, a simple Multi Layer Preception (https://en.wikipedia.org/wiki/Multilayer_perceptron) is used. Default: None.

  • decoder (BaseDecoder) – An instance of BaseDecoder (inheriting from torch.nn.Module which plays the role of decoder. This argument allows you to use your own neural networks architectures if desired. If None is provided, a simple Multi Layer Preception (https://en.wikipedia.org/wiki/Multilayer_perceptron) is used. Default: None.

Note

For high dimensional data we advice you to provide you own network architectures. With the provided MLP you may end up with a MemoryError.

embed(inputs)[source]

Return the embeddings of the input data.

Parameters

inputs (torch.Tensor) – The input data to be embedded, of shape [B x input_dim].

Returns

A tensor of shape [B x latent_dim] containing the embeddings.

Return type

torch.Tensor

forward(inputs, **kwargs)[source]

Main forward pass outputing the VAE outputs This function should output a ModelOutput instance gathering all the model outputs

Parameters

inputs (BaseDataset) – The training data with labels, masks etc…

Returns

A ModelOutput instance providing the outputs of the model.

Return type

ModelOutput

Note

The loss must be computed in this forward pass and accessed through loss = model_output.loss

interpolate(starting_inputs, ending_inputs, granularity=10)[source]

This function performs a linear interpolation in the latent space of the autoencoder from starting inputs to ending inputs. It returns the interpolation trajectories.

Parameters
  • starting_inputs (torch.Tensor) – The starting inputs in the interpolation of shape [B x input_dim]

  • ending_inputs (torch.Tensor) – The starting inputs in the interpolation of shape [B x input_dim]

  • granularity (int) – The granularity of the interpolation.

Returns

A tensor of shape [B x granularity x input_dim] containing the interpolation trajectories.

Return type

torch.Tensor

classmethod load_from_folder(dir_path)[source]

Class method to be used to load the model from a specific folder

Parameters

dir_path (str) – The path where the model should have been be saved.

Note

This function requires the folder to contain:

  • a model_config.json and a model.pt if no custom architectures were provided

or

  • a model_config.json, a model.pt and a encoder.pkl (resp. decoder.pkl) if a custom encoder (resp. decoder) was provided
classmethod load_from_hf_hub(hf_hub_path, allow_pickle=False)[source]

Class method to be used to load a pretrained model from the Hugging Face hub

Parameters

hf_hub_path (str) – The path where the model should have been be saved on the hugginface hub.

Note

This function requires the folder to contain:

  • a model_config.json and a model.pt if no custom architectures were provided

or

  • a model_config.json, a model.pt and a encoder.pkl (resp. decoder.pkl) if a custom encoder (resp. decoder) was provided
predict(inputs)[source]

The input data is encoded and decoded without computing loss

Parameters

inputs (torch.Tensor) – The input data to be reconstructed, as well as to generate the embedding.

Returns

An instance of ModelOutput containing reconstruction and embedding

Return type

ModelOutput

push_to_hf_hub(hf_hub_path)[source]

Method allowing to save your model directly on the Hugging Face hub. You will need to have the huggingface_hub package installed and a valid Hugging Face account. You can install the package using

python -m pip install huggingface_hub

end then login using

huggingface-cli login
Parameters

hf_hub_path (str) – path to your repo on the Hugging Face hub.

reconstruct(inputs)[source]

This function returns the reconstructions of given input data.

Parameters
  • inputs (torch.Tensor) – The inputs data to be reconstructed of shape [B x input_dim]

  • ending_inputs (torch.Tensor) – The starting inputs in the interpolation of shape

Returns

A tensor of shape [B x input_dim] containing the reconstructed samples.

Return type

torch.Tensor

save(dir_path)[source]

Method to save the model at a specific location. It saves, the model weights as a models.pt file along with the model config as a model_config.json file. If the model to save used custom encoder (resp. decoder) provided by the user, these are also saved as decoder.pkl (resp. decoder.pkl).

Parameters

dir_path (str) – The path where the model should be saved. If the path path does not exist a folder will be created at the provided location.

set_decoder(decoder)[source]

Set the decoder of the model

set_encoder(encoder)[source]

Set the encoder of the model

update()[source]

Method that allows model update during the training (at the end of a training epoch)

If needed, this method must be implemented in a child class.

By default, it does nothing.

class pythae.models.base.base_utils.ModelOutput[source]

Base ModelOutput class fixing the output type from the models. This class is inspired from the ModelOutput class from hugginface transformers library

to_tuple()[source]

Convert self to a tuple containing all the attributes/keys that are not None.