Skip to main content

A torch-based predictive coding library

Project description

Torch Predictive Coding

A simple predictive coding layer on top of pytorch

Currently set up for simple supervised classification tasks. See examples/cifar10.py and examples/mnist.py for examples of how to do this.

Installation

pip install torch-pc

Development

uv sync

# Try an example
uv run example/mnist.py

Sample code

torch-pc is meant to integrate tightly with torch so things such as data loading / data transformations remain the same. Saving and loading a PC model is easy with a simple save and load method on PCNetwork. It is also possible to transfer weights from a backprop model to PC and vice versa (assuming architecture matches).

Because PC is a relatively new method of training models, most research has been conducted around linear layers so all models must use linear layers. Future version will expand on this, but currently model instatiation looks like this:

model = PCNetwork(
    dims=[256, 512, 256, 128], # Input dimensions of your linear layers
    output_dim=10,             # Dimensions in your output / prediction
    activation_fn=torch.tanh,  # Type of activation between layers (default: relu)
    activation_deriv=lambda a: 1 - torch.tanh(a)**2, # Derivative of activation function
    use_bias=False, # Optional, adds bias term to each linear layer, default false
)

Kick off PC training:

energy_history, supervised_energy_history = train_pcn(
    model=model, # PCNetwork
    data_loader=train_dl, # standard torch DataLoader class
    num_epochs=10, 
    eta_infer=0.1,  # Learning rate on inference part of training
    eta_learn=0.01, # Learning rate on update part of training
    infer_steps=10, # Number of inference steps per batch
    T_learn=3,      # Number of learning steps per batch.
    device=DEVICE,
)

Run test for classification tasks (returns top-1 and top-3 accuracy):

acc1, acc3 = test_pcn_classify(
    model=model,         # PCNetwork
    data_loader=test_dl, # standard torch DataLoader class
    infer_steps=300,     # Number of inference steps per batch
    eta_infer=0.1,       # Learning rate on inference part of prediction
    device=DEVICE,
)

Run test for regression tasks (returns mean squared error and mean average error):

mse, mae = test_pcn_regress(
    model=model,         # PCNetwork
    data_loader=test_dl, # standard torch DataLoader class
    infer_steps=300,     # Number of inference steps per batch
    eta_infer=0.1,       # Learning rate on inference part of prediction
    device=DEVICE,
)

Project details


Download files

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

Source Distribution

torch_pc-0.1.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

torch_pc-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: torch_pc-0.1.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.11

File hashes

Hashes for torch_pc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aadef279c6adce4057d1c907a02d149e81808e4ff070d3f7c19b698640433df4
MD5 2d042ccd85a2f7e166fff7e2f14a5df7
BLAKE2b-256 952748d03d84eddc81b472675a7fe37c44fa899027250ba328c8460fbe5e2d52

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torch_pc-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.11

File hashes

Hashes for torch_pc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2e5753acb5c044cf1b99ec95d945ae9d79af6d4124724dbc712ac556d3e1662
MD5 a67956cd16e773e10bfb51b80aca9a9c
BLAKE2b-256 e4cd4326c59764f488efabf6bcb4ca23c55c59928a638b95f32ce022b952e1a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page