Inference and Action in an Infinite Recursive Loop: a Prototype Model of Self-Awareness?


Abstract

This post presents a simple yet powerful demonstration of active inference using a multilayer predictive coding neural network implemented in C. The model tracks a moving target following a circular trajectory by minimizing sensory prediction errors. Particular emphasis is placed on the role of multilayer generative architectures in encoding hierarchical latent causes and facilitating efficient inference and action.

Introduction

Biological systems maintain adaptive control over their bodies and environments by continuously predicting incoming sensory data and updating beliefs and actions to minimize discrepancies between expected and observed sensations. This principle — known as active inference — formalizes perception, learning, and action within a single free-energy minimization framework.
A popular computational substrate for active inference is predictive coding, where the brain is modeled as a hierarchy of interconnected neural populations. These populations exchange prediction errors and predictions through bottom-up (recognition of prediction errors) and top-down (generative of predictions) synaptic connections.
In this post, we implement a multilayer predictive coding model with active inference capabilities to track a moving point on a circular path. The system infers latent beliefs about the causes of sensory input and generates actions to realize predicted sensory states, effectively closing the perception-action loop.

Model Overview

We model an agent, that extends the two-neuron engaged in a recursive infinite regress loop, introduced in
. The model contains:
Two sensory inputs: neurons 0 and 1, in the figure below (e.g., representing retinal input from a moving target)
Two latent variables, corresponding to the motor_out signals in
: neurons 2 and 3 (encoding beliefs about causes of sensory data)
One higher-layer latent variable: neuron 4​(representing a shared latent cause, or higher abstraction)
Actions, corresponding to the proprio_input signals in
: a_x, a_y, not shown (moving the sensory frame to align predicted and actual input)
The multilayer architecture enables the agent to infer both immediate sensory causes and more abstract latent states that modulate those causes.
animated_IR_in_plotted_NN_20250624_120732.gif

Inference and Action

The figure above represents the exchange of information within the system, during each iteration.
During each iteration:
Discrepancies between actual and predicted sensory information are computed.
Prediction errors are communicated up through the network, serving to update connections and latent states.
Latent beliefs about the system’s world model, the actual position of the target, are represented by the new latent states of the neurons.
Updated latent states are communicated down through the network, generating new predictions.
Actions to track the target are set to realize predicted proprioceptive states, closing the perception-action loop, by aligning the sensory frame with predicted inputs.
The cycle resumes, in a non-terminating loop.

Multilayer Advantage

The inclusion of a higher-layer latent variable, neuron 4 above, serves several roles:
Hierarchical abstraction: encodes shared patterns or constraints between sensory dimensions.
Contextual modulation: higher-layer beliefs influence lower-layer inference via generative connections.
Improved generalization: facilitates learning higher-order structure (e.g., oscillatory behavior), rather than relations between isolated x and y axis positions.
Multilayer architectures allow the model to separate transient fluctuations from underlying causes, enhancing robustness and adaptability in the creation of a stable world-model.

Results

When tested on a moving target following a circular path, the agent successfully infers latent causes and tracks the target by adjusting actions. The prediction errors and free energy rapidly decrease as inference and learning proceed. Surprisal, a metric closely related to the Variational Free Energy (VFE), peaks when the path of the target unexpectedly changes:
simulation_metrics_Selfaware_20250626_065131.png
The higher-order latent belief, represented by the state of neuron 4, evolves towards an attractor indicative of oscillatory behavior, as the system’s tracking of the target stabilizes:
target_animated_2D_selfaware_20250628_153721.gif
A modification where actions directly realize predicted sensory states simplifies the control mechanism, analogous to proprioceptive reflex arcs in biological systems.

A Prototype Model of Self-Awareness?

The communication of information in an infinite recursive loop between agents (neurons), as demonstrated in the first figure above, ensures that each and every agent has access to its own state, at all times. This means that, each agent is, in a way, self-aware of itself.
To have an intuitive feeling of this process, it is enough that one follows the path of each dot, as it changes color between linear functions of states and prediction erros. The information paths always return to their points of origin.
The infinitely regressive awareness of agents’ states, as functions of themselves, is mathematically derived in the equations below:
eq 1.png
Equation (1), above, represents the state s of a generic neuron i at any layer l.
eq 2.png
Equation (2), represents the state s of any generic neuron i, as its state at the previous iteration t-1 incremented by its variation at the current iteration t.
eq 3.png
eq 4.png
From Equation (4), we see that, the predicted state of neurons at layer l is a function
text 4.png
of the states of neurons at the higher level layer l-1.
Since the states are updated according to prediction errors communicated up from lower level layers, weighted by bottom-up synapses v, and prediction errors are the difference between states and predicted states, it comes that the states’ increments of Equation (2) can be calculated by the following:
eq 5.png
From Equation (2), it comes that, at the limit when time increments approximate 0, the state of each neuron becomes a function of itself and its increment, as described in Equation (5). Combining Equations (2), (4) and (5), and rewriting in terms of functions f() and summations of f(), it comes:
eq 6.png
From Equation (6), it comes that, the state of any neuron in the network becomes a summation of functions
text 4.png
of itself and the states of all other neurons in the network, to which it is directly, or indirectly, connected. If there are N neurons, that translates to:
eq 7.png
Using the entire expression of Equation (7) in the place of the state within the summation, it comes:
eq 8.png
Equation (8) describes that, the state of any neuron emerges as an infinitely nested recursive function of itself. This infinitely regressive mathematical dependency is valid at any instant in time.
We believe that this process plays a fundamental role in explaining the emergence of self-awareness in biological organisms, humans included.
According to the formalism above, even systems of two agents, engaged in an infinite recursive loop of interactions, can be thought of presenting the conditions for emergence of a minimal level of self-awareness. Two-agent systems may include human beings interacting with their environment, or human beings interacting with LLMs. The subject is further explored in a interesting conversation with ChatGPT, the full transcript of which is included in the Appendix.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.