pyhgf: A neural network library for predictive coding
Open accessAbstract
Bayesian models of cognition have gained considerable traction in computational neuroscience and psychiatry. Their scope is now expected to expand rapidly to artificial intelligence, providing general inference frameworks to support embodied, adaptable, and energy-efficient autonomous agents. A central theory in this domain is predictive coding, which posits that learning and behaviour are driven by hierarchical probabilistic inferences about the causes of sensory inputs. Biological realism constrains these networks to rely on simple local computations in the form of precision-weighted predictions and prediction errors. This can make this framework highly efficient, but its implementation comes with unique challenges on the software development side. Embedding such models in standard neural network libraries often becomes limiting, as these libraries’ compilation and differentiation backends can force a conceptual separation between optimisation algorithms and the systems being optimised. This critically departs from other biological principles such as self-monitoring, self-organisation, cellular growth, and functional plasticity. In this paper, we introduce pyhgf: a Python package backed by JAX and Rust for creating, manipulating, and sampling dynamic networks for predictive coding. We improve over other frameworks by enclosing the network components as transparent, modular, and malleable variables in the message-passing steps. The resulting graphs can implement arbitrary algorithms as belief propagation. Moreover, the transparency of core variables can also translate into inference processes that leverage self-organisation principles and express structure learning, meta-learning, or causal discovery as the consequence of network structural adaptation to surprising inputs. The main functions of the library are differentiable and seamlessly integrate into sampling or optimisation workflows. Additionally, we offer generalised Bayesian filtering and the hierarchical Gaussian filter as key examples of dynamic networks implemented in our library. The source code, tutorials, and documentation are hosted under the main repository at https://github.com/ComputationalPsychiatry/pyhgf .
The claims and statistics of this paper haven’t been checked yet.
Run the check — free, no account needed. It reads the open-access text and checks the sources it cites.
Integrity signals
Registry
Cites
Statistics
Claims the paper makes
The claims and statistics of this paper haven’t been checked yet.
Run the check — free, no account needed. It reads the open-access text and checks the sources it cites.
Self-check
Percentages, sums and reported differences re-computed from the paper’s own numbers.
The self-check needs the paper’s full text.
Cites
Efficient Guided Generation for Large Language Models
Willard, B. T., & Louf, R. (2023) · arXiv · doi:10.48550/arxiv.2307.09702 · Cited by 27 · cited 2× · see citing works
“Overall, the results suggest good reliability of the inferred posterior density, as both empirical means were included in the 94% highest density interval (HDI) of the population estimates.”
Flagged first, then by citations
Cited by
Sorted by citations
Closed-form predictive coding via hierarchical Gaussian filters
Baskakovs, A., Estebe, S., Enevoldsen, K., Nielbo, K., Mathys, C., & Legrand, N. (2026) · arXiv · doi:10.48550/arxiv.2605.20293
“A recent generalization [Weber et al., 2026] and implementation [Legrand et al., 2024] extend this scheme to arbitrary networks of exponential-family distributions with hierarchical, state-dependent value and volatility coupling.”
InfluentialSelf-citation
- Authors
- Legrand, N., Weber, L., Waade, P. T., Møller Daugaard, A. H., Khodadadi, M., Mikuš, N., & Mathys, C.
- Affiliations
- Aarhus University (DK)
- University of Oxford (GB)
- Scuola Internazionale Superiore di Studi Avanzati (IT)
- Corresponding author
- Legrand, Nicolas
- Venue
- PLOS Computational Biology
- Publisher
- Public Library of Science (PLoS)
- Published
- 2026-06-22
- Volume
- 22
- Issue
- 6
- Pages
- e1014340
- ISSN
- 1553-734X (ISSN-L)
- 1553-7358
- PMC
- PMC13318038
- OpenAlex
- W4403564456
- Cited by
- 0
- References
- 1
- FWCI
- 0.00
- Type
- journal article
- Language
- en
- Update notices
- new_version (2026-06-30)
- Open access
- gold
- doi.org
- Licence
- Locations
- PLoS Computational Biology (publishedVersion, cc-by)
- PubMed (publishedVersion)
- PubMed Central (publishedVersion, cc-by)
- arXiv (Cornell University) (submittedVersion, cc-by)
- arXiv (Cornell University) (cc-by)
- Funders
- Aarhus Universitets Forskningsfond (AUFF-E-2019-7-10)
- Carlsbergfondet (CF21-0439)
- Topics
- Life Sciences › Neuroscience › Cognitive Neuroscience › Embodied and Extended Cognition
- Physical Sciences › Computer Science › Artificial Intelligence › Bayesian Modeling and Causal Inference
- Physical Sciences › Computer Science › Artificial Intelligence › Gaussian Processes and Bayesian Inference
- Keywords
- predictive coding, Bayesian models of cognition, message passing, belief propagation, differentiable programming, JAX, Rust, dynamic Bayesian networks, hierarchical Gaussian filter, Bayesian filtering, structure learning, meta-learning, causal discovery, self-organization, neural network libraries
- MeSH terms
- Algorithms, Bayes Theorem, Cognition, Humans, Models, Neurological, Software, Neural Networks, Computer, Computational Biology
About these checks
Registry, retraction, Cites and statistics results are computed, not judged, with one exception: a printed reference that no index lists is read once by a model before it counts as a citation error. Claims are extracted by a model and kept only when their quote appears verbatim in the text; each cited source is then checked, with a model’s help, for whether it says what the claim relies on. Those verdicts can be wrong — read the claim’s quote and the cited source before relying on one.
Statistics and arithmetic are recomputed, not judged.
Confidence bands are shown only for fields calibrated on our annotated test set. None is calibrated yet, so no band is shown.
What counts as a claim ›How we measure confidence ›How we check this ›
Are you an author, or do you see a problem with this page? Request a review