This repository contains the implementation of "Sleep-Based Learning," a bio-inspired architecture that mimics biological REM cycles to achieve continual learning in Large Language Models. https://gallahat.substack.com/p/llm-sleep-based-learning-implementing
- Wake State: Standard chatbot interaction.
- Sleep State: Generates synthetic future trajectories ("dreams") based on recent interactions using a specialized "Dream Generator" model.
- Memory Consolidation: Fine-tunes the model on a mixed batch of new dreams, recalled memories (old dreams), and grounding data to prevent catastrophic forgetting.
- Bio-Inspired: Mimics the "Dream to Predict" hypothesis of biological REM sleep.
FullPipeline.ipynb: The main notebook containing the full Sleep-Dream-Wake cycle implementation, including the Gradio interface.DreamsGenTrain.ipynb: Notebook for training the "Dream Generator" model.grounding-dataset.json: Dataset of general facts used for grounding during the sleep cycle.
-
Clone the repository:
git clone https://github.com/Gal-Lahat/sleep-based-learning.git cd sleep-based-learning -
Install the required dependencies:
pip install -r requirements.txt
-
Set up your Hugging Face token:
- Create a
.envfile in the root directory. - Add your token:
HF_TOKEN=your_huggingface_token_here
- Create a
Open DreamsGenTrain.ipynb to train the specialized model responsible for generating synthetic dreams. This notebook demonstrates how to fine-tune a model (e.g., Gemma-3 4B) to predict future conversation turns.
Open FullPipeline.ipynb to run the interactive chatbot.
- Run all cells to initialize the model and the Gradio interface.
- Chat with the model.
- Type
/sleepto trigger the Sleep-Dream-Wake cycle, where the model will generate dreams, train on them, and consolidate memories.
- Interaction: The user chats with the model.
- Sleep Trigger: When
/sleepis invoked, the current conversation history is processed. - Dream Generation: The Dream Generator creates synthetic future scenarios based on the conversation.
- Mixed-Batch Training: These new dreams are mixed with old dreams (recalled from storage) and grounding facts.
- Fine-Tuning: A lightweight LoRA adapter is trained on this mixed dataset.
- Merging: The adapter is merged into the base model (or a continuous learning adapter), updating the model's long-term memory.
If you use this code or concept, please cite:
Gal Lahat. (2025). LLM Sleep Based Learning: Implementing REM-style Cycles and Synthetic Dreaming for Continual Memory Consolidation in LLMs. https://gallahat.substack.com/p/llm-sleep-based-learning-implementing
This project is licensed under the MIT License - see the LICENSE file for details.