Skip to main content

SPICE: Sparse and Interpretable Cognitive Equations

Project description

Computational Discovery of Sparse and Interpretable Cognitive Equations (SPICE)

SPICE Logo

SPICE is a framework for automating scientific practice in cognitive science and is based on a two cornerstones:

  1. A task-specific RNN is trained to predict human behavior and thus learn implicitly latent cognitive mechanisms.

  2. Sparse Identification of nonlinear Dynamics (SINDy; an equation discovery algorithm) is used to obtain mathematically interpretable equations for the learned cognitive mechanisms.

📚 Documentation: https://whyhardt.github.io/SPICE/

The resulting model with the neural-network architecture but with equations instead of RNN modules is called SPICE model. An overview is given in Figure 1.

This README file gives an overview on how to install and run SPICE as a scikit-learn estimator. To learn how to use SPICE in more comprehensive scenarios, you can go to tutorials.

Figure 1 - SPICE Overview

Installation

You can install SPICE using pip:

pip install autospice

or, you can clone this repository and install it locally from the root folder:

pip install -e .

Features

  • Scikit-learn compatible estimator interface
  • Customizable network architecture for identifying complex cognitive mechanisms
  • Participant embeddings for identifying individual differences
  • Precoded models for simple Rescorla-Wagner, forgetting mechanism, choise perseveration and parcitipant embeddings

Quick Start

from spice.estimator import SpiceEstimator
from spice.precoded import RescorlaWagnerRNN, RESCOLA_WAGNER_CONFIG
from spice.resources.bandits import BanditsDrift, AgentQ, create_dataset

# Simulate dataset from a two-armed bandit task with a Q agent
environment = BanditsDrift(sigma=0.2, n_actions=2)

agent = AgentQ(
    n_actions=2,
    alpha_reward=0.6,  # Learning rate for positive rewards 
    alpha_penalty=0.6,  # Learning rate for negative rewards
    forget_rate=0.3,
)

dataset, _, _ = create_dataset(
    agent=agent,
    environment=environment,
    n_trials=200,
    n_sessions=256,
)

# Create and fit SPICE model
spice_estimator = SpiceEstimator(
    rnn_class=RescorlaWagnerRNN,
    spice_config=RESCOLA_WAGNER_CONFIG,
    hidden_size=8,
    learning_rate=5e-3,
    epochs=16,
    n_steps_per_call=16,
    spice_participant_id=0,
    save_path_rnn='rnn_model.pkl',   # (Optional) File path (.pkl) to save RNN model after training
    save_path_spice='spice_model.pkl', # (Optional) File path (.pkl) to save SPICE model after training
    verbose=True,
)

spice_estimator.fit(dataset.xs, dataset.ys)

# Get learned SPICE features
features = spice_estimator.spice_agent.get_spice_features()
for id, feat in features.items():
    print(f"\nAgent {id}:")
    for model_name, (feat_names, coeffs) in feat.items():
        print(f"  {model_name}:")
        for name, coeff in zip(feat_names, coeffs):
            print(f"    {name}: {coeff}")

# Predict behavior
pred_rnn, pred_spice = spice_estimator.predict(dataset.xs)

print("\nPrediction shapes:")
print(f"RNN predictions: {pred_rnn.shape}")
print(f"SPICE predictions: {pred_spice.shape}")  

Saving and loading models

In addition to specifying save paths when creating the SpiceEstimator, you can also save an existing estimator's models, or load from save files:

# Save trained model to file
spice_estimator.save_spice(path_rnn='rnn_model.pkl', path_spice='spice_model.pkl')

# Load saved model
loaded_spice = SpiceEstimator.load_spice(path_rnn='rnn_model.pkl', path_spice='spice_model.pkl')

# Use loaded model for predictions
pred_rnn, pred_spice = loaded_spice.predict(dataset.xs)

Requirements

See requirements.txt for a complete list of dependencies.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Citation

If you use this package in your research, please cite:

@software{spice2025,
  title = {SPICE: Sparse and Interpretable Cognitive Equations},
  year = {2025},
  author = {Weinhardt, Daniel},
  url = {https://github.com/whyhardt/SPICE}
}

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

autospice-0.1.6.tar.gz (725.3 kB view details)

Uploaded Source

Built Distribution

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

autospice-0.1.6-py3-none-any.whl (67.9 kB view details)

Uploaded Python 3

File details

Details for the file autospice-0.1.6.tar.gz.

File metadata

  • Download URL: autospice-0.1.6.tar.gz
  • Upload date:
  • Size: 725.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for autospice-0.1.6.tar.gz
Algorithm Hash digest
SHA256 c3b43ca5ddb37541b8076cc5721c83468b69f97218d2071050cdab9a592d2921
MD5 c6a6d3e2f6cd6002b09619a04076c6c9
BLAKE2b-256 eb5b3fd3ec6805364211078806ae5979ee7901308b38f2a13878d91b376bf4c7

See more details on using hashes here.

File details

Details for the file autospice-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: autospice-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 67.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for autospice-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 c4f2dab0bd3e559757f4b1be3ce84ae8d449fd629bee75524d6b663bb4a8a859
MD5 097081b2cc374e221b717796384eb131
BLAKE2b-256 a397d5a03fb8aa042c389c14255b604cce7de791be72dbef6b9e0706457eeba3

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