IDEAS home Printed from https://ideas.repec.org/a/plo/pcbi00/1014340.html

pyhgf: A neural network library for predictive coding

Author

Listed:
  • Nicolas Legrand
  • Lilian Weber
  • Peter Thestrup Waade
  • Anna Hedvig Møller Daugaard
  • Mojtaba Khodadadi
  • Nace Mikuš
  • Christoph Mathys

Abstract

Bayesian models of cognition have gained considerable traction in computational neuroscience and psychiatry. Their scopes are 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 optimization algorithms and the systems being optimized. 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 optimization workflows. Additionally, we offer generalized 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.Author summary: Many theories of brain function propose that perception and learning arise from Bayesian inference, in which the brain continuously updates beliefs about the causes of sensory inputs. One influential framework implementing this idea is predictive coding, where hierarchical networks exchange predictions and prediction errors to update internal beliefs. These models have been widely used to study learning, decision-making, and psychiatric conditions. However, applying predictive coding to complex models or large datasets remains difficult because existing tools are often specialized, inflexible, or poorly integrated with modern machine-learning and probabilistic programming frameworks.

Suggested Citation

  • Nicolas Legrand & Lilian Weber & Peter Thestrup Waade & Anna Hedvig Møller Daugaard & Mojtaba Khodadadi & Nace Mikuš & Christoph Mathys, 2026. "pyhgf: A neural network library for predictive coding," PLOS Computational Biology, Public Library of Science, vol. 22(6), pages 1-18, June.
  • Handle: RePEc:plo:pcbi00:1014340
    DOI: 10.1371/journal.pcbi.1014340
    as

    Download full text from publisher

    File URL: https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1014340
    Download Restriction: no

    File URL: https://journals.plos.org/ploscompbiol/article/file?id=10.1371/journal.pcbi.1014340&type=printable
    Download Restriction: no

    File URL: https://libkey.io/10.1371/journal.pcbi.1014340?utm_source=ideas
    LibKey link: if access is restricted and if your library uses this service, LibKey will redirect you to where you can use your library subscription to access this item
    ---><---

    More about this item

    Statistics

    Access and download statistics

    Corrections

    All material on this site has been provided by the respective publishers and authors. You can help correct errors and omissions. When requesting a correction, please mention this item's handle: RePEc:plo:pcbi00:1014340. See general information about how to correct material in RePEc.

    If you have authored this item and are not yet registered with RePEc, we encourage you to do it here. This allows to link your profile to this item. It also allows you to accept potential citations to this item that we are uncertain about.

    We have no bibliographic references for this item. You can help adding them by using this form .

    If you know of missing items citing this one, you can help us creating those links by adding the relevant references in the same way as above, for each refering item. If you are a registered author of this item, you may also want to check the "citations" tab in your RePEc Author Service profile, as there may be some citations waiting for confirmation.

    For technical questions regarding this item, or to correct its authors, title, abstract, bibliographic or download information, contact: ploscompbiol (email available below). General contact details of provider: https://journals.plos.org/ploscompbiol/ .

    Please note that corrections may take a couple of weeks to filter through the various RePEc services.

    IDEAS is a RePEc service. RePEc uses bibliographic data supplied by the respective publishers.