BaseAE¶
Abstract class
This is the base AuteEncoder architecture module from which all future autoencoder based models should inherit.
It contains:
- a
BaseAEConfiginstance containing the main model’s parameters (e.g. latent dimension …) - a
BaseAEinstance which creates a BaseAE model having a basic autoencoding architecture - The
ModelOutputinstance used for neural nets outputs and model outputs of theforwardmethod).
- 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.
- 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
- forward(inputs, **kwargs)[source]¶
Main forward pass outputing the VAE outputs This function should output a
ModelOutputinstance 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
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
- 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.jsonand amodel.ptif no custom architectures were provided
or
- a
model_config.json, amodel.ptand aencoder.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.jsonand amodel.ptif no custom architectures were provided
or
- a
model_config.json, amodel.ptand aencoder.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
- 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
- save(dir_path)[source]¶
Method to save the model at a specific location. It saves, the model weights as a
models.ptfile along with the model config as amodel_config.jsonfile. If the model to save used custom encoder (resp. decoder) provided by the user, these are also saved asdecoder.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.