- Python 63.2%
- Jupyter Notebook 35.2%
- Dockerfile 1.6%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| configs | ||
| discrete_bottleneck | ||
| notebooks | ||
| tests | ||
| .dockerignore | ||
| .gitignore | ||
| .pre-commit-config.yaml | ||
| __init__.py | ||
| Dockerfile | ||
| pip_requirements.txt | ||
| README.md | ||
| requirements.yaml | ||
| run.py | ||
Outline
The README contains a description of the symbol-lab environments, the models as well as instructions for running the code.
- Section 1 contains a description of the project.
- Section 2 talks about our synthetic environments.
- Section 3 describes the implemented models.
- Section 4 presents the installation instructions and how to train the model.
- Section 5 shows how to do a hyperparameter search for any model using our codebase.
- Section 6 presents the test cases and how to run them and ensure that the modules work as expected.
The codebase is built upon PytorchLightning and Hydra. If you have not used them before, please take a look at the respective documentation so you can get to speed when running the code from this repo.
1. Project Description
Using continuous latent variables has been ubiquitous in deep learning; however, recent developments in discrete optimization have paved the path for exploiting the benefits of such representations, not only in modalities such as image or speech but particularly in the domain of language, which is inherently discrete.
Our goal to learn discrete representation of text in a self-supervised way. To that aim, we propose symbol-lab, an environment for testing the models’ ability to encode input into a symbolic (discrete) compositional representation. With a structure that allows for fine-grained control over the difficulty of the task, and an arbitrary amount of training data, symbol-lab aids us in uncovering the strengths and weaknesses of existing methods and facilitates the development of new ones. The learning methods/models that we are working with/on are listed in Section 3.
2. Symbol-lab
Currently, symbol-lab supports two environments:
- Grid Dataset
- Controlled decoder operating on structured discrete latent
Below a short description of scenarios supported by each dataset is given, and you can find more details about each environment in datasets readme.
Note that these datasets inherit PyTorch's Dataset class. Therefore, you could either use symbol-lab through the PyTorch lightning datamodule or as standalone datasets in your code. You can refer to our usage example in this notebook.
2.1. Grid Dataset
Grid Dataset consists of samples representing grids of cells that can be occupied by different types of objects or can be left empty. Each sample of this dataset represents one random grid. Grid dataset supports two scenarios:
- ConstantGrid: In this dataset, all samples are grids of the same size and have the same number of objects.
- VariableElementCountGrid: In this dataset, all samples are grids of the same size, but the number of objects present in any sample grid can be anywhere between a predefined minimum and a maximum.
You can find the corresponding dataset classes with more details in the comments here.
2.2. Structured Discrete Latent
Unlike Grid dataset in which the latents were not explicitly available nor they were used to generate the samples, in this dataset, we explicitly specify both the structure of the discrete latent Z and the decoder's (f) properties. A sample of this dataset would be f(z) where z is sampled from the possible discrete space Z. With this dataset we can evaluate the performance of different models under any combination of the following scenarios:
Decoder f can be:
- Stochastic vs. Deterministic
- Invertible vs. Non-invertible
- Outputs a single real number or a sequence
Discrete latent Z can be:
- A single discrete variable with 2 possible values
- A single discrete variable with
m>2possible values - A sequence of discrete variables each with
m>2possible values
You can find the corresponding dataset classes with more details in the comments here.
3. Models
This repository (currently) implements three models:
- A sequence to sequence model with a continuous latent representation in the bottleneck (Sutskever et al. 2014)
- A version of seq2seq that uses the quantization technique from VQ-VAE to obtain a discrete representation at the output of the encoder.
- Our adaptation of the discrete sequence autoencoder proposed by SEQ^3.
Note, that both baselines encode the input sequence to one high-dimensional single vector representation (either discrete or continuous), but SEQ^3 encodes the source text into a sequence of tokens/words.
4. Setup Instructions
- Clone this repository
- Run
docker build -t symbol_lab .(have a folder with your public ssh key in the same directory; it will be used for login in to the container) - Choose your preferred port for running a jupyter server (9991, for instance) and run the following:
port=9991docker run -ti --rm --gpus all -p $port:$port --name symbol_lab_container symbol_lab bashcd / && jupyter lab --ip 0.0.0.0 --port 9991(copy the url for the jupyter server host)
Detach from the container with Ctrl+P, then Ctrl-Q- Create an ssh tunnel between the local and the remote machine using:
ssh -f YOURUSERNAME@your_machine_address -L PORT:localhost:PORT -Nand enter your password when prompted. - Paste the URL you copied earlier into your browser.
Training
Login to your weights&biases (wandb) account, copy your WANDB_API_KEY, and set the environment variable. If you're in a notebook, %env WANDB_API_KEY=YOUR_KEY. If you're in the terminal, export WANDB_API_KEY=YOUR_KEY
The entry point for this codebase is run.py in the home directory of the package. To start training, type any of the following to train the corresponding model:
python3 run.py mode=train model=seq2seqpython3 run.py mode=train model=vqvaepython3 run.py mode=train model=seq3
5. Hyperparameter Search
Use hydra's multirun option for a manual sweep over hyperparameters. For instance let's say we want to train the seq2seq model with several learning rates and number of layers on the grid dataset with different number of objects when the number of rows and columns is fixed. We can use the following line (to either run that many jobs locally, or if configured properly, through SLURM)
python3 run.py mode=train model=seq2seq model.lr=0.001,0.0001 model.n_layers=2,4 datamodule=grid datamodule.num_rows=3 datamodule.num_columns=4 datamodule.num_objects_to_place=3,4,5,6 --multirun
If you are running your jobs locally or working on a cluster that is not managed by SLURM, refer to the following. https://hydra.cc/docs/tutorials/basic/running_your_app/multi-run/
To do hp optimization, hydra has several options: Optuna, Nevergrad, Ax Sweeper (later).
6. Unit Testing
python tests/test_dataset.pypython tests/test_seq2seq_model.pypython tests/performance_seq2seq_model.py