jpc : Flexible Inference for Predictive Coding Networks in JAX

Francesco Innocenti, Paul Kinghorn, Will Yun-Farmbrough, Miguel De Llanza Varona, Ryan Singh, Christopher L. Buckley · 2024

Read jpc : Flexible Inference for Predictive Coding Networks in JAX by Francesco Innocenti, Paul Kinghorn, Will Yun-Farmbrough, Miguel De Llanza Varona, Ryan Singh, Christopher L. Buckley (2024) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2412-03676.txt.

Introduction

Francesco Innocenti and five co-authors set out JPC, a library for training networks that learn by foretelling their own input, built on a fast code base. Their paper is from two thousand twenty-four.

Such networks, drawn from how the brain may work, learn by a flow of error through their layers. The library gives a plain, fast, and loose face for many forms: sorting, making, and mixed nets. In place of the plain step rule past libraries used, it solves the flow with true equation solvers. A second-order solver runs far faster, at like scores, across tasks and depths. The library adds theory tools to study such nets. The code is shared.

Catalog record

CREATORFrancesco Innocenti, Paul Kinghorn, Will Yun-Farmbrough, Miguel De Llanza Varona, Ryan Singh, Christopher L. Buckley
YEAR2024
COLLECTIONPAPERS
FORMATTXT
FILE SIZE17 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2412-03676

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

jpc : Flexible Inference for Predictive Coding Networks in JAX Francesco Innocenti, Paul Kinghorn, Will Yun-Farmbrough, Miguel De Llanza Varona, Ryan Singh, Christopher L. Buckley Abstract We introduce JPC , a J AX library for training neural networks with P redictive C oding. JPC provides a simple, fast and flexible interface to train a variety of PC networks (PCNs) including discriminative, generative and hybrid models. Unlike existing libraries, JPC leverages ordinary differential equation solvers to integrate the gradient flow inference dynamics of PCNs. We find that a second-order solver achieves significantly faster runtimes compared to standard Euler integration, with comparable performance on a range of datasets and network depths. JPC also provides some theoretical tools that can be used to study PCNs. We hope that JPC will facilitate future research of PC. The code is available at www.github.com/thebuckleylab/jpc 1 Introduction In recent years, predictive coding (PC) has been explored as a biologically plausible learning algorithm that can train deep neural networks as an alternative to backpropagation [ 14 , 8 , 7 , 12 ] . However, with a few recent notable exceptions [ 6 , 10 ] , there has been a lack of unified open-source implementations of PC networks (PCNs) which would facilitate research and reproducibility 1 1 1 We also acknowledge earlier libraries such as pypc and Torch2PC [ 11 ] . . In this short paper, we introduce JPC , a J AX library for training neural networks with PC . JPC provides a simple, fast and flexible API for training a variety of PCNs including discriminative, generative and hybrid models. Like JAX, JPC follows a fully functional paradigm close to the mathematics, and the core library is <1000 lines of code. Unlike existing implementations, JPC leverages ordinary differential equation solvers (ODE) to integrate the gradient flow inference dynamics of PCNs. JPC also provides some theoretical tools that can be used to study and potentially identify problems with PCNs. The rest of the paper is structured as follows. After a brief review of PC (§ 2 ), we showcase some empirical results showing that a second-order ODE solver can achieve significantly faster runtimes than standard Euler integration of the gradient flow PC inference dynamics, with comparable performance on different datasets and networks (§ 3 ). We then explain the library’s core implementation (§ 4 ), before concluding with possible extensions (§ 5 ). blabla 2 Predictive coding: A primer Here we include a minimal presentation of PC necessary to get started with JPC . The reader is referred to [ 14 , 8 , 7 , 12 ] for reviews and to [ 1 ] for a more formal treatment. PCNs are typically defined by an energy function which is a sum of squared prediction errors across layers, and which for a standard feedforward network takes the form \mathcal{F}=\sum_{\ell=1}^{L}||\mathbf{z}_{\ell}-f_{\ell}(\mathbf{W}_{\ell}\mathbf{z}_{\ell-1})||^{2} (1) where \mathbf{z}_{\ell} is the activity of a given layer and f_{\ell} is some activation function. We ignore multiple data points and biases for simplicity. To train a PCN, the last layer is clamped to some data, \mathbf{z}_{L}\coloneq\mathbf{y} . This could be a label for classification or an image for generation, and these two settings are typically referred to as discriminative and generative PC. The first layer can also be fixed to some data serving as a “prior”, \mathbf{z}_{0}\coloneq\mathbf{x} , such as an image in a supervised task. In unsupervised training, this layer is left free to vary like any other hidden layer. The energy (Eq. 1 ) is then minimised in a bi-level fashion, first w.r.t. the activities (inference) and then w.r.t. the weights (learning) \mathbf{x} \mathbf{y} \displaystyle\textit{Infer: }\argmin_{{\color[rgb]{1,0.5,0.5}\textstyle\mathbf{z}_{\ell}}}\mathcal{F} (2) \displaystyle\textit{Learn: }\argmin_{{\color[rgb]{0.5,0.5,1}\textstyle\mathbf{W}_{\ell}}}\mathcal{F} (3) The inference dynamics are generally first run to convergence until \Delta\mathbf{z}_{\ell}\approx 0 . Then, at the reached equilibrium of the activities, the weights are updated via common neural network optimisers such as stochastic GD or Adam (Eq. 3 ). This process is repeated for every training step, typically for a given data batch. Inference is typically performed by standard GD on the energy, which can be seen as the Euler discretisation of the gradient system \dot{\mathbf{z}_{\ell}}=-\partial\mathcal{F}/\partial\mathbf{z}_{\ell} . JPC simply leverages well-tested ODE solvers to integrate this gradient flow. 3 Runtime efficiency A comprehensive benchmarking of various types of PCN with GD as inference optimiser was recently performed by [ 10 ] . For this reason, here we focus on runtime efficiency, comparing standard Euler integration of the inference gradient flow dynamics with Heun, a second-order explicit Runge–Kutta method. Note that, as a second-order method, Heun has a higher computational cost than Euler; however, it could still be faster if it requires significantly fewer steps to converge. The solvers were compared on feedforward networks trained to classify standard image datasets, with different number of hidden layers H\in\{3,5,10\} . Because our goal was to specifically test for runtime, we trained each network for only one epoch across different step sizes dt\in\{5e^{-1},1e^{-1},5e^{-2}\} , retaining the run with the highest mean test accuracy achieved (see Figures 3 - 6 ). Unlike Euler, Heun employed a standard Proportional–Integral–Derivative step size controller. Therefore, to make comparison fair, we also trained networks across many upper integration limits T\in\{5,10,20,50,100,200,500\} , again retaining the run with the maximum accuracy (Figures 3 - 6 ). In cases where the accuracy difference between any T was not significantly different, we selected runs with the smaller T . Figure 1 shows that, despite requiring more computations at each step, Heun tends to converge significantly faster than Euler, and in general more so on deeper networks ( H=10 ). The convergence behaviour of Euler was more consistent during training across datasets and network depths, with Heun sometimes increasing in runtime. As a caveat, we also note that other optimiser-specific hyperparameters could lead to different results, and we welcome the community to test these and other solvers against other implementations as well as tasks. Figure 1: Second-order Runge–Kutta method (Heun) solves PC inference faster than standard Euler on a range of datasets and networks. We plot the wall-clock time of Euler and Heun at each training step of one epoch for networks with hidden layers H\in\{3,5,10\} trained on standard image classification datasets. The runs with the highest mean test accuracy achieved across different hyperparameters were selected (see Figures 3 - 6 ). The time of the first training iteration where “just-in-time” (jit) compilation occurs is excluded. All networks had 300 hidden units and Tanh as activation function, and were trained with learning rate 1e^{-3} and batch size 64 . Shaded regions indicate \pm 1 standard deviation across 3 different random weight initialisations. 4 Implementation JPC provides both a simple, high-level application programming interface (API) to train and test PCNs in a few lines of code (§ 4.1 ) and more advanced functions offering greater flexibility as well as additional features (§ 4.2 ). It is built on top of three main JAX libraries: • Equinox [ 5 ] , to define neural networks with PyTorch-like syntax, • Diffrax [ 4 ] , to leverage ODE solvers to integrate the gradient flow PC inference dynamics (Eq. 2 ), and • Optax [ 2 ] , for parameter optimisation (Eq. 3 ). Below we provide a sketch of JPC

Read the complete text