pyhgf: A neural network library for predictive coding
- 1Interacting Minds Centre, Aarhus University, Aarhus, Denmark
- 2Department of Psychiatry, University of Oxford, Oxford, United Kingdom
- 3Scuola Internazionale Superiore di Studi Avanzati (SISSA), Trieste, Italy
Abstract
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.
Related
- Predictive coding networks capture human neural representations missing in supervised DNNs
- Predictive coding unifies neural and behavioral signatures of contextual facilitation
- Brain network-driven predictive neural markers for suicide risk
- Predictive coding yields a brain-like code for shape from motion
- A neuro-inspired computational framework for AGI: predictive coding, active inference and free energy minimisation.
BibTeX
@article{legrand2026pyhgf,
title = {{pyhgf: A neural network library for predictive coding}},
author = {Nicolas Legrand and Lilian A. E. Weber and Peter Thestrup Waade and Anna Hedvig Møller and Mojtaba Khodadadi and Nace Mikuš and Christoph D. Mathys},
journal = {PLoS Computational Biology},
year = {2026},
month = jun,
doi = {10.1371/journal.pcbi.1014340},
url = {https://doi.org/10.1371/journal.pcbi.1014340},
note = {CC BY},
}
RIS
TY - JOUR
TI - pyhgf: A neural network library for predictive coding
AU - Nicolas Legrand
AU - Lilian A. E. Weber
AU - Peter Thestrup Waade
AU - Anna Hedvig Møller
AU - Mojtaba Khodadadi
AU - Nace Mikuš
AU - Christoph D. Mathys
PY - 2026
DA - 2026/06/22
JO - PLoS Computational Biology
DO - 10.1371/journal.pcbi.1014340
UR - https://doi.org/10.1371/journal.pcbi.1014340
AB - 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.
SN - 1553-734X
SN - 1553-7358
ER -