Skip to main content

PyAIF (pyaif-toolkit)

PyAIF is an active-inference toolkit for Python. It constructs agents with factorised discrete hidden states and either categorical or continuous observations. It provides single-step inference, deep temporal inference, policy evaluation, action selection, and parameter-learning hooks.

Documentation · PyPI · Source code

Citation

If you use PyAIF in your research, please cite:

D. A. Warnakulasuriya, J. Plosila and H. Haghbayan, “Towards Self-Supervised Intent Recognition in Human-Robot Collaboration using Active Inference,” 2026 11th International Conference on Control and Robotics Engineering (ICCRE), Kyoto, Japan, 2026, pp. 191–198, doi:10.1109/ICCRE69951.2026.11593576.

Demonstration

Active Inference POMDP implementation on a Franka Emika Panda Robot

Active Inference POMDP implementation on a Franka Emika Panda Robot — ASL Lab UTU.

Learning under uncertainty

Active-inference learning response to a context change

The agent minimizes variational free energy (VFE) as it improves its generative model. When the task context changes, prediction errors produce a temporary spike in VFE surprise. Continued learning then refines the model for the new context, reducing surprise again. The animation plots VFE surprise (the smoothed deviation from an adaptive VFE baseline), rather than raw cumulative model evidence.

Version 0.2 adds domain-independent continuous-observation inference. Research applications and domain-specific likelihood construction remain in separate repositories.

Installation

PyAIF requires Python 3.9 or newer.

Install the published distribution:

python -m pip install pyaif-toolkit

The distribution is named pyaif-toolkit on PyPI, while the Python import package remains PyAIF.

To install from a source checkout:

python -m pip install .

For development:

python -m pip install -e ".[dev]"
pytest

Quick start

The public API separates the state-transition model, observation model, and inference algorithm.

import numpy as np

from PyAIF import (
    ActiveInfAgent,
    CategoricalLikelihood,
    GenerativeModel,
    ShallowInference,
)


def object_array(*values):
    result = np.empty(len(values), dtype=object)
    for index, value in enumerate(values):
        result[index] = np.asarray(value, dtype=float)
    return result


# A[o, s]: categorical observation likelihood.
A = object_array(
    np.array(
        [
            [0.95, 0.05],
            [0.05, 0.95],
        ]
    )
)

# B[s_next, s_previous, action]: controlled transitions.
B_factor = np.zeros((2, 2, 2))
B_factor[:, :, 0] = np.eye(2)
B_factor[:, :, 1] = np.fliplr(np.eye(2))
B = object_array(B_factor)

# D[s]: initial-state prior. C[o]: outcome preferences.
D = object_array(np.array([0.5, 0.5]))
C = object_array(np.zeros(2))

model = GenerativeModel(
    B=B,
    D=D,
    controls_dim=[2],
    controllable_factors=[0],
)
likelihood = CategoricalLikelihood(
    A=A,
    preferences=C,
    modality_dependencies=[[0]],
)
agent = ActiveInfAgent(
    model=model,
    likelihood=likelihood,
    inference=ShallowInference(),
    action_selection="deterministic",
)

agent.reset()
agent.observe([0])
agent.infer_states()
agent.infer_policies()
expected_free_energy = agent.last_policy_inference.expected_free_energy
action = agent.select_action()

Use DeepTemporalInference(horizon=...) for policy-dependent beliefs over a fixed temporal window. For continuous control, choose either RecedingHorizonInference(horizon=...) for policy-conditioned temporal message passing or FilteredRecedingHorizonInference(horizon=...) for the conventional robotics split: filter the current state once, then predict and score future states under each policy. Deep preferences have shape (number_of_outcomes, horizon).

Continuous observations

The likelihood component selects observation semantics; the inference component independently selects the temporal algorithm:

likelihood = ContinuousLikelihood(
    likelihood_fn=density,  # density(value, modality) -> state tensor
    observation_grids=[rssi_grid],
    log_preferences={0: rssi_log_preferences},
    modality_dependencies=[[0, 1]],
)

agent = ActiveInfAgent(
    model=model,
    likelihood=likelihood,
    inference=ShallowInference(),  # or a deep/receding inference configuration
)

For an existing domain model that implements likelihoods, get_o_grid, and log_preferences, use ContinuousLikelihood.from_model(...). PyAIF then integrates densities on the supplied grids for preference and information-gain evaluation. Small state spaces are enumerated exactly; large ones use reproducible Monte Carlo sampling controlled by policy_samples, exact_state_limit, and random_seed.

The optional learning_fn, preference_learning_fn, and parameter_information_gain_fn callbacks keep domain-specific updates outside the reusable agent core.

Performance and parallel policies

Deep temporal inference automatically batches independent policies into NumPy tensor operations. This reduces Python-loop overhead while preserving policy-by-policy inference results. Multi-factor categorical likelihoods use optimized contraction paths; likelihood entropy and packed posterior batches are reused across the future-time calculation.

Policy evaluation can also use bounded worker threads:

inference = DeepTemporalInference(
    horizon=3,
    message_passing_iterations=16,
    policy_workers=4,
)

policy_workers=1 is the default and is usually fastest for small categorical models because batched NumPy operations already process all policies efficiently. Values greater than one split policy batches across threads without copying the agent's arrays. Benchmark representative workloads before increasing the worker count; threading is most useful when each policy contains sufficiently large tensor contractions. ShallowInference accepts the same policy_workers option for concurrent policy scoring.

See the reproducible filtered receding-horizon CPU benchmark for comparisons among PyAIF temporal modes, classic NumPy pymdp, and current JAX pymdp. The original benchmark is retained for historical comparison. A separate resolution-scaling profile breaks policy evaluation into rollout, EFE contractions, posterior updating, and Bayesian model averaging.

Agent lifecycle

The supported component-based lifecycle is:

agent.reset(trial=trial)
agent.observe(observation, time_step=t)
agent.infer_states()
agent.infer_policies()
action = agent.select_action()
agent.learn()  # only when parameter learning is enabled

The older matrix-heavy constructor and methods such as choose_action() and perform_learning() remain available while examples migrate, but new projects should use the component constructor and lifecycle above.

Factor-dependent transitions

A factor's transition may depend on other factors. For example, a human partner's next target can depend on their current target and a hidden capacity factor:

model = GenerativeModel(
    B=B,  # B[1].shape == (targets, targets, capacities, 1)
    D=D,
    controls_dim=[1, 1],
    controllable_factors=[0],
    B_dependencies=[[0], [1, 0]],
)

See Model shapes.

Expected free energy and policy costs

Free energies are reported as negative energies that the agent maximises (larger is better), like the variational free energy. agent.last_policy_inference reports expected_free_energy and its terms expected_utility, state_information_gain, parameter_information_gain and policy_cost. A domain-specific cost can be subtracted from every policy:

def human_effort(agent, policy_index, expected_states):
    return 0.5 * float(expected_states[0][1])

agent = ActiveInfAgent(..., policy_cost_fn=human_effort)

Model structure

  • GenerativeModel owns state transitions (B), initial-state priors (D), control dimensions, controllable factors, and optional policies.
  • CategoricalLikelihood owns observation likelihoods (A), preferences (C), and modality-to-factor dependencies.
  • ContinuousLikelihood owns density evaluation, observation grids, log preferences, dependencies, and optional domain learning hooks.
  • ShallowInference performs single-step factorised inference.
  • DeepTemporalInference performs marginal message passing over a fixed window.
  • RecedingHorizonInference replans a full window at every observation and uses policy-conditioned marginal message passing, including backward future-to-present messages.
  • FilteredRecedingHorizonInference filters the current state once, then rolls that shared posterior forward under every policy. This estimator/planner separation is usually the more natural default for online robotics. It carries the shared current posterior directly and skips policy-averaged future-state diagnostics by default; set average_future_states=True when those diagnostics are needed. Both receding modes select the first action and support uninterrupted control. Horizon 3 means the current state plus two future states. See the lifecycle and migration notes and the runnable temporal-message-passing and filter-then-plan examples. Parameter learning is not yet supported in either receding mode.
  • PyAIF.learning contains reusable categorical updates for A, B, C, D, and E.

See Model shapes and Public API for details.

Examples

  • examples/quickstart_discrete.py: minimal categorical agent.
  • examples/quickstart_continuous.py: minimal Gaussian continuous-observation agent.
  • examples/quickstart_receding.py: receding-horizon temporal message passing.
  • examples/quickstart_filtered_receding.py: robotics-style current-state filtering followed by predictive policy rollout.
  • examples/learning_under_uncertainty/: deep temporal parameter-learning experiments under epistemic and aleatoric uncertainty.

Model-selection and bounded-rationality experiments are maintained separately from the reusable PyAIF package.

The automated regression suite covers the reusable component API and executes one trial of each parameter-learning experiment.

Run the minimal component examples with:

python examples/quickstart_discrete.py
python examples/quickstart_continuous.py

Version policy

  • 0.1.x: categorical observations and discrete parameter learning.
  • 0.2.x: continuous-observation likelihood components for discrete hidden states.

Behavioral changes are protected with numerical regression tests. Research experiments may evolve independently from the packaged API.

License

PyAIF is distributed under the BSD 3-Clause License. Copyright © 2026 Diluna A. Warnakulasuriya.

This software license does not automatically apply to papers, datasets, figures, trained models, or other research artifacts.

Development

ruff check PyAIF tests
ruff format --check PyAIF tests
pytest
python -m build
python -m twine check dist/*

See CONTRIBUTING.md for the pull-request and release process.

Metadata

Release files for pyaif-toolkit 0.5.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 pyaif-toolkit 0.5.0
File Size Uploaded Provenance Actions
pyaif_toolkit-0.5.0.tar.gz 1.2 MB With provenance Details for pyaif_toolkit-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyaif-toolkit 0.5.0
File Interpreter ABI Platform Actions
pyaif_toolkit-0.5.0-py3-none-any.whl Python 3 none any Details for pyaif_toolkit-0.5.0-py3-none-any.whl

Total release size: 1.2 MB

Release files / pyaif_toolkit-0.5.0.tar.gz

Download URL pyaif_toolkit-0.5.0.tar.gz
Size 1.2 MB
Tags Source
SHA-256 checksum
How to use checksums
21860b75aa7d0528d485da24f5778ba685d2b1181851933df10bc559a7f914b6
BLAKE2b-256 checksum
How to use checksums
7ee4c73b4475a4f0cd05d17b732b9d067b0b716752d53a8a9ed5ea057e8d95bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.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 Nov 11, 2026.

Transparency log

Release files / pyaif_toolkit-0.5.0-py3-none-any.whl

Download URL pyaif_toolkit-0.5.0-py3-none-any.whl
Size 70.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e0a045e45fb6488a107aaa64b5a46a363994d8da5b2bd52b1d37202f519d0a9d
BLAKE2b-256 checksum
How to use checksums
838cc04735790df8140a8e5f6bab5947ef05525caeb0abcac0a01ae22c1e1baf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.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 Nov 11, 2026.

Transparency log

Security for pyaif-toolkit 0.5.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 pyaif_toolkit-0.5.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 11, 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