Skip to main content

mlx-pc

A straightforward, minimal Predictive Coding (PC) implementation for the Apple MLX framework.

mlx-pc provides stateless predictive coding layers and a network container to manage relaxation dynamics. It is designed to be completely decoupled from the optimizer, allowing you to plug in standard MLX optimizers or custom implementations.

Installation

pip install mlx-pc

API Reference

The library exposes two main components: PCNetwork and PCLayer.

  1. PCNetwork (Container) The primary module that stacks PCLayers and handles the temporal state management (relaxation iterations) during the forward pass.
  • Initialization:
model = PCNetwork(layer_dims: list[int], bias=True)

layer_dims: A list of integers defining the feature dimensions of each layer (e.g., [128, 256, 512]).

  • Forward Pass (call):
predictions, layer_errors = model(sensory_x, max_iters=10, base_eta=0.1, alpha=1.0, beta=0.5)

x: Input tensor of shape (batch_size, seq_len, layer_dims[0]). max_iters: Number of internal relaxation steps to perform for the fast variables. Returns: A tuple containing the final predictions and a list of layer_errors tensors.

  1. PCLayer (Stateless Block) A single predictive coding layer. It acts as a pure function, computing local surprisal and the updated state based on bottom-up inputs and top-down priors.
  • Initialization:
layer = PCLayer(in_dim: int, out_dim: int, bias=True)
  • Forward Pass (call):
current_state, error = layer(x, higher_state, prev_state, base_eta=0.1, alpha=1.0, beta=0.5)

x: The bottom-up input from the lower layer. higher_state: The expected state from the higher layer. prev_state: The state of this layer at iteration t-1. Returns: The updated current_state and the calculated error (residual).

Quick Integration Example

mlx-pc relies on standard MLX auto-grad to update weights. You extract the total squared error (Free Energy) and pass it to any optimizer.

import mlx.core as mx
import mlx.optimizers as optim
from mlx_pc import PCNetwork

# 1. Init
model = PCNetwork(layer_dims=[128, 256, 512])
optimizer = optim.AdamW(learning_rate=1e-3)

# 2. Define Loss
def loss_fn(model, x):
    predictions, layer_errors = model(x)
    return mx.sum(mx.array([mx.sum(mx.square(e)) for e in layer_errors]))

loss_and_grad_fn = mx.value_and_grad(model, loss_fn)

# 3. Step
x = mx.random.normal((4, 32, 128))
loss, grads = loss_and_grad_fn(model, x)
optimizer.update(model, grads)
mx.eval(model.parameters(), optimizer.state)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlx_predictive_coding-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlx_predictive_coding-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file mlx_predictive_coding-0.1.0.tar.gz.

File metadata

  • Download URL: mlx_predictive_coding-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for mlx_predictive_coding-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9a7499926ca9f6389dd0a22b50c2cb5ba43f44e3e6b88dda806784a872f8d958
MD5 a857338eb5117e9e6ab51e7ed278d297
BLAKE2b-256 68ecf6c927ca7d8ffbb5c6d4e924f6e92bccb61feed696c4e869c70265e81bcb

See more details on using hashes here.

File details

Details for the file mlx_predictive_coding-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mlx_predictive_coding-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bb73342366546b84515026c71d80031e377f6bb806906d0827ba8e2290effe2
MD5 d329a2813251d0b0e1ac101ade56512b
BLAKE2b-256 feb7ec8bfbbc26d7164844e0408cdca71cb022eae9d2393c5013c789a97609b1

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page