bayesflow-irt
A BayesFlow add-on for amortized Bayesian inference in Item Response Theory (IRT) models: train a network once on simulated response matrices, then draw posteriors for new data sets without refitting, for any number of items and persons within the trained range.
What is included
- Variable-size simulators and adapters for the 1PL, 2PL, PCM, and GPCM models
MatrixEquivariantIRTSummary, a summary network that is exchangeable over both items and persons (Hartford-style matrix blocks)EquivariantIRTApproximatorfor per-item posterior inference- Person scoring, metadata sidecars for trained models, and memory controls for
large
n_persons - Optional: hyperparameter-optimization hooks (
bayesflow_irt.hpo) - Planned: pretrained checkpoints with model cards, and example notebooks
Installation
The package is not on PyPI yet. Install it from a clone:
git clone https://github.com/matthiaskloft/bayesflow-irt.git
cd bayesflow-irt
pip install -e ".[torch]"
Requires BayesFlow 2.1 or later (installed automatically). BayesFlow 2.0.x is not supported, and checkpoints trained on it do not load.
Extras: dev (tests), examples (plotting and Jupyter), card (model-card
plots), hpo (bayesflow-hpo, from PyPI), and numba (faster response
sampling). The
tensorflow and jax backend extras are also available.
Set the Keras backend before importing keras, bayesflow, or bayesflow_irt:
export KERAS_BACKEND=torch # PowerShell: $env:KERAS_BACKEND = "torch"
Quick start
import bayesflow as bf
from bayesflow_irt import (
EquivariantIRTApproximator,
MatrixEquivariantIRTSummary,
Prior,
make_2pl_adapter,
make_2pl_simulator,
)
simulator = make_2pl_simulator(
n_items=(10, 25),
n_persons=(100, 250),
prior_a=Prior("lognormal", loc=0.0, scale=0.3),
prior_b=Prior("normal", loc=0.0, scale=2.0),
)
adapter = make_2pl_adapter(simulator, internal_parameterization="loading")
approximator = EquivariantIRTApproximator(
adapter=adapter,
inference_network=bf.networks.CouplingFlow(),
summary_network=MatrixEquivariantIRTSummary(embed_dim=64),
)
approximator.compile(optimizer="adam")
approximator.fit(simulator=simulator, epochs=10, num_batches=32, batch_size=64)
batch = simulator.sample((4,))
posterior = approximator.sample(num_samples=200, conditions=batch)
The public functions and classes are documented in their docstrings.
Examples
examples/train_1pl_minimal.py is a minimal end-to-end script (simulate, adapt, train, sample, save). Pretrained checkpoints are not distributed yet; example notebooks will follow with them.
For longer runs, scripts/train_irt.py runs the same training pipeline from a JSON config and writes only files:
python scripts/train_irt.py --config scripts/configs/smoke_1pl.json
scripts/configs/ has a quick smoke test (smoke_1pl.json) and a fixed-size
2PL run (2pl_fixed.json).
On Windows, run Jupyter with a kernel that imports torch first
(scripts/ipykernel_torch_preload.py);
otherwise loading pyzmq before torch can crash with WinError 1114.
Target-size adaptation
Target-size adaptation is explicit through adapt_and_fit: it initializes from
a pretrained checkpoint, trains on fresh simulations at the observed matrix's
dimensions using the recorded priors, saves a distinct checkpoint and metadata,
then reloads it for item inference. Observed responses are never training targets.
from bayesflow_irt import adapt_and_fit
result = adapt_and_fit(
"parent.keras", responses, "new_adapted_run",
metadata="parent_metadata.json",
epochs=2, num_batches=10, batch_size=4, learning_rate=1e-4, seed=42,
n_item_samples=100, fit_kwargs={"workers": 0}, score_kwargs={},
)
result.scorer.score(new_person_responses) # reuse the inferred item draws
The output directory must be new. Results include checkpoint/sidecar paths,
training history, item draws, and separate adaptation, inference and scoring
times. Training or persistence failures raise before inference. Omit
score_kwargs to defer scoring. The checkpoint can later be loaded with
keras.saving.load_model(result.checkpoint, compile=False) for inference without
retraining. See examples/adapt_1pl.py for a small example.
Validate adapted models on fresh target-size simulations before accuracy or
calibration claims; million-person feasibility is a separate experiment.
Pass existing prior objects to replace any recorded priors during fine-tuning:
from bayesflow_irt import Prior
# Add this argument to adapt_and_fit(...):
prior_overrides = {
"b": Prior("normal", loc=0, scale=1.5),
"a": Prior("gamma", concentration=4, scale=0.25), # 2PL/GPCM
"theta": Prior("normal", loc=0, scale=1.2),
}
Each entry replaces the entire prior; omitted entries keep the parent's prior.
The simulator uses these priors and the adapted sidecar records both the new
specification and overrides. Parent files and adapter transforms stay unchanged.
VariablePrior objects are also accepted when they preserve the pretrained
adapter's sigma condition keys. Bare callables cannot be persisted and are
rejected. Prior names and distributions are checked by the existing simulator
and prior utilities. Normal theta priors are also used for person scoring;
other theta distributions support item inference, return scorer=None, and
raise before training if score_kwargs requests unsupported person scoring.
Development
pip install -e ".[dev,torch]"
pytest tests/
ruff check src/
See CONTRIBUTING.md and CHANGELOG.md.
License and citation
MIT, see LICENSE. To cite the software, see CITATION.cff. The methods the package implements are cited in references.md.
Metadata
Release files for bayesflow-irt 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bayesflow_irt-0.7.0.tar.gz | 595.1 kB | With provenance | Details for bayesflow_irt-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bayesflow_irt-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for bayesflow_irt-0.7.0-py3-none-any.whl |
Total release size: 934.0 kB
Release files / bayesflow_irt-0.7.0.tar.gz
| Download URL | bayesflow_irt-0.7.0.tar.gz |
|---|---|
| Size | 595.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6fb595adf7af0d3e39f87c487f472cb9f828e3ca65158c41591b871492b366dd
|
|
BLAKE2b-256 checksum How to use checksums |
d1d3870dbd15940a07ab2d2fff56b5b24f3b6e61d40ec3f9b933c8c4fe17f10e
|
| 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 logRelease files / bayesflow_irt-0.7.0-py3-none-any.whl
| Download URL | bayesflow_irt-0.7.0-py3-none-any.whl |
|---|---|
| Size | 338.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cc8ed9e15e7ba62b6ec4cb69067a15221c9a6d7eb3accdcda3aa58738c91baf6
|
|
BLAKE2b-256 checksum How to use checksums |
4a945360f497fe86cc6601982151acbbc5f1570d1cd55874bd92b8130fe35e5b
|
| 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 logSecurity for bayesflow-irt 0.7.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.
Attestation for bayesflow_irt-0.7.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 logA 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.