Skip to main content

bayesflow-hpo

CI PyPI GitHub release codecov Python 3.12+ 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.12, BayesFlow >= 2.1.0 and a Keras 3 backend (>= 3.15). BayesFlow 2.0.x is not supported; weights trained under 2.0.x cannot be loaded on 2.1, see the changelog. No backend is installed as a dependency, so install one separately (PyTorch recommended) and select it before importing:

pip install torch
# or the CPU wheel:
pip install torch --index-url https://download.pytorch.org/whl/cpu
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) 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

bayesflow-hpo is developed by a small group of researchers. Bug reports, questions and feature requests are welcome from everyone as GitHub issues. Pull requests are reserved for approved collaborators; if you would like to contribute code, please open an issue first to discuss it. If you use the package in your research, please cite it via CITATION.cff (GitHub's "Cite this repository").

Development setup (collaborators)

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/

New code must be backed by a research article or package documentation, with the reference recorded in docs/references.md; see docs/contributing-references.md.

License

MIT

Metadata

Release files for bayesflow-hpo 0.8.0

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.8.0
File Size Uploaded Provenance Actions
bayesflow_hpo-0.8.0.tar.gz 273.9 kB With provenance Details for bayesflow_hpo-0.8.0.tar.gz

Built distribution (wheel)

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

Total release size: 524.5 kB

Release files / bayesflow_hpo-0.8.0.tar.gz

Download URL bayesflow_hpo-0.8.0.tar.gz
Size 273.9 kB
Tags Source
SHA-256 checksum
How to use checksums
669a2b4f2c687c4060951ff1dfec7a28d82868090a3efdb1284a0d8ec58eac81
BLAKE2b-256 checksum
How to use checksums
0ff77863ccbc9d0d551ea10f8d368434c0650818103748495553f475492d70d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 7, 2026.

Transparency log

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

Download URL bayesflow_hpo-0.8.0-py3-none-any.whl
Size 250.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0fd8d71d03ac6339a4ee2dc5424dc1bae78df0837cfd1eef40467c2105f8a6a7
BLAKE2b-256 checksum
How to use checksums
c924289fa1606132cf3feb604f49697c877db0a4f0ef7b7cfbb5e059e2b908cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 7, 2026.

Transparency log

Security for bayesflow-hpo 0.8.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bayesflow_hpo-0.8.0.tar.gz

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 Nov 7, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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