A small library for conditional density estimation using masked autoregressive flows (MAF) and mixture density networks (MDN), with Python & C implementations and a small CLI.
Download the prebuilt binary for your platform from the GitHub Releases page.
# Train a conditional density model
./smolcde-linux-x86_64 train \
--features features.csv \
--params params.csv \
--out model.maf \
--epochs 200 --hidden 32 --blocks 5
# Draw samples conditioned on new features
./smolcde-linux-x86_64 infer \
--model model.maf \
--features test_features.csv \
--out predictions.csv \
--mode sample --samples 64pip install .import cde
import numpy as np
# Conditional density p(params | features)
C = cde.MAFEstimator(param_dim=3, feature_dim=10, hidden_size=32, n_flows=5)
C.train(params_data, features_data, n_epochs=200)
# Sample from the posterior for new features
samples = C.sample(features_new, n_samples=1000)
mean_est = np.mean(samples, axis=1)pip install . # or: pip install -e ".[dev]"
pytest # 17 tests, including C gradient checkscmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
cp build/libsmolmaf.so . # or .dylib / .dll
cp build/smolcde . # CLI binary
pytestSee Releases. Each release bundles:
smolcde-*— CLI binary for your platformlibsmolmaf-*.{so,dylib,dll}— shared library for Python ctypescde.py— drop-in Python moduleREADME.md
Note: Prebuilt macOS binaries are Apple Silicon (arm64) only. Intel Mac users can build from source with
cmakeandmake.
Note: Prebuilt macOS binaries are Apple Silicon (arm64) only. Intel Mac users can build from source with
cmakeandmake.
bash demos/demo_banana.sh # Synthetic banana-shaped density
bash demos/demo_mnist.sh # MNIST 8×8 classification (one-hot MAF)
bash demos/demo_lorenz.sh # Lorenz attractor regression
python demos/bench_c_vs_py.py # Speed comparison| Component | Language | Description |
|---|---|---|
cde.py |
Python | MAFEstimator, MDNEstimator, data generators |
maf.c / maf.h |
C | Fast MAF forward, backward, sampling, log-prob |
main.c |
C | CLI: train and infer commands |
Key design choice: the CLI binary statically links maf.c for
portability, while Python loads libsmolmaf via ctypes for speed.
C training is ~24× faster than Python autograd (2000 samples, 100 eepochs: C 254 ms vs Python 6.16 s).
- D=1 MAF is degenerate — the autoregressive structure is vacuous
for scalar targets. For classification, use one-hot encoding (D≥2).
For scalar regression, use MDN. A
RuntimeWarningis emitted whenparam_dim=1.
python -m pytest test_cde.py -v17 tests covering gradient correctness, deterministic sampling, model roundtrip, all inference modes, CLI coverage, resume training, and MNIST pipeline.
MIT