Skip to main content

bayesflow-hpo

CI codecov Python 3.11+ License: MIT

Automated hyperparameter optimization for BayesFlow 2.x neural posterior estimation (NPE) models, powered by Optuna.

Tunes inference networks, summary networks, and training hyperparameters — so you can focus on the model, not the plumbing.

Installation

pip install bayesflow-hpo            # core
pip install bayesflow-hpo[dashboard] # + Optuna dashboard
pip install bayesflow-hpo[sklearn]   # + C2ST metrics

Requires Python >= 3.11 and a Keras 3 backend (PyTorch recommended: export KERAS_BACKEND=torch).

How it works

                              ┌──────────────────────────┐
                              │   CompositeSearchSpace   │
                              │  ┌────────────────────┐  │
 Your simulator + adapter ──► │  │ Inference networks │  │
                              │  │ Summary networks   │  │ ──► Optuna Study
 Validation conditions ─────► │  │ Training params    │  │      (n_trials)
                              │  └────────────────────┘  │
                              └──────────────────────────┘
                                          │
                                 Each trial:
                                 1. Sample hyperparameters
                                 2. Budget check (reject if too large)
                                 3. Build approximator
                                 4. Compile with the mode's LR schedule
                                 5. Train
                                 6. Validate on fixed dataset
                                 7. Report metrics to Optuna

Features

  • 10 network search spaces — CouplingFlow, FlowMatching, DiffusionModel, ConsistencyModel, StableConsistencyModel, DeepSet, SetTransformer, FusionTransformer, TimeSeriesNetwork, TimeSeriesTransformer
  • FlowMatching solver controls — tune or fix fm_integrate_method / fm_integrate_steps plus TimeMLP kwargs (fm_merge, fm_norm, fm_residual, fm_spectral_normalization) with BayesFlow-default constants when untuned
  • Network selection — let Optuna choose the best architecture via NetworkSelectionSpace / SummarySelectionSpace
  • Sampler presets — TPE, GP, BoTorch, NSGA-II/III, Auto, Random with auto-wired budget constraints
  • QMC warm-up — Sobol sequences for better space-filling startup coverage
  • Multi-objective pruning — dominance, MO-SHA, primary-median strategies
  • Metric constraints — hard post-validation rejection, soft feasibility guidance
  • Coherent training modes — equal-budget cosine runs, plus open-ended inverse-sqrt runs with validation early stopping
  • Derived hyperparameters — express exact simulation-budget relationships without redundant sampler dimensions
  • Custom hooks — replace build, train, or validate steps while reusing the full trial lifecycle
  • Pre-flight validation — check_pipeline() catches interface errors before GPU hours are wasted
  • Multi-objective — single-metric, mean-aggregated, or full Pareto-front optimization (2-3 objectives)
  • Rich visualization — plot_study() adaptive dashboard with pairwise Pareto projections, per-objective history, and parameter importance; plus standalone plot_pareto_3d(), plot_parallel_coordinates(), and more
  • 19 built-in validation metrics — calibration, accuracy, SBC diagnostics, C2ST, plus a registry for custom metrics
  • Budget constraints — reject infeasible architectures before training (max_param_count, max_memory_mb="auto")
  • Custom network registration — plug in your own inference/summary networks
  • Study management — resume, warm-start, save/load workflows, analyze results
  • Lexicographic-Pareto selection — select_best_trial() with satisficing thresholds

Examples

See the examples/ directory for complete walkthroughs:

# Speed-sensitive FlowMatching search space
import bayesflow_hpo as hpo

inference_space = hpo.FlowMatchingSpace.fast()
search_space = hpo.CompositeSearchSpace(inference_space=inference_space)
Notebook Description
getting_started.ipynb End-to-end HPO: search, train, validate, select & retrain the best model
two_moons_optimization.ipynb Network selection (CouplingFlow vs FlowMatching) on the Two Moons benchmark
custom_summary_network.ipynb Custom summary network registration + HPO
qmc_warmup_benchmark.ipynb QMC warm-up effectiveness benchmark (TPE vs GP, convergence analysis)
optuna_dashboard.md Optuna dashboard integration guide

Contributing

Each checkout needs its own virtual environment — including each git worktree, since an editable install resolves to the source tree it was installed from.

git clone git@github.com:matthiaskloft/bayesflow-hpo.git
cd bayesflow-hpo
python -m venv .venv
PY=.venv/bin/python                             # .venv/Scripts/python on Windows
$PY -m pip install torch --index-url https://download.pytorch.org/whl/cpu
$PY -m pip install -e ".[dev]"                  # [dev] pulls no backend of its own

export KERAS_BACKEND=torch
$PY -m pytest tests/ -v
$PY -m ruff check src/ tests/

License

MIT

Metadata

Release files for bayesflow-hpo 0.5.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 bayesflow-hpo 0.5.3
File Size Uploaded
bayesflow_hpo-0.5.3.tar.gz 266.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bayesflow-hpo 0.5.3
File Interpreter ABI Platform
bayesflow_hpo-0.5.3-py3-none-any.whl Python 3 none any Details

Total release size: 512.1 kB

Release files / bayesflow_hpo-0.5.3.tar.gz

Download URL bayesflow_hpo-0.5.3.tar.gz
Size 266.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d40c7acbd72c559f1e138d1e813028c991c08c1f2eea0442cd509042ad9ea1d1
BLAKE2b-256 checksum
How to use checksums
0c7940a6dbbf7763b36f6cd6123d43bc9e3b903afcc21d41191d0478ba439f40
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 Oct 15, 2026.

Transparency log

Release files / bayesflow_hpo-0.5.3-py3-none-any.whl

Download URL bayesflow_hpo-0.5.3-py3-none-any.whl
Size 245.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2dd9c68a168b46a53ab1d5328d90792bc69cc62ec61a3371c56fd5ca5c9a5972
BLAKE2b-256 checksum
How to use checksums
fd483e13f6c44b2be68cedee40cd67342198a0b31f74cd9e048231b8ae58f2b2
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 Oct 15, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.5.3 This release

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