This repository contains code for a project undertaken as part of the Advanced Topics in Machine Learning course (HT 2020) at Oxford. The code here was written by Mikhail Andrenkov, Maxence Draguet, Sebastian Lee, and Diane Magnin.
It is a reproduction of the code for the paper Inference Suboptimality in Variational Autoencoders by Cremer, Li & Duvenaud.
Our code contains the following features relevant to replicating results from the paper:
- Flexible encoder/decoder architectures
- Various approximate posteriors inlcuding:
- Factorised Gaussian
- R-NVP flows
- R-NVP flows with auxiliary variables
- Relevant binarised image datasets inlcuding:
- MNIST
- Fashion-MNIST
- CIFAR-10
- Local optimisation training loop
- AIS and IWAE log-likelihood estimators
Additionally we implemented a planar flows approximate posterior.
To run this code you will need the following:
- Python 3.7+
Our code uses PyTorch. We include a requirements file (requirements.txt). We recommend creating a virtual environment (using conda
or virtualenv
) for this code base e.g.
python3 -m venv aml; source aml/bin/activate
From there, all Python prerequisites should be satisfied by running
pip3 install -r requirements.txt
To run experiments with a GPU, it is essential to use Python 3.7.5 (on Windows). Our code is compatible with CUDA 10.1.
We do not provide the datasets directly in this repository. However we are using modifications of standard datasets (e.g. MNIST, CIFAR10) that can be loaded with the torchvision datasets module. To retrieve the datasets, and make the requisite modifications (the binarisation specified by Larochelle et al) run:
python data/get_datasets.py
Standalone experiments can be run from the experiment folder using the main.py script. Configuration for such an experiment can be set using the base_config.yaml file for general attributes of the experiment as well as specific config files in the additional_configs/ folder (e.g. for setting parameters of a flow module).
Running a specific experiment from the paper can be done by accessing the relevant hard coded configuration files in the Experiment_List folder, which have been made to match the specifications of the paper. For example to reproduce the configuration of a fully-factorised gaussian approximate posterior with an amortised inference network (๐(VAE[q]) | qFFG
from Table 2. in the paper), run from the experiments folder:
python main.py -config experiment_list/expA/base_config.yaml -additional_configs experiment_list/expA/additional_configs/
Alternatively, all results from a given experiment can be run at once in sequence using the bash script in the relevant experiment folder.
Results of an experiment are by default saved in experiments/results/X/ where X is a timestamp for the experiment. Here you will find a copy of the configuration used to run that experiment, a .csv file containing logging of relevant metrics (e.g. train/test loss), and tensorboard events files. To view the tensorboard logs navigate to this folder and run:
tensorboard --logdir .
Alternatively run the command from elsewhere and modify the path accordingly. Plots of an experiment run can also be made by running the plot_from_df.py script from the experiments/plotting folder and passing the path to the folder containing the csv file to the -save_path flag.
Weights of the models being trained in a given experiment are also saved by default in experiment/saved_models/Y/X/ where Y is a hash of the configuration file and X is a timestamp for the experiment. Saved models can be loaded (e.g. to run local optimisation) by specifying the saved model path in the base config (Note they are saved weights and not full checkpoints so cannot be used to resume training).
Below is the structure of the relevant files in our repository.
โ
โโโ requirements.txt
โโโ README.md
โ
โโโ data
โ
โโโ experiments
โ โ
โ โ
โ โโโ additional_configs
โ โ โ
โ โ โโโ aux_flow_config.yaml
โ โ โโโ esimator_config.yaml
โ โ โโโ flow_config.yaml
โ โ โโโ local_optimisation_config.yaml
โ โ โโโ planar_config.yaml
โ โ
โ โโโ experiment_list (bash scripts for paper experiments)
โ โ โ
โ โ โโโ expA
โ โ โโโ expB
โ โ โโโ expB
โ โ โโโ expC
โ โ โโโ expD
โ โ โโโ expE
โ โ
โ โโโ plotting
โ โ โ
โ โ โโโ plot_config.json
โ โ โโโ plot_from_df.py
โ โ
โ โโโ results
โ โ โ
โ โ โโโ **result files (not tracked/commited)**
โ โ
โ โโโ saved_models
โ โ โ
โ โ โโโ **saved_model files (not tracked/commited)**
โ โ
โ โโโ base_config.yaml
โ โโโ context.py
โ โโโ main.py
โ
โโโ models
โ โ
โ โ
โ โโโ approximate_posteriors
โ โ โ
โ โ โโโ __init__.py
โ โ โ
โ โ โโโ base_approximate_posterior.py
โ โ โโโ base_norm_flow.py
โ โ โโโ gaussian.py
โ โ โโโ planar_flow.py
โ โ โโโ rnvp_aux_flow.py
โ โ โโโ rnvp_flow.py
โ โ โโโ sylv_flow.py
โ โ
โ โโโ likelihood_estimators
โ โ โ
โ โ โโโ __init__.py
โ โ โ
โ โ โโโ ais_estimator.yaml
โ โ โโโ base_estimator.yaml
โ โ โโโ iwae_estimator.yaml
โ โ โโโ max_estimator.yaml
โ โ
โ โโโ local_optimisation_modules
โ โ โ
โ โ โโโ __init__.py
โ โ โ
โ โ โโโ base_local_optimisation.py
โ โ โโโ gaussian_local_optimisation.py
โ โ โโโ rnvp_aux_flow_local_optimisation.py
โ โ โโโ rnvp_flow_local_optimisation.py
โ โ
โ โโโ loss_modules
โ โ โ
โ โ โโโ __init__.py
โ โ โ
โ โ โโโ base_loss.py
โ โ โโโ gaussian_loss.py
โ โ โโโ planar_loss.py
โ โ โโโ rnvp_aux_loss.py
โ โ โโโ rnvp_loss.py
โ โ
โ โโโ networks
โ โ โ
โ โ โโโ __init__.py
โ โ โ
โ โ โโโ base_network.py
โ โ โโโ convolutional.py
โ โ โโโ deconvolutional.py
โ โ โโโ fc_encoder.py
โ โ โโโ fc_decoder.py
โ โ
โ โโโ decoder.py
โ โโโ encoder.py
โ โโโ vae_runner.py
โ โโโ vae.py
โ
โโโ utils
โ
โ
โโโ __init__.py
โ
โโโ custom_torch_transforms.py
โโโ dataloaders.py
โโโ math_operations.py
โโโ parameters.py
โโโ torch_operations_test.py
โโโ torch_operations.py