title: Continuous Thought Machine - Energy based Halting
emoji: π°οΈ
colorFrom: blue
colorTo: indigo
sdk: docker
sdk_version: 20.10.21
app_file: app.py
pinned: false
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
π°οΈ The Continuous Thought Machine
π PAPER: Technical Report | π Blog | πΉοΈ Interactive Website | βοΈ Tutorial
Overview
The Continuous Thought Machine (CTM) is a novel neural architecture designed to unfold and leverage neural activity as the underlying mechanism for observation and action. By introducing an internal temporal axis decoupled from input data, CTM enables neurons to process information over time with fine-grained temporal dynamics.
Key Contributions
- Internal Temporal Axis: Decoupled from input data, allowing neuron activity to unfold independently
- Neuron-Level Temporal Processing: Each neuron uses unique weight parameters to process a history of incoming signals
- Neural Synchronisation: Direct latent representation for modulating data and producing outputs, encoding information in the timing of neural activity
The CTM demonstrates strong performance across diverse tasks including ImageNet classification, 2D maze solving, sorting, parity computation, question-answering, and reinforcement learning.
π¬ Energy-Based Halting Experiment
This repository includes an implementation of Energy-Based Halting, a mechanism that frames "thinking" as an optimization process where the model dynamically adjusts its internal thought process duration based on sample difficulty.
Concept
Instead of using heuristic certainty thresholds, we train a learned energy scalar that:
- Minimizes energy for correct predictions (pushing the system to low-energy equilibrium)
- Maximizes energy for incorrect predictions (pushing away from stable states)
- Enables adaptive halting based on energy thresholds or convergence
Implementation
Modified Components:
models/ctm.py: Added energy projection head that maps synchronization states to scalar energy valuesutils/losses.py: ImplementedEnergyContrastiveLossfor training the energy functiontasks/image_classification/train_energy.py: Training script with energy haltinginference_energy.py: Adaptive inference that halts when energy drops below threshold or stabilizesconfigs/energy_experiment.yaml: Configuration for energy experiments
Training:
# Local training
pixi run accelerate launch tasks/image_classification/train_energy.py \
--energy_head_enabled \
--loss_type energy_contrastive \
--dataset cifar10
# Or with traditional python
pixi run python tasks/image_classification/train_energy.py \
--energy_head_enabled \
--loss_type energy_contrastive
Deployment to Hugging Face: See GUIDE_HF.md for instructions on deploying the training job to Hugging Face Spaces with GPU support.
π Quick Start
Setup with Pixi (Recommended)
We use Pixi for dependency management, which handles both Python packages and system dependencies like ffmpeg.
# Install dependencies
pixi install
# Run training
pixi run python tasks/image_classification/train.py
Alternative: Conda Setup
conda create --name=ctm python=3.12
conda activate ctm
pip install -r requirements.txt
conda install -c conda-forge ffmpeg
If there are PyTorch version issues:
pip uninstall torch
pip install torch --index-url https://download.pytorch.org/whl/cu121
π Repository Structure
βββ tasks/
β βββ image_classification/
β β βββ train.py # Standard training
β β βββ train_energy.py # Energy halting training
β β βββ analysis/run_imagenet_analysis.py
β β βββ plotting.py
β βββ mazes/
β β βββ train.py
β β βββ analysis/
β βββ sort/
β βββ parity/
β βββ qamnist/
β βββ rl/
βββ models/
β βββ ctm.py # Main CTM model (with energy head support)
β βββ modules.py # Neuron-level models, Synapse UNET
β βββ ff.py # Feed-forward baseline
β βββ lstm.py # LSTM baseline
βββ utils/
β βββ losses.py # Loss functions (includes EnergyContrastiveLoss)
β βββ schedulers.py
β βββ housekeeping.py
βββ data/
β βββ custom_datasets.py
βββ configs/
β βββ energy_experiment.yaml # Energy halting hyperparameters
βββ inference_energy.py # Adaptive energy-based inference
βββ Dockerfile # For HF Spaces deployment
βββ GUIDE_HF.md # Hugging Face deployment guide
βββ checkpoints/ # Model checkpoints
π― Model Training
Each task has dedicated training code designed for ease-of-use and collaboration. Training scripts include reasonable defaults, with paper-replicating configurations in accompanying script folders.
Image Classification Example
# Standard CTM training
python -m tasks.image_classification.train
# Energy halting training
python -m tasks.image_classification.train_energy \
--energy_head_enabled \
--loss_type energy_contrastive
VSCode Debug Configuration
{
"name": "Debug: train image classifier",
"type": "debugpy",
"request": "launch",
"module": "tasks.image_classification.train",
"console": "integratedTerminal",
"justMyCode": false
}
π Analysis & Visualization
Analysis and plotting code to replicate paper figures is provided in tasks/.../analysis/*.
Note: ffmpeg is required for generating videos:
conda install -c conda-forge ffmpeg
# or with pixi (already included)
pixi install
π¦ Checkpoints and Data
Download pre-trained checkpoints and datasets:
- Checkpoints: Google Drive
- Maze Data: Google Drive
Place checkpoints in the checkpoints/ folder following the structure checkpoints/{task}/...
π€ Hugging Face Integration
This repository includes full support for training on Hugging Face infrastructure:
- Accelerate: Multi-GPU and mixed precision training
- Hub Integration: Automatic checkpoint uploading
- Spaces Deployment: Run training jobs on GPU Spaces
See GUIDE_HF.md for detailed instructions.
π Interactive Resources
- Interactive Website: Maze-solving demo, videos, and visualizations
- Paper: Technical details and experiments
- Blog: High-level overview and insights
- Tutorial Notebook: Hands-on introduction
π Citation
The Continuous Thought Machine : Energy-Based Halting Extension
This repository contains experimental extensions for Energy-Based Halting developed by Uday Phalak.
@misc{ctmenergy2025,
title={Energy-Based Halting for Continuous Thought Machines},
author={Phalak, Uday},
year={2025},
note={Experimental Extension of Continuous Thought Machines}
}
Based on The Continuous Thought Machine
@article{ctm2025,
title={The Continuous Thought Machine},
author={Darlow, Luke and Regan, Ciaran and Risi, Sebastian and Seely, Jeffrey and Jones, Llion},
journal={arXiv preprint arXiv:2505.05522},
year={2025}
}
π License
This project is released under the MIT License. See LICENSE file for details.