File size: 10,981 Bytes
32d978d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""
Predictive Coding — Hierarchical Prediction-Error Minimization

Implements Friston's Box 3: the canonical microcircuit for hierarchical
prediction error processing:
- Superficial Pyramidal (SG): prediction errors ε (bottom-up)
- Layer 4 (L4): state estimation
- Deep Pyramidal (IG): predictions μ (top-down)

Recognition dynamics: perception as gradient descent on free energy.
μ̇ = Dμ − ∂F/∂μ  (internal state update)
ε = observed − predicted  (prediction error)

Reference: Friston (2009) Box 3, Figure I
Author: Algorembrant, Rembrant Oyangoren Albeos (2026)
"""

import numpy as np
from typing import Optional


class CorticalColumn:
    """
    A single cortical column implementing the canonical microcircuit.
    
    Contains:
    - Superficial granular (SG): prediction error neurons
    - Layer 4 (L4): state representation neurons
    - Infragranular (IG): prediction neurons (deep pyramidal)
    """
    
    def __init__(self, size: int, level: int = 0):
        """
        Args:
            size: Number of units in this column.
            level: Hierarchical level (0 = lowest/sensory).
        """
        self.size = size
        self.level = level
        
        # State variables (generalized coordinates of motion)
        self.mu = np.zeros(size)          # Internal state estimates (expectations)
        self.mu_dot = np.zeros(size)      # Velocity of expectations
        self.epsilon = np.zeros(size)     # Prediction errors
        self.prediction = np.zeros(size)  # Top-down predictions to level below
        
        # Precision (inverse variance) — controls gain on prediction errors
        self.precision = np.ones(size)    # π = 1/σ² (higher = more confident)
        
        # Connection weights
        self.forward_weights = None   # Bottom-up: from level below
        self.backward_weights = None  # Top-down: to level below
        self.lateral_weights = None   # Within level
        
    def initialize_connections(self, input_size: int, output_size: Optional[int] = None):
        """Initialize synaptic weights for this column's connections."""
        self.forward_weights = np.random.randn(self.size, input_size) * 0.1
        if output_size is not None:
            self.backward_weights = np.random.randn(output_size, self.size) * 0.1
        self.lateral_weights = np.random.randn(self.size, self.size) * 0.01
        np.fill_diagonal(self.lateral_weights, 0)


class PredictiveCodingHierarchy:
    """
    Full hierarchical predictive coding network.
    
    Implements the hierarchical generative model from Friston Box 3:
    - Each level generates predictions for the level below
    - Prediction errors propagate upward (superficial pyramidal)
    - Predictions propagate downward (deep pyramidal)
    - Recognition dynamics minimize free energy via gradient descent
    
    This is NOT variational inference in the ML sense — this is 
    biological free-energy minimization via neural dynamics.
    """
    
    def __init__(self, layer_sizes: list[int], learning_rate: float = 0.01,
                 dt: float = 0.1, n_iterations: int = 10):
        """
        Args:
            layer_sizes: Sizes of each hierarchical level [sensory, ..., abstract].
            learning_rate: Step size for recognition dynamics.
            dt: Integration time step.
            n_iterations: Number of iterations per perception step.
        """
        self.n_levels = len(layer_sizes)
        self.learning_rate = learning_rate
        self.dt = dt
        self.n_iterations = n_iterations
        
        # Build cortical columns at each level
        self.columns: list[CorticalColumn] = []
        for i, size in enumerate(layer_sizes):
            col = CorticalColumn(size, level=i)
            self.columns.append(col)
        
        # Initialize inter-level connections
        for i in range(1, self.n_levels):
            self.columns[i].initialize_connections(
                input_size=layer_sizes[i-1],
                output_size=layer_sizes[i-1]
            )
        # Level 0 has lateral connections only
        self.columns[0].lateral_weights = np.random.randn(
            layer_sizes[0], layer_sizes[0]
        ) * 0.01
        np.fill_diagonal(self.columns[0].lateral_weights, 0)
    
    def _generate_prediction(self, level: int) -> np.ndarray:
        """
        Generate top-down prediction from level i to level i-1.
        
        g(μ⁽ⁱ⁾) — the generative model mapping from higher to lower.
        """
        col = self.columns[level]
        if col.backward_weights is not None:
            # Nonlinear generative mapping (sigmoid for bounded predictions)
            hidden = np.tanh(col.mu)
            return np.dot(col.backward_weights, hidden)
        return np.zeros(self.columns[level - 1].size if level > 0 else col.size)
    
    def _compute_prediction_errors(self, sensory_input: np.ndarray):
        """
        Compute prediction errors at each level of the hierarchy.
        
        ε⁽ⁱ⁾ = μ⁽ⁱ⁻¹⁾ − g(μ⁽ⁱ⁾)
        
        Prediction error = what I observe − what I predicted.
        """
        # Level 0: error between sensory input and level 1's prediction
        if self.n_levels > 1:
            prediction_from_above = self._generate_prediction(1)
            self.columns[0].epsilon = sensory_input - prediction_from_above
        else:
            self.columns[0].epsilon = sensory_input - self.columns[0].mu
        
        # Higher levels: error between current state and prediction from above
        for i in range(1, self.n_levels - 1):
            prediction_from_above = self._generate_prediction(i + 1)
            self.columns[i].epsilon = self.columns[i].mu - prediction_from_above
    
    def _recognition_dynamics(self):
        """
        Recognition dynamics — gradient descent on free energy.
        
        μ̇⁽ⁱ⁾ = Dμ⁽ⁱ⁾ − ∂F/∂μ⁽ⁱ⁾
        
        The internal states update to minimize prediction error,
        weighted by precision. This IS perception in the Fristonian framework.
        """
        for i in range(self.n_levels):
            col = self.columns[i]
            
            # Gradient of free energy w.r.t. internal states
            # ∂F/∂μ = precision-weighted prediction error + prior gradient
            
            dF_dmu = np.zeros(col.size)
            
            # Bottom-up: precision-weighted error from level below
            if i > 0:
                lower_col = self.columns[i - 1]
                if col.forward_weights is not None:
                    # Error signal from lower level, weighted by precision
                    weighted_error = lower_col.precision * lower_col.epsilon
                    dF_dmu -= np.dot(col.forward_weights, weighted_error)
            
            # Top-down: prediction error at this level
            if i < self.n_levels - 1:
                dF_dmu += col.precision * col.epsilon
            
            # Lateral dynamics (recurrent processing within level)
            if col.lateral_weights is not None:
                lateral = np.dot(col.lateral_weights, col.mu)
                dF_dmu -= 0.1 * lateral
            
            # Update internal states (gradient descent on F)
            col.mu_dot = -self.learning_rate * dF_dmu
            col.mu += col.mu_dot * self.dt
            
            # Bounded activation
            col.mu = np.clip(col.mu, -5.0, 5.0)
    
    def process(self, sensory_input: np.ndarray) -> dict:
        """
        Run perception: minimize free energy given sensory input.
        
        This is "seeing" — the brain settling into an interpretation
        of sensory data that minimizes surprise (prediction error).
        
        Args:
            sensory_input: Raw sensory data (preprocessed by retina/V1).
            
        Returns:
            Dict with internal states, prediction errors, and free energy.
        """
        free_energy_history = []
        
        for iteration in range(self.n_iterations):
            # 1. Compute prediction errors at all levels
            self._compute_prediction_errors(sensory_input)
            
            # 2. Run recognition dynamics (update internal states)
            self._recognition_dynamics()
            
            # 3. Update predictions (generative model output)
            for i in range(1, self.n_levels):
                self.columns[i].prediction = self._generate_prediction(i)
            
            # 4. Compute total free energy (should decrease over iterations)
            F = self._compute_free_energy()
            free_energy_history.append(F)
        
        return {
            'states': [col.mu.copy() for col in self.columns],
            'errors': [col.epsilon.copy() for col in self.columns],
            'predictions': [col.prediction.copy() for col in self.columns],
            'free_energy': free_energy_history,
            'final_F': free_energy_history[-1] if free_energy_history else 0.0
        }
    
    def _compute_free_energy(self) -> float:
        """
        Compute variational free energy across the hierarchy.
        
        F ≈ Σᵢ (εᵢ)ᵀ Πᵢ εᵢ  (precision-weighted sum of squared errors)
        
        This is the Laplace approximation to true free energy.
        """
        F = 0.0
        for col in self.columns:
            # Precision-weighted prediction error (energy term)
            F += 0.5 * np.sum(col.precision * col.epsilon**2)
        return float(F)
    
    def learn(self, learning_rate: float = 0.001):
        """
        Update generative model parameters (synaptic plasticity).
        
        This slowly adjusts the top-down generative model to better
        predict sensory input. Corresponds to synaptic plasticity
        in the brain (much slower timescale than recognition dynamics).
        """
        for i in range(1, self.n_levels):
            col = self.columns[i]
            lower = self.columns[i - 1]
            
            if col.backward_weights is not None:
                # Gradient of F w.r.t. backward weights
                # ΔW = -lr * ε * ∂g/∂W
                hidden = np.tanh(col.mu)
                dg_dW = np.outer(lower.epsilon, hidden)
                col.backward_weights += learning_rate * dg_dW
            
            if col.forward_weights is not None:
                # Forward weights learn from prediction errors
                dW = np.outer(col.mu, lower.epsilon * lower.precision)
                col.forward_weights += learning_rate * dW
    
    def get_representation(self, level: int) -> np.ndarray:
        """Get the internal state representation at a given hierarchical level."""
        return self.columns[level].mu.copy()
    
    def get_total_surprise(self) -> float:
        """Total surprise = total precision-weighted prediction error."""
        return self._compute_free_energy()