Skip to main content

Dynamic neural networks for predictive coding

Project description

pre-commit license codecov black mypy Imports: isort pip

PyHGF: A Neural Network Library for Predictive Coding

hgf

PyHGF is a Python library for creating and manipulating dynamic probabilistic networks for predictive coding. These networks approximate Bayesian inference by optimizing beliefs through the diffusion of predictions and precision-weighted prediction errors. The network structure remains flexible during message-passing steps, allowing for dynamic adjustments. They can be used as a biologically plausible cognitive model in computational neuroscience or as a generalization of Bayesian filtering for designing efficient, modular decision-making agents. The default implementation supports the generalized Hierarchical Gaussian Filters (gHGF, Weber et al., 2024), but the framework is designed to be adaptable to other algorithms. Built on top of JAX, the core functions are differentiable and JIT-compiled where applicable. The library is optimized for modularity and ease of use, allowing seamless integration with other libraries in the ecosystem for Bayesian inference and optimization. Additionally, a binding with an implementation in Rust is under active development, which will further enhance flexibility during inference. You can find the method paper describing the toolbox here and the method paper describing the gHGF, which is the main framework currently supported by the toolbox here.

Getting started

Installation

The last official release can be downloaded from PIP:

pip install pyhgf

The current version under development can be installed from the master branch of the GitHub folder:

pip install “git+https://github.com/ComputationalPsychiatry/pyhgf.git”

How does it work?

Dynamic networks can be defined as a tuple containing the following variables:

  • The attributes (dictionary) that store each node's states and parameters (e.g. value, precision, learning rates, volatility coupling, ...).
  • The edges (tuple) that lists, for each node, the indexes of the parents and children.
  • A set of update functions. An update function receive a network tuple and returns an updated network tuple.
  • An update sequence (tuple) that defines the order and target of the update functions.
networks

You can find a deeper introduction to how to create and manipulate networks under the following link:

The Generalized Hierarchical Gaussian Filter

Generalized Hierarchical Gaussian Filters (gHGF) are specific instances of dynamic networks where node encodes a Gaussian distribution that can inherit its value (mean) and volatility (variance) from other nodes. The presentation of a new observation at the lowest level of the hierarchy (i.e., the input node) triggers a recursive update of the nodes' belief (i.e., posterior distribution) through top-down predictions and bottom-up precision-weighted prediction errors. The resulting probabilistic network operates as a Bayesian filter, and a response function can parametrize actions/decisions given the current beliefs. By comparing those behaviours with actual outcomes, a surprise function can be optimized over a set of free parameters. The Hierarchical Gaussian Filter for binary and continuous inputs was first described in Mathys et al. (2011, 2014), and later implemented in the Matlab HGF Toolbox (part of TAPAS (Frässle et al. 2021).

You can find a deeper introduction on how does the gHGF works under the following link:

Model fitting

Here we demonstrate how to fit forwards a two-level binary Hierarchical Gaussian filter. The input time series are binary observations using an associative learning task Iglesias et al. (2013).

from pyhgf.model import Network
from pyhgf import load_data

# Load time series example data (observations, decisions)
u, y = load_data("binary")

# Create a two-level binary HGF from scratch
hgf = (
    Network()
    .add_nodes(kind="binary-state")
    .add_nodes(kind="continuous-state", value_children=0)
)

# add new observations
hgf.input_data(input_data=u)

# visualization of the belief trajectories
hgf.plot_trajectories();

png

from pyhgf.response import binary_softmax_inverse_temperature

# compute the model's surprise (-log(p)) 
# using the binary softmax with inverse temperature as the response model
surprise = hgf.surprise(
    response_function=binary_softmax_inverse_temperature,
    response_function_inputs=y,
    response_function_parameters=4.0
)
print(f"Sum of surprises = {surprise.sum()}")

Model's surprise = 138.8992462158203

Acknowledgments

This implementation of the Hierarchical Gaussian Filter was inspired by the original Matlab HGF Toolbox. A Julia implementation is also available here.

References

  1. Legrand, N., Weber, L., Waade, P. T., Daugaard, A. H. M., Khodadadi, M., Mikuš, N., & Mathys, C. (2024). pyhgf: A neural network library for predictive coding (Version 1). arXiv. https://doi.org/10.48550/ARXIV.2410.09206
  2. Mathys, C. (2011). A Bayesian foundation for individual learning under uncertainty. In Frontiers in Human Neuroscience (Vol. 5). Frontiers Media SA. https://doi.org/10.3389/fnhum.2011.00039
  3. Mathys, C. D., Lomakina, E. I., Daunizeau, J., Iglesias, S., Brodersen, K. H., Friston, K. J., & Stephan, K. E. (2014). Uncertainty in perception and the hierarchical Gaussian filter. Frontiers in Human Neuroscience, 8. https://doi.org/10.3389/fnhum.2014.00825
  4. Weber, L. A., Waade, P. T., Legrand, N., Møller, A. H., Stephan, K. E., & Mathys, C. (2023). The generalized Hierarchical Gaussian Filter (Version 2). arXiv. https://doi.org/10.48550/ARXIV.2305.10937
  5. Frässle, S., Aponte, E. A., Bollmann, S., Brodersen, K. H., Do, C. T., Harrison, O. K., Harrison, S. J., Heinzle, J., Iglesias, S., Kasper, L., Lomakina, E. I., Mathys, C., Müller-Schrader, M., Pereira, I., Petzschner, F. H., Raman, S., Schöbi, D., Toussaint, B., Weber, L. A., … Stephan, K. E. (2021). TAPAS: An Open-Source Software Package for Translational Neuromodeling and Computational Psychiatry. In Frontiers in Psychiatry (Vol. 12). Frontiers Media SA. https://doi.org/10.3389/fpsyt.2021.680811
  6. Iglesias, S., Kasper, L., Harrison, S. J., Manka, R., Mathys, C., & Stephan, K. E. (2021). Cholinergic and dopaminergic effects on prediction error and uncertainty responses during sensory associative learning. In NeuroImage (Vol. 226, p. 117590). Elsevier BV. https://doi.org/10.1016/j.neuroimage.2020.117590

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyhgf-0.2.8.tar.gz (22.7 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyhgf-0.2.8-cp37-abi3-win_amd64.whl (267.7 kB view details)

Uploaded CPython 3.7+Windows x86-64

pyhgf-0.2.8-cp37-abi3-win32.whl (261.0 kB view details)

Uploaded CPython 3.7+Windows x86

pyhgf-0.2.8-cp37-abi3-musllinux_1_2_x86_64.whl (537.6 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ x86-64

pyhgf-0.2.8-cp37-abi3-musllinux_1_2_i686.whl (568.5 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ i686

pyhgf-0.2.8-cp37-abi3-musllinux_1_2_armv7l.whl (625.3 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARMv7l

pyhgf-0.2.8-cp37-abi3-musllinux_1_2_aarch64.whl (531.0 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARM64

pyhgf-0.2.8-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (371.0 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ x86-64

pyhgf-0.2.8-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (397.7 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ s390x

pyhgf-0.2.8-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (495.1 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ppc64le

pyhgf-0.2.8-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (360.2 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARMv7l

pyhgf-0.2.8-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (349.5 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

pyhgf-0.2.8-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl (391.7 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.5+ i686

pyhgf-0.2.8-cp37-abi3-macosx_11_0_arm64.whl (333.9 kB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

pyhgf-0.2.8-cp37-abi3-macosx_10_12_x86_64.whl (348.3 kB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

Details for the file pyhgf-0.2.8.tar.gz.

File metadata

  • Download URL: pyhgf-0.2.8.tar.gz
  • Upload date:
  • Size: 22.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for pyhgf-0.2.8.tar.gz
Algorithm Hash digest
SHA256 c3650ad30b9aed08ff79e01ab0a6e5d1fef119640658176db1f243c7da85d564
MD5 0172cc9228557744efc416ea0eefa0ff
BLAKE2b-256 396940af4f3b9f0701a5a4cd78bdd356667e23b1d8b623daebf1c0c963d5b44b

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: pyhgf-0.2.8-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 267.7 kB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9e1a11e7e12aed8878b62401be355eef77621e926d420c25138f79348125fa78
MD5 21baa91b0d443bf81e5a9ffbc3aaa81e
BLAKE2b-256 aa2b6972cb1badfba4ce3afbe566046056ec359181558e1d8fab8c7c8deb6de1

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-win32.whl.

File metadata

  • Download URL: pyhgf-0.2.8-cp37-abi3-win32.whl
  • Upload date:
  • Size: 261.0 kB
  • Tags: CPython 3.7+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 ef7eda1584bc763cf79bfbe63e79694e11304bf6830867dfc7eb4b982c00a6c0
MD5 9759108543fe310540ef31e24b165094
BLAKE2b-256 4da873631b2f42f44d1a014da29e94aa25a460c478ff2815250cf5fd07d842b1

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b83ccef19af93498a58a4bba2e509e862afa1ab7b876dd9de373b1058cd85876
MD5 ced94c0fe7aa03c1cd51a873d37a7813
BLAKE2b-256 3e0fcbe2025b1f3498b92869ae7f235ecd12a6b6b8e686e57b9b637907605cfe

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 11e3a20675639972ee6eff35818cd3360f1a3c866d3516058c23e03e776e642f
MD5 0f379ef0964b59f973e266c0c6bf951d
BLAKE2b-256 f042576077b1bf514dbceedc4f705af034cd9ab24c3de1832e57349e2746d5d2

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 068ad3747af9badbbf43d57593c6197485aea59cacaf3384e3794509b4fe3f92
MD5 b99c07ba2be4f1c2bd9f03d2708053ce
BLAKE2b-256 6d5a309b38236f3fb6751115c65f40e8f42e4875441eff20a645c21555347ef4

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 c49bf1c132dcd89a80c573744ce59bbe99586afe8f2c69eb4cdfc5fd72af45af
MD5 8072c72aeda8abc4868723fccb7a1890
BLAKE2b-256 843f631e76d97c26f7a0653e813c826b83172458070e5e958b64e210a781922c

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2a34f8d83822961632deada5dc8ddd930a760caa864a9c9f2bbb8044713146de
MD5 eacdba395b46c27c10bd5a4b760f6d11
BLAKE2b-256 56ab8c3477b55514fd46aca79b6e19f2e09e2d7f0a5b470f29d720bf7359a292

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 80c98f78c31c986d379d3b73d3549bebabc6d9d9c04748a32aaf043ac0ba123e
MD5 2dd53e9874d9221555f808893c52059d
BLAKE2b-256 a50fba546691b8aae7d43688ecca25f5daed244e2aaeefcb730390f74a8beb12

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 eb7984493e94ab5a24f6b7806a336221608678fc69c99f5c7f4aa8f98e2a546d
MD5 e4b23be3b9fdf7eeec6ee16ba9eada1a
BLAKE2b-256 362af2e80ce54f8137ffcfd19b2e8a4ab90a4267900ed4427e9f9a253a05ec98

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 5b068ced7ee5f503aa5c9df1a9dafe29b6a2c578d1eb0737c2bdf9a66a03cf1f
MD5 99b05479e908e5713699b6938bc7e8f5
BLAKE2b-256 36df165385ebaf6be6b354ef034c67917c7d05085a495904fae671e87d7078f5

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7b5b5907036a0b44bcfe2988bc8d39949df7ec0c0deec71fa9cb678a4591f217
MD5 f26131d6d7786003e59d5d6dae6470c1
BLAKE2b-256 6651bda792248b37a1c222d75cedfcfaf80f3e729ea1a37daccc55d2aa9d701b

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 47b8afaaed72f457139222a24ff56e4cbad4d5776fb58fb6ea6b87eb95663679
MD5 3e17397054450370e509f30562e3b8ca
BLAKE2b-256 447b402c0368ac3b37ad2944abac553f27f1c01cd32978a4961ff14a41adfa2f

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4e67db888b3f4b57d0039f2b68278dfb63b347e713b84e952465918991cf72d5
MD5 706cade6603534586b026ab1070440d7
BLAKE2b-256 425a4e849ac123e990c24b4587f0af58ccb8be4126312a036d54cdf02e03fd10

See more details on using hashes here.

File details

Details for the file pyhgf-0.2.8-cp37-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyhgf-0.2.8-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3d2e99328654167dceb270987beb5c5208a1b2e2d0b3a7ed6090b0097ed17e72
MD5 2739c5bd9236ddf6bccf589ba2fca4e9
BLAKE2b-256 8ae7a53d6518b41c6306ce624abb0da3b66f7e48fe5790affe404a1c541163f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page