Skip to main content

🧶 Disent

A modular disentangled representation learning framework built with PyTorch Lightning

license python versions pypi version tests status Code style: ruff

Visit the docs for more info, or browse the releases.

Contributions are welcome!

────────────────
NOTE: My MSc. research has moved here
Some of the contributions have been incorporated directly into disent
────────────────


Table Of Contents


Overview

Disent is a modular disentangled representation learning framework for auto-encoders, built upon PyTorch-Lightning. This framework consists of various composable components that can be used to build and benchmark various disentanglement vision tasks.

The name of the framework is derived from both disentanglement and scientific dissent.

Get started with disent by installing it with $pip install disent or cloning this repository.

Goals

Disent aims to fill the following criteria:

  1. Provide high quality, readable, consistent and easily comparable implementations of frameworks
  2. Highlight difference between framework implementations by overriding hooks and minimising duplicate code
  3. Use best practice eg. torch.distributions
  4. Be extremely flexible & configurable
  5. Support low memory systems

Citing Disent

Please use the following citation if you use Disent in your own research:

@Misc{Michlo2021Disent,
  author =       {Nathan Juraj Michlo},
  title =        {Disent - A modular disentangled representation learning framework for pytorch},
  howpublished = {Github},
  year =         {2021},
  url =          {https://github.com/nmichlo/disent}
}

Features

Disent includes implementations of modules, metrics and datasets from various papers.

Note that "🧵" means that the dataset, framework or metric was introduced by disent!

Datasets

Various common datasets used in disentanglement research are included with disent. The dataset loaders provide various features including:

  • automatic downloads & preperation prepare=True
  • automatic hash verification
  • automatic optimization of underlying hdf5 formats for low-memory disk-based access.

Data input and target dataset augmentations and transforms are supported, as well as augmentations on the GPU or CPU at different points in the pipeline.

  • Ground Truth:

    • 🧵 dSpritesImagenet: Version of DSprite with foreground or background deterministically masked out with tiny-imagenet data.

      dSpritesImagenet Dataset Factor Traversals

  • Ground Truth Synthetic:

    • 🧵 XYSquares: Three non-overlapping squares that can move around a grid. This dataset is adversarial to VAEs that use pixel-wise reconstruction losses.

      XYSquares Dataset Factor Traversals

    • 🧵 XYObject: A simplistic version of dSprites with a single square.

      XYObject Dataset Factor Traversals

    • 🧵 XYObjectShaded: Exact same dataset as XYObject, but ground truth factors have a different representation.

      XYObjectShaded Dataset Factor Traversals

Frameworks

Disent provides the following Auto-Encoders and Variational Auto-Encoders!

  • Unsupervised:
    • AE: Auto-Encoder
    • VAE: Variational Auto-Encoder
    • Beta-VAE: VAE with Scaled Loss
    • DFC-VAE: Deep Feature Consistent VAE
    • DIP-VAE: Disentangled Inferred Prior VAE
    • InfoVAE: Information Maximizing VAE
    • BetaTCVAE: Total Correlation VAE
  • Weakly Supervised:
    • Ada-GVAE: Adaptive GVAE, AdaVae.cfg(average_mode='gvae'), usually better than below!
    • Ada-ML-VAE: Adaptive ML-VAE, AdaVae.cfg(average_mode='ml-vae')
  • Supervised:
    • TAE: Triplet Auto-Encoder
    • TVAE: Triplet Variational Auto-Encoder

Introduced in Disent

  • Unsupervised:
    • 🧵 Ada-TVAE-D: Adaptive Triplet VAE that uses data distances instead of ground-truth distances as the supervision signal.
    • 🧵 Ada-TAE-D: Adaptive Triplet AE that uses data distances instead of ground-truth distances as the supervision signal.
  • Weakly Supervised:
    • 🧵 Ada-AE: Adaptive AE, the auto-encoder version of the Ada-GVAE
  • Supervised:
    • 🧵 Ada-TVAE: Adaptive Triplet VAE, disentangled version of the TVAE
    • 🧵 Ada-TAE: Adaptive Triplet AE, disentangled version of the TAE
🏗 Todo: Many popular disentanglement frameworks still need to be added, please submit an issue if you have a request for an additional framework.

  • FactorVAE
  • GroupVAE
  • MLVAE

Metrics

Various metrics are provided by disent that can be used to evaluate the learnt representations of models that have been trained on ground-truth data.

  • Disentanglement:
    • FactorVAE Score
    • DCI
    • MIG
    • SAP
    • Unsupervised Scores
    • 🧵 Flatness Components: Measures of the three components needed to learn factored representations from distances. VAEs often learn the first two (correlation & linearity), and the can happen accidentally (axis-alignment)!
      • 🪡 Ground-Truth Correlation - The spearman rank correlation between latent distances and ground-truth distances.
      • 🪡 Linearity Ratio - How well factor traversals lie along an n-dimensional arbitrarily rotated line in the latent space
      • 🪡 Axis-Alignment Ratio - How well factor traversals are represented by a single latent variable, ie. an n-dimensional line that is axis-aligned.
    • 🧵 Flatness Score - Measuring the max distance between factor traversal embeddings and the path length of their embeddings.
🏗 Todo: Some popular metrics still need to be added, please submit an issue if you wish to add your own, or you have a request.

Schedules & Annealing

Hyper-parameter annealing is supported through the use of schedules. The currently implemented schedules include:

  • Linear Schedule
  • Cyclic Schedule
  • Cosine Wave Schedule
  • Various other wrapper schedules

Architecture

The disent module structure:

  • disent.dataset: dataset wrappers, datasets & sampling strategies
    • disent.dataset.data: raw datasets
    • disent.dataset.sampling: sampling strategies for DisentDataset when multiple elements are required by frameworks, eg. for triplet loss
    • disent.dataset.transform: common data transforms and augmentations
    • disent.dataset.wrapper: wrapped datasets are no longer ground-truth datasets, these may have some elements masked out. We can still unwrap these classes to obtain the original datasets for benchmarking.
  • disent.frameworks: frameworks, including Auto-Encoders and VAEs
    • disent.frameworks.ae: Auto-Encoder based frameworks
    • disent.frameworks.vae: Variational Auto-Encoder based frameworks
  • disent.metrics: metrics for evaluating disentanglement using ground truth datasets
  • disent.model: common encoder and decoder models used for VAE research
  • disent.nn: torch components for building models including layers, transforms, losses and general maths
  • disent.schedule: annealing schedules that can be registered to a framework
  • disent.util: helper classes, functions, callbacks, anything unrelated to a pytorch system/model/framework.

⚠️ The API Is Mostly Stable ⚠️

Disent is still under development. Features and APIs are subject to change! However, I will try and minimise the impact of these.

A small suite of tests currently exist which will be expanded upon in time.

Hydra Experiment Directories

Easily run experiments with hydra config, these files are not available from pip install.

  • experiment/run.py: entrypoint for running basic experiments with hydra config
  • experiment/config/config.yaml: main configuration file, this is probably what you want to edit!
  • experiment/config: root folder for hydra config files
  • experiment/util: various helper code for experiments

Extending The Default Configs

All configs in experiment/config can easily be extended or overridden without modifying any files. We can add a new config folder to the hydra search path by setting the environment variable DISENT_CONFIGS_PREPEND to point to a config folder that should take priority over those contained in the default folder.

The advantage of this is that new frameworks and datasets can be used with experiments without cloning or modifying disent itself. You can separate your research code from the library!

  • See the examples in the docs for more information!

Examples

Python Example

The following is a basic working example of disent that trains a BetaVAE with a cyclic beta schedule and evaluates the trained model with various metrics.

💾 Basic Example

import lightning as L
import torch
from torch.utils.data import DataLoader

from disent.dataset import DisentDataset
from disent.dataset.data import XYObjectData
from disent.dataset.sampling import SingleSampler
from disent.dataset.transform import ToImgTensorF32
from disent.frameworks.vae import BetaVae
from disent.metrics import metric_dci
from disent.metrics import metric_mig
from disent.model import AutoEncoder
from disent.model.ae import DecoderConv64
from disent.model.ae import EncoderConv64
from disent.schedule import CyclicSchedule

# create the dataset & dataloaders
# - ToImgTensorF32 transforms images from numpy arrays to tensors and performs checks
# - if you use `num_workers != 0` in the DataLoader, the make sure to
#   wrap `trainer.fit` with `if __name__ == '__main__': ...`
data = XYObjectData()
dataset = DisentDataset(dataset=data, sampler=SingleSampler(), transform=ToImgTensorF32())
dataloader = DataLoader(dataset=dataset, batch_size=128, shuffle=True, num_workers=0)

# create the BetaVAE model
# - adjusting the beta, learning rate, and representation size.
module = BetaVae(
    model=AutoEncoder(
        # z_multiplier is needed to output mu & logvar when parameterising normal distribution
        encoder=EncoderConv64(x_shape=data.x_shape, z_size=10, z_multiplier=2),
        decoder=DecoderConv64(x_shape=data.x_shape, z_size=10),
    ),
    cfg=BetaVae.cfg(
        optimizer="adam",
        optimizer_kwargs=dict(lr=1e-3),
        loss_reduction="mean_sum",
        beta=4,
    ),
)

# cyclic schedule for target 'beta' in the config/cfg. The initial value from the
# config is saved and multiplied by the ratio from the schedule on each step.
# - based on: https://arxiv.org/abs/1903.10145
module.register_schedule(
    "beta",
    CyclicSchedule(
        period=1024,  # repeat every: trainer.global_step % period
    ),
)

# train model
# - for 2048 batches/steps
trainer = L.Trainer(
    max_steps=2048,
    accelerator="gpu" if torch.cuda.is_available() else "cpu",
    devices=1,
    logger=False,
    enable_checkpointing=False,
)
trainer.fit(module, dataloader)

# compute disentanglement metrics
# - we cannot guarantee which device the representation is on
# - this will take a while to run
get_repr = lambda x: module.encode(x.to(module.device))

metrics = {
    **metric_dci(dataset, get_repr, num_train=1000, num_test=500, show_progress=True),
    **metric_mig(dataset, get_repr, num_train=2000),
}

# evaluate
print("metrics:", metrics)

Visit the docs for more examples!

Hydra Config Example

The entrypoint for basic experiments is experiment/run.py.

Some configuration will be required, but basic experiments can be adjusted by modifying the Hydra Config 1.1 files in experiment/config.

Modifying the main experiment/config/config.yaml is all you need for most basic experiments. The main config file contains a defaults list with entries corresponding to yaml configuration files (config options) in the subfolders (config groups) in experiment/config/<config_group>/<option>.yaml.

💾 Config Defaults Example

defaults:
  # data
  - sampling: default__bb
  - dataset: xyobject
  - augment: none
  # system
  - framework: adavae_os
  - model: vae_conv64
  # training
  - optimizer: adam
  - schedule: beta_cyclic
  - metrics: fast
  - run_length: short
  # logs
  - run_callbacks: vis
  - run_logging: wandb
  # runtime
  - run_location: local
  - run_launcher: local
  - run_action: train

# <rest of config.yaml left out>
...

Easily modify any of these values to adjust how the basic experiment will be run. For example, change framework: adavae to framework: betavae, or change the dataset from xyobject to shapes3d. Add new options by adding new yaml files in the config group folders.

Weights and Biases is supported by changing run_logging: none to run_logging: wandb. However, you will need to login from the command line. W&B logging supports visualisations of latent traversals.


Install

pip install disent

Otherwise, to install from source we recommend using a conda virtual environment.

⤵️ Install from Source
# clone the repo
git clone https://github.com/nmichlo/disent
cd disent

# create and activate the conda environment [py38,py39,py310]
conda create -n disent-py310 python=3.10
conda activate disent-py310

# check that the correct python version is used
which python
which pip

# make sure to upgrade pip
pip install --upgrade pip

# install minimal requirements
pip install -e .

# (optional) install extra requirements
# - first do the above because torch is required to compile torchsort while installing
pip install -e ".[extra]"

# (optional) install test requirements
pip install -e ".[test]"

Development

Code style: ruff

Make sure to install pre-commit hooks to ensure code is automatically formatted correctly when committing or pushing changes to disent.

# install git hooks
pip install pre-commit
pre-commit install

# manually trigger all pre-commit hooks
pre-commit run --all-files

To run tests locally, make sure to install all the test and extra dependencies in your environment.

# torchsort first requires torch to be installed
pip install "torch>=2.0.0"
pip install -e ".[test]"

Why?

  • Created as part of my Computer Science MSc which ended early 2022.
  • I needed custom high quality implementations of various VAE's.
  • A pytorch version of disentanglement_lib.
  • I didn't have time to wait for Weakly-Supervised Disentanglement Without Compromises to release their code as part of disentanglement_lib. (As of September 2020 it has been released, but has unresolved discrepencies).
  • disentanglement_lib still uses outdated Tensorflow 1.0, and the flow of data is unintuitive because of its use of Gin Config.

Metadata

Release files for disent 0.9.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for disent 0.9.3
File Size Uploaded
disent-0.9.3.tar.gz 1.2 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for disent 0.9.3
File Interpreter ABI Platform
disent-0.9.3-py3-none-any.whl Python 3 none any Details

Total release size: 1.5 MB

Release files / disent-0.9.3.tar.gz

Download URL disent-0.9.3.tar.gz
Size 1.2 MB
Tags Source
SHA-256 checksum
How to use checksums
7ae300208ccbe9639877ea3103f27af98a9b8bdce1fca19eefd170e35ef4f54a
BLAKE2b-256 checksum
How to use checksums
83cea0267c30c4a0961ba74197bec4019cdba8187eb7a997091b6fb0f023a852
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 13, 2026.

Transparency log

Release files / disent-0.9.3-py3-none-any.whl

Download URL disent-0.9.3-py3-none-any.whl
Size 344.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b0790dec53a37b2703da2e7968435ac8060e9a32cca7bdb044da6b4ee33e8233
BLAKE2b-256 checksum
How to use checksums
18e47f6690540835bcd7db92846bfe89457fb63a6911df0eb3c6dde10cb01551
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 13, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.9.3 This release

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page