<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Predictive Coding | Mahyar's world 🌏</title><link>https://mahyar-osanlouy.com/tag/predictive-coding/</link><atom:link href="https://mahyar-osanlouy.com/tag/predictive-coding/index.xml" rel="self" type="application/rss+xml"/><description>Predictive Coding</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><lastBuildDate>Fri, 15 Nov 2024 00:00:00 +0000</lastBuildDate><image><url>https://mahyar-osanlouy.com/media/icon_hu35e4e9c9135f02752aab27d124db531b_75212_512x512_fill_lanczos_center_3.png</url><title>Predictive Coding</title><link>https://mahyar-osanlouy.com/tag/predictive-coding/</link></image><item><title>Temporal Predictive Coding: A New Framework for Neural Processing of Dynamic Stimuli</title><link>https://mahyar-osanlouy.com/post/temporal-predictive-coding/</link><pubDate>Fri, 15 Nov 2024 00:00:00 +0000</pubDate><guid>https://mahyar-osanlouy.com/post/temporal-predictive-coding/</guid><description>&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>One of the most fascinating aspects of the brain is its ability to process and predict dynamic sensory inputs that
continuously change over time. From tracking a moving object to predicting the next note in a melody, our brains are
remarkably adept at temporal prediction. In a recent paper published in PLOS Computational Biology titled
&amp;ldquo;Predictive coding networks for temporal prediction,&amp;rdquo; my colleagues and I proposed a new computational framework that
may help explain how the brain accomplishes this feat.&lt;/p>
&lt;p>Predictive coding has emerged as an influential theoretical model for understanding cortical function. The core idea
is deceptively simple: the brain constantly generates predictions of incoming sensory inputs and compares these
predictions with actual sensory data. Any mismatch results in prediction errors that drive learning and perceptual
processing. This framework has successfully explained many neural phenomena and receptive field properties in visual cortex.&lt;/p>
&lt;p>However, most previous predictive coding models have focused on static inputs, neglecting the temporal dimension that
is crucial for real-world perception. Our work addresses this gap by extending predictive coding to the temporal domain
while maintaining its elegant biological implementation.&lt;/p>
&lt;h2 id="the-temporal-predictive-coding-model">The Temporal Predictive Coding Model&lt;/h2>
&lt;h3 id="generative-model-and-free-energy">Generative Model and Free Energy&lt;/h3>
&lt;p>At the foundation of our temporal predictive coding (tPC) model is a Hidden Markov Model (HMM) structure,
which assumes that observations are generated by hidden states that evolve according to a Markov process.
Mathematically, we can express this generative model as:&lt;/p>
&lt;p>$$
x_k = A f(x_{k-1}) + B u_k + \omega_x
$$&lt;/p>
&lt;p>$$
y_k = C f(x_k) + \omega_y
$$&lt;/p>
&lt;p>Where:&lt;/p>
&lt;ul>
&lt;li>$x_{k}$ is the hidden state at time $k$.&lt;/li>
&lt;li>$y_{k}$ is the observed sensory input at time $k$.&lt;/li>
&lt;li>$u_{k}$ is the control input at time $k$.&lt;/li>
&lt;li>$A$ is the dynamics matrix governing state transitions.&lt;/li>
&lt;li>$B$ is the control matrix.&lt;/li>
&lt;li>$C$ is the observation matrix.&lt;/li>
&lt;li>$f$ is a potentially nonlinear function.&lt;/li>
&lt;li>$\omega_x$ and $\omega_y$ are Gaussian process and observation noise.&lt;/li>
&lt;/ul>
&lt;p>The goal is to infer the current hidden state $x_{k}$ given the current observation $y_{k}$ and previous state estimate
$y_{1:k-1}$. To achieve this, we formulate a variational free energy objective:&lt;/p>
&lt;p>$$
\mathcal{F}&lt;em>k = \frac{1}{2}(y_k - C f(x_k))^T \Sigma_y^{-1} (y_k - C f(x_k)) + \frac{1}{2}(x_k - A f(\hat{x}&lt;/em>{k-1}) - B u_k)^T \Sigma_x^{-1} (x_k - A f(\hat{x}_{k-1}) - B u_k)
$$&lt;/p>
&lt;p>This free energy can be understood as the sum of two weighted prediction errors:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Sensory prediction errors&lt;/strong>: The difference between observed and predicted sensory inputs $y_k - Cf(x_k)$.&lt;/li>
&lt;li>&lt;strong>Temporal prediction errors&lt;/strong>: The difference between the current state and the prediction from the previous state
$x_k - Af(\hat{x}_{k-1}) - Bu_k$.&lt;/li>
&lt;/ol>
&lt;p>Each prediction error is weighted by the precision (inverse variance) of the corresponding noise distribution,
ensuring that more reliable predictions carry more weight.&lt;/p>
&lt;h2 id="neural-implementation">Neural Implementation&lt;/h2>
&lt;p>A crucial contribution of our work is showing how temporal predictive coding can be implemented in neural circuits
using biologically plausible mechanisms. The neural dynamics for inferring the hidden state follow gradient descent
on the free energy:&lt;/p>
&lt;p>$$
\tau \frac{d x_k}{d t} = -\epsilon_x + f'(x_k) \odot C^T \epsilon_y
$$&lt;/p>
&lt;p>Where $\epsilon_x$ and $\epsilon_y$ are precision-weighted prediction errors:&lt;/p>
&lt;p>$$
\epsilon_y = \Sigma_y^{-1} \left( y_k - C f(x_k) \right)
$$&lt;/p>
&lt;p>$$
\epsilon_x = \Sigma_x^{-1} \left( x_k - A f(\hat{x}_{k-1}) - B u_k \right)
$$&lt;/p>
&lt;h3 id="we-proposed-multiple-neural-circuit-implementations-of-this-model">We proposed multiple neural circuit implementations of this model:&lt;/h3>
&lt;ol>
&lt;li>Network with explicit prediction error neurons: Where dedicated neurons represent prediction errors at each level of processing&lt;/li>
&lt;li>Dendritic computing implementation: Where prediction errors are computed as differences between somatic and dendritic potentials&lt;/li>
&lt;li>Single-iteration implementation: A simplified version that performs single updates per time step&lt;/li>
&lt;/ol>
&lt;h3 id="neural-circuit-implementation">Neural circuit implementation&lt;/h3>
&lt;p>Importantly, all of these implementations rely on local information and Hebbian plasticity.
The synaptic weights are updated according to:&lt;/p>
&lt;p>$$
\Delta A = \eta \epsilon_x f(\hat{x}_{k-1})^T
$$&lt;/p>
&lt;p>$$
\Delta B = \eta \epsilon_x u_k^T
$$&lt;/p>
&lt;p>$$
\Delta C = \eta \epsilon_y f(x_k)^T
$$&lt;/p>
&lt;p>These update rules are Hebbian in nature because they depend only on the activities of pre- and post-synaptic neurons,
making them biologically plausible.&lt;/p>
&lt;h2 id="relationship-to-kalman-filtering">Relationship to Kalman Filtering&lt;/h2>
&lt;p>An intriguing property of our model is its relationship to the Kalman filter, which is the optimal solution for
linear Gaussian filtering problems. We demonstrated that both Kalman filtering and temporal predictive coding can be
derived as special cases of Bayesian filtering, with the key difference being how they handle uncertainty.&lt;/p>
&lt;p>The Kalman filter propagates uncertainty estimates through time, tracking the full posterior covariance at each step.
In contrast, tPC approximates this by assuming a point estimate (Dirac distribution) for the previous state. Despite
this simplification, our tPC model achieves comparable performance to the Kalman filter in tracking tasks while being
computationally simpler and more biologically plausible.&lt;/p>
&lt;p>For linear systems, the tPC dynamics at equilibrium yield:&lt;/p>
&lt;p>$$
\hat{x}&lt;em>k^- = A\hat{x}&lt;/em>{k-1} + Bu_k
$$&lt;/p>
&lt;p>$$
\hat{x}_k = \hat{x}_k^- + K(y_k - C\hat{x}_k^-)
$$&lt;/p>
&lt;p>$$
K = \Sigma_x C^T \left[C \Sigma_x C^T + \Sigma_y \right]^{-1}
$$&lt;/p>
&lt;p>This resembles the Kalman filter update equations but with a fixed gain matrix $K$ rather than a dynamically updated one
based on posterior uncertainty.&lt;/p>
&lt;h2 id="experimental-results">Experimental Results&lt;/h2>
&lt;h3 id="performance-in-linear-filtering-tasks">Performance in Linear Filtering Tasks&lt;/h3>
&lt;p>We tested our model on classic tracking problems, where the goal is to infer the hidden state
(position, velocity, acceleration) of an object undergoing unknown acceleration based on noisy observations.
Even with just a few inference steps between observations, tPC achieved performance approaching that of the
optimal Kalman filter.&lt;/p>
&lt;p>A key advantage of our model is its ability to learn the parameters of the generative model (matrices $A$, $B$, and $C$)
using Hebbian plasticity. Even when starting with random matrices, tPC could learn to accurately predict observations.
Interestingly, the model also implicitly encoded noise covariance information in its recurrent connections, without
needing explicit representation of precision matrices.&lt;/p>
&lt;h3 id="motion-sensitive-receptive-fields">Motion-Sensitive Receptive Fields&lt;/h3>
&lt;p>Perhaps most excitingly, when trained on natural movies, our tPC model developed spatiotemporal receptive fields
resembling those observed in the visual cortex. These fields exhibited Gabor-like patterns and direction selectivity,
a hallmark of motion-sensitive neurons in early visual areas.&lt;/p>
&lt;h3 id="nonlinear-extensions">Nonlinear Extensions&lt;/h3>
&lt;p>We extended the model to handle nonlinear dynamics by incorporating nonlinear activation functions. When tested on a
simulated pendulum task, the nonlinear tPC significantly outperformed the linear model, accurately predicting the
pendulum&amp;rsquo;s motion even at extreme angles where nonlinear effects are strongest.&lt;/p>
&lt;h2 id="implications-and-future-directions">Implications and Future Directions&lt;/h2>
&lt;p>Our temporal predictive coding framework has several important implications:&lt;/p>
&lt;ol>
&lt;li>It provides a biologically plausible explanation for how the brain processes dynamic stimuli and performs temporal predictions.&lt;/li>
&lt;li>It demonstrates that complex temporal filtering operations can be implemented in neural circuits using simple, local computations.&lt;/li>
&lt;li>It offers a unified framework that connects normative theories of perception (Bayesian inference) with mechanistic models of neural circuits.&lt;/li>
&lt;li>It suggests that the same computational principles might underlie both static and dynamic sensory processing in the brain.&lt;/li>
&lt;/ol>
&lt;h2 id="conclusion">Conclusion&lt;/h2>
&lt;p>The temporal predictive coding model we&amp;rsquo;ve developed bridges an important gap in our understanding of how the brain
processes dynamic sensory inputs. By extending predictive coding to the temporal domain while maintaining its biological
plausibility, our model provides a compelling computational mechanism for temporal prediction in neural circuits.&lt;/p>
&lt;p>The fact that our model develops receptive fields resembling those in the visual cortex and approximates optimal
filtering solutions suggests that temporal predictive coding may indeed capture fundamental principles of neural
computation in the brain. As we continue to refine these models and test them against empirical data, we hope to
gain deeper insights into the remarkable predictive capabilities of the brain.&lt;/p>
&lt;p>&lt;em>This blog is based on the paper
&amp;ldquo;Predictive coding networks for temporal prediction&amp;rdquo; by Beren Millidge, Mufeng Tang, Mahyar Osanlouy, Nicol S. Harper,
and Rafal Bogacz, published in PLOS Computational Biology, April 2024.&lt;/em> &lt;a href="https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1011183" target="_blank" rel="noopener">Link to the paper&lt;/a>&lt;/p></description></item><item><title>Generalization of predictive coding model to dynamic stimuli</title><link>https://mahyar-osanlouy.com/post/tpc/</link><pubDate>Mon, 26 Apr 2021 00:00:00 +0000</pubDate><guid>https://mahyar-osanlouy.com/post/tpc/</guid><description>&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>Predictive coding is an established model of perceptual inference and learning in hierarchical networks of the brain.
It describes a network of neuron-like nodes, which can infer stimulus properties from noisy input using only
&lt;em>local computation&lt;/em>, i.e. the changes of activity of each neuron in the model is determined only by its inputs and its
current activity levels. Furthermore, the network encodes the estimated parameters of a probabilistic model from which
the stimuli are generated in its synaptic connections, and learn these parameters employing only
&lt;em>local plasticity&lt;/em>, where the changes in synaptic weights only depend of activities of pre and post-synaptic neurons.
In its original form the predictive coding model assumes static input stimuli. However, most of
the stimuli experienced by animals and humans change in time, and it is critical for survival to efficiently interpret
such stimuli.&lt;/p>
&lt;p>Very soon after developing the predictive coding model, it was pointed that it could be generalized to dynamic stimuli,
and the Kalman filter could be employed to infer the states of hidden variables represented by the model. However that work has not described how such computation could be implemented in a biologically
plausible network of neuron-like nodes. More recently, a generalization of predictive coding to dynamic stimuli has been
proposed, in which different neurons represent not only the hidden variables, but also their temporal derivatives.
Although it is possible to implement this model in a network only employing local computation and local plasticity,
this network requires a very intricate and specific pattern of connectivity between various neurons, and there is no evidence that such connectivity exists in cortical circuits.&lt;/p>
&lt;p>This report outlines a simple generalization of predictive coding model to dynamic stimuli, which does not require more
intricate network than the original predictive coding model. A simulation of the proposed generalizations is shown for
a toy problem, and directions are suggested in which the work on the model needs to be conducted.&lt;/p>
&lt;h2 id="model">Model&lt;/h2>
&lt;h3 id="process-generating-stimuli">Process generating stimuli&lt;/h3>
&lt;p>In this report we assume that stimuli are generated from a very simple linear model, which parallels the assumptions
about signal made by the Kalman filter. Let us denote an observed stimulus at time
$t$ by a vector with elements $y_i(t)$. Let us assume that the stimulus depends on values of hidden variables
denoted by $x_j(t)$ according to:&lt;/p>
&lt;p>$$
y_i(t) = \sum_j w_{i,j} x_j(t) + \epsilon_{y,i}(t)
\label{eq:gen_y}
$$&lt;/p>
&lt;p>In the above equation, $w_{i,j}$ form a matrix of parameters, and $\epsilon_{y,i}(t)$ is a noise process (with zero mean).
Furthermore, let us assume that the hidden variables evolve according to:&lt;/p>
&lt;p>$$
\dot{x}&lt;em>j = \sum_k v&lt;/em>{j,k} x_k(t) + \epsilon_{x,j}(t)
\label{eq:gen_x}
$$&lt;/p>
&lt;p>Analogously as above, $v_{j,k}$ form a matrix of parameters, and $\epsilon_{x,j}(t)$ is a noise process.
A natural way for estimating $x_j$ from $y_i$ is to employ the Kalman filter, but it involves complex equations,
and it is not clear how such computation could be implemented in a network of neurons. Therefore, this report describes
a simpler method for estimating $x_j$ that has a more natural neural implementation.&lt;/p>
&lt;h3 id="computations-in-the-model">Computations in the model&lt;/h3>
&lt;p>Given a observed stimuli $y_i$, we will seek to infer the hidden variables $x_j$ and estimate the parameters $w_{i,j}$
and $v_{j,k}$. In the reminder of Section 2, we will use $x_j$, $w_{i,j}$ and $v_{j,k}$ to denote the estimates of
corresponding terms in Equations \ref{eq:gen_y}. We wish to find $x_j$ such that the stimulus $y_i$ is
close to the predicted value $\sum_j w_{i,j} x_j$. Thus we define error in prediction
of $y_i$ as:&lt;/p>
&lt;p>$$
e_i = y_i - \sum_j w_{i,j} x_j
\label{eq:error_y}
$$&lt;/p>
&lt;p>We wish to minimize a squared sum of these errors which we denote by $E_y = \frac{1}{2} \sum_i \varepsilon_{y,i}^2$.
Hence we change $x_j$ in the direction opposite to the gradient of $E_y$, but we additionally append this dynamics
towards our goal with the natural evolution of $x_j$:&lt;/p>
&lt;p>$$
\dot{x}&lt;em>j = - \frac{\partial E_y}{\partial x_j} + \sum_k v&lt;/em>{j,k} x_k
$$&lt;/p>
&lt;p>Evaluating the gradient, we obtain the equation describing the dynamics of our estimate of hidden variables:&lt;/p>
&lt;p>$$
\dot{x}&lt;em>j = \sum_i w&lt;/em>{i,j} \varepsilon_{y,i} + \sum_k v_{j,k} x_k
$$&lt;/p>
&lt;p>In order to learn parameters $w_{i,j}$, which describe how $y_i$ depends on $x_j$, we modify them to minimize $E_y$:&lt;/p>
&lt;p>$$
\dot{w}&lt;em>{i,j} = - \alpha \frac{\partial E_y}{\partial w&lt;/em>{i,j}} = \alpha \varepsilon_{y,i} x_j
$$&lt;/p>
&lt;p>In the above equation $\alpha$ denotes a learning rate. In order to learn parameters $v_{j,k}$ describing the natural
dynamics of hidden variables, we need to define an error in prediction of this dynamics:&lt;/p>
&lt;p>$$
\varepsilon_{x,j} = \dot{x}&lt;em>j - \sum_k v&lt;/em>{j,k} x_k
$$&lt;/p>
&lt;p>We wish to minimize squared sum of these errors $E_x = \frac{1}{2} \sum_j \varepsilon_{x,j}^2$,
and hence we modify the weights in the direction opposite to the gradient of $E_x$ over $v_{j,k}$:&lt;/p>
&lt;p>$$
\dot{v}&lt;em>{j,k} = \alpha \varepsilon&lt;/em>{x,j} x_k
$$&lt;/p>
&lt;p>In summary, this generalized predictive coding model continuously updates hidden variables and parameters according and
recomputes prediction errors.&lt;/p>
&lt;h3 id="possible-neural-implementations">Possible neural implementations&lt;/h3>
&lt;p>Inference of hidden variables $x_j$ from sensory input $y_i$ can be easily performed in a network shown in Figure 1A.
The bottom layer consists of sensory neurons representing the stimulus. They project to neurons computing prediction
error. These errors are then send to the neurons encoding hidden variables which
change their activity according to Equation \ref{eq:dot_x}. The weights of connections between neurons encoding errors
and hidden variables are symmetric, i.e. equal in both direction. This network has an architecture very similar to
a standard predictive coding model , but additionally includes recurrent connections between the
neurons encoding hidden variables with weights $v_{j,k}$.&lt;/p>
&lt;img src="featured.png" alt="Receptive fields" width="800">
&lt;p>Learning parameters $w_{i,j}$ corresponds to local Hebbian plasticity in the network
of Figure 1, analogously as in the standard predictive coding networks. However,
learning parameters $v_{j,k}$ is less straightforward because the
prediction error $\varepsilon_{x,j}$ is not explicitly represented in activity of any neurons in the network.
Nevertheless, it is possible to construct models in which $\varepsilon_{x,j}$ would be represented in internal
signals (e.g. concentrations of particular ions or proteins) within neurons encoding $x_j$,
and let us consider two such possible models.&lt;/p>
&lt;p>The first model is illustrated in Figure 1B.
In this network, the recurrent inputs from neurons representing hidden variables converge on a separate dendritic
branch, which sums them and thus can compute $\sum_k v_{j,k} x_k$. To compute the error $\varepsilon_{x,j}$,
the neuron would need to compute the difference between change in its activity and the membrane potential in the dendrite.
Since both of these quantities are encoded within the same neuron, it is plausible that such a computation may be performed,
and an error encoded in an internal signal. Such signal could then drive local synaptic plasticity.&lt;/p>
&lt;p>An alternative way of computing prediction errors $\varepsilon_{x,j}$ relies on an observation that by combining
equations describing the dynamics of $\dot{x}&lt;em>j$ adn error $\varepsilon&lt;/em>{x,j}$, we see that these errors are equal to:&lt;/p>
&lt;p>$$
\varepsilon_{x,j} = \sum_i w_{i,j} \varepsilon_{y,i}
$$&lt;/p>
&lt;p>Such input from the previous layer of prediction error neurons could be computed in dendrites shown in
Figure 1C. The membrane potential of such dendrite would need to set level of an internal signal that would govern the
plasticity within the entire neuron. This mechanism could be considered biologically plausible as it is analogous to
observations that high membrane potential of apical dendrites of pyramidal neurons triggers plateau potentials via
calcium influx, leading to a burst of spikes by the neuron. Such bursts of spikes may subsequently
induce synaptic plasticity.&lt;/p>
&lt;h2 id="results">Results&lt;/h2>
&lt;p>I tested the model on a simple problem in which hidden variables and stimuli were 2-dimensional.
The hidden variables were generated according to $\dot{x}&lt;em>j = \sum_k v&lt;/em>{j,k} x_k(t) + \epsilon_{x,j}(t)$ with parameters
$v_{j,k}$ set to a rotation matrix visualized in Figure 2C. The stimuli were generated according
to $y_i(t) = \sum_j w_{i,j} x_j(t) + \epsilon_{y,i}(t)$ with parameters $w_{i,j}$ set to the identity matrix,
so that the stimuli were simply noisy versions of the hidden variables. The stimuli are shown in Figure 2A, and they are
noisy periodic signal because parameters $v_{j,k}$ were set to a rotation matrix. The variables and stimuli were generated
with a sampling frequency 10, by solving our equations using Euler method with integration step $0.1$. During each step,
noise with variance of $0.01$ was added.&lt;/p>
&lt;img src="results.png" alt="Receptive fields" width="700">
&lt;p>At the start of the learning process, weights $w_{i,j}$ were initialized to an identity matrix,
while the weights between hidden units were all set to $v_{j,k}=0$. The hidden units were also initialized to $x_j=0$.
The hidden variables and parameters were updated according to our equations above using the Euler method with integration
step of $0.1$, and learning rate set to $\alpha=0.01$.&lt;/p>
&lt;p>Figure 2B shows that as the learning progressed, the error in prediction of stimuli decreased, so the network was able
to better predict the stimuli. Figure 2D visualizes learned values of parameters $v_{j,k}$, which are very close to the
original parameters used to generate the training data (cf. Figure 2C).
Thus the network was able to discover the underlying process generating the stimuli.&lt;/p>
&lt;h2 id="discussion">Discussion&lt;/h2>
&lt;p>This report outlines generalization of predictive coding to dynamic stimuli for linear and shallow generative models,
so more work would be required to extend this to more complex models and relate it with experimental data.
In particular the work can be extended in the following directions:&lt;/p>
&lt;ul>
&lt;li>Introduce the non-linear activation functions to hidden units, and test if the model can learn dynamics of non-linear systems.&lt;/li>
&lt;li>Introduce multiple levels of hierarchy and investigate if the model can extract dynamics of stimuli generated by hierarchical dynamical systems.&lt;/li>
&lt;li>Test the model performance on real world machine learning problems, e.g. prediction of EEG signal from past history.&lt;/li>
&lt;li>Investigate if after training with natural stimuli the receptive fields of neurons in the model have similar properties
to the receptive fields in the visual system, analogously as in neural networks trained with the back-propagation algorithm.&lt;/li>
&lt;/ul></description></item></channel></rss>