Skip to main content

bayesflow-hpo

CI PyPI GitHub release 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. 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, 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

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.6.1

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.6.1
File Size Uploaded Provenance
bayesflow_hpo-0.6.1.tar.gz 269.9 kB With provenance Details

Built distribution (wheel)

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

Total release size: 517.8 kB

Release files / bayesflow_hpo-0.6.1.tar.gz

Download URL bayesflow_hpo-0.6.1.tar.gz
Size 269.9 kB
Tags Source
SHA-256 checksum
How to use checksums
14f045fff51092f6ad058be12a8df772ca2f4d8d61e8d8ff2855def01dd209f7
BLAKE2b-256 checksum
How to use checksums
4424f87aedd689e6e409923961cb3b91ad85c8b40fd5422bc7131e678b67d9a1
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 18, 2026.

Transparency log

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

Download URL bayesflow_hpo-0.6.1-py3-none-any.whl
Size 247.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4aa53237c48880e9365a3e090547d2fbd7a72358dbf49e5cc5f8a2095b98ae96
BLAKE2b-256 checksum
How to use checksums
59246957bbb148e9a3c1bc08d48f7a8fa3913413f1a3bd8d287911c8d3ca3848
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 18, 2026.

Transparency log

Security for bayesflow-hpo 0.6.1

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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bayesflow_hpo-0.6.1.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 Oct 18, 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.

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