Skip to content

FarhanRajputFelix/self-regulating-ai

Repository files navigation

Self-Regulating Learning System

A bio-inspired adaptive learning system that implements homeostatic regulation to autonomously adjust optimizer hyperparameters in response to non-stationary data distributions.

CI Python 3.9+ License: MIT


Problem Statement

Fixed hyperparameters fail in non-stationary environments. Manual tuning is impractical for real-time systems. Adaptive methods like Adam and RMSProp pre-define update rules but don't react to environmental changes.

This project asks: Can a learning agent regulate its own optimizer using biologically-inspired feedback -- modeling learning rate adaptation as a control-theoretic problem?


System Architecture

The system implements two complementary subsystems:

1. Homeostatic Learning Rate Regulator (src/)

A closed-loop control mechanism where the learning rate is dynamically adjusted based on the error signal magnitude:

graph TD
    Env["Environment<br/>(Non-Stationary Data)"] -->|x, y_true| Agent["Linear Agent<br/>(Weights, Prediction)"]
    Agent -->|y_hat| LossFn["Loss Function<br/>(MSE)"]
    LossFn -->|error signal| Reg["Homeostatic<br/>Regulator"]
    Reg -->|adjusted LR| Opt["Gradient Descent<br/>Optimizer"]
    Opt -->|weight update| Agent

    style Reg fill:#198754,stroke:#fff,color:#fff
    style Env fill:#0d6efd,stroke:#fff,color:#fff
Loading

Core equation:

LR(t+1) = LR_base * exp(-alpha * |error|)
  • High error --> aggressive LR reduction (stabilize)
  • Low error --> LR relaxes toward base (fine-tune)
  • Bounded: LR never exceeds LR_base, approaches 0 under catastrophic error

2. ML Failure Detection System (monitoring/, controller/)

A meta-learning pipeline that predicts system failures before they cascade:

graph LR
    A["Base Model<br/>(RandomForest)"] --> B["Entropy<br/>Monitor"]
    A --> C["Drift Detector<br/>(KS Test)"]
    A --> D["Confidence<br/>Trend Monitor"]
    B --> E["Meta-Model<br/>(LogReg)"]
    C --> E
    D --> E
    E -->|failure_prob| F["Decision<br/>Engine"]
    F -->|CONTINUE / WARN / FALLBACK| G["System Action"]

    style E fill:#dc3545,stroke:#fff,color:#fff
    style F fill:#fd7e14,stroke:#fff,color:#fff
Loading
Signal Method Purpose
Entropy Shannon entropy of prediction probabilities Measures model confusion
Drift Kolmogorov-Smirnov two-sample test Detects distribution shift
Confidence Linear regression slope over sliding window Tracks prediction stability

Project Structure

self-regulating-ai/
|-- src/                        # Core homeostatic regulation system
|   |-- data_generator.py       # Non-stationary environment simulator
|   |-- model.py                # Linear learning agent (gradient descent)
|   |-- regulator.py            # Homeostatic LR regulator (exp-decay feedback)
|   +-- main.py                 # Simulation loop
|
|-- monitoring/                 # Failure detection subsystem
|   |-- entropy.py              # Prediction uncertainty measurement
|   |-- drift.py                # Data distribution shift detection (KS test)
|   |-- confidence.py           # Confidence trend monitoring (sliding window)
|   +-- meta_model.py           # Meta-model: predicts system failure probability
|
|-- controller/
|   +-- decision_engine.py      # Action decision (CONTINUE / WARN / FALLBACK)
|
|-- base_model/
|   +-- model.py                # Base classifier training (RandomForest)
|
|-- experiments/
|   |-- run_experiments.py      # Full experiment suite with 4 baseline comparisons
|   |-- baseline_test.py        # Component-level verification tests
|   +-- simulate_failure.py     # Failure injection simulation
|
|-- results/                    # Generated experiment outputs
|   |-- loss_comparison.png     # MSE convergence curves (4 methods)
|   |-- lr_adaptation.png       # Learning rate trajectories
|   |-- convergence_analysis.png # Post-perturbation recovery comparison
|   +-- results_table.md        # Summary metrics table
|
|-- .github/workflows/ci.yml   # CI pipeline (Python 3.9/3.10/3.11)
|-- Dockerfile                  # Reproducible experiment container
|-- requirements.txt            # Dependencies
+-- README.md

Experimental Results

Setup: 5D linear regression with sinusoidal coefficient drift at t=250, 500 timesteps, 100 samples per batch.

Loss Convergence Under Non-Stationary Data

Loss Comparison

Learning Rate Adaptation Trajectories

LR Adaptation

Post-Perturbation Recovery Analysis

Recovery Analysis

Results Summary

Method Pre-Shift MSE Post-Shift Peak LR Stability
Homeostatic Regulator 0.486 3.915 Most stable
Fixed LR (0.01) 0.435 3.829 Zero variance (static)
Step Decay 1.039 6.542 Staircase jumps
Adam-Style 2.243 4.527 High variance

Key findings:

  • Homeostatic regulator achieves near-best pre-shift performance while maintaining the most stable LR behavior post-perturbation
  • Step Decay degrades severely after shift (LR already too small to recover)
  • Adam-Style has highest pre-shift loss due to moment estimate overhead on simple problems
  • Fixed LR performs well on smooth regions but cannot adapt to changing dynamics

Core Algorithm

for t in 1..T:
    x, y = environment.generate(t)      # distribution shifts over t
    y_hat = agent.predict(x)
    loss = MSE(y, y_hat)
    error = |loss - target_loss|
    lr = lr_base * exp(-alpha * error)   # homeostatic regulation
    agent.update_weights(x, y, lr)       # gradient descent step

Why exponential decay?

  • Bounded: LR is always in (0, lr_base] -- no divergence risk
  • Proportional: Response strength scales with error magnitude
  • Bio-inspired: Models homeostatic regulation in biological systems where organisms maintain internal stability despite external perturbations

Research Motivation

Adaptive learning rate methods (Adam, RMSProp) pre-define update rules using per-parameter moment estimates. This system instead models regulation as a control problem, drawing from biological homeostasis where organisms maintain internal stability despite external perturbations.

Key question: Does error-driven exponential decay regulation outperform fixed schedules in non-stationary settings?

Result: The homeostatic regulator is competitive with or outperforms fixed LR and step decay baselines, especially in recovery stability after distribution shift. Adam slightly outperforms on smooth regions due to per-parameter adaptation, but the regulator provides more predictable behavior post-perturbation.


Limitations

  • Linear agent only; non-linear (neural network) extension untested
  • Single feedback signal (MSE); multi-objective regulation not explored
  • Exponential decay parameter alpha requires initial tuning
  • No comparison with advanced schedulers (cosine annealing, warm restarts)
  • Sinusoidal drift is synthetic; real-world distribution shifts may differ

Future Work

  • Extend to neural networks with multi-layer feedback
  • Multi-objective homeostatic regulation (loss + gradient magnitude + confidence)
  • Compare against cosine annealing and cyclical LR schedulers
  • Formalize convergence guarantees under bounded distribution shift
  • Integrate the failure detection system with the homeostatic regulator for a fully autonomous pipeline

Quick Start

Local

pip install -r requirements.txt

# Run core simulation
python -m src.main

# Run full experiment suite (generates plots)
python experiments/run_experiments.py

# Run component tests
python experiments/baseline_test.py

Docker

docker build -t self-reg-ai .
docker run self-reg-ai

Technical Depth

Concept Implementation
Homeostatic Regulation Control-theoretic LR adaptation via exponential decay of error magnitude
Non-Stationary Environments Sinusoidal coefficient drift in 5D linear regression
Drift Detection Kolmogorov-Smirnov two-sample test for distribution shift
Meta-Learning Logistic regression over (entropy, drift, confidence) signals
Decision Control Three-tier action system: CONTINUE / WARN / FALLBACK

Why This Matters

This project demonstrates understanding of: control systems, adaptive optimization, non-stationary learning, bio-inspired algorithm design, and meta-learning -- concepts central to reinforcement learning, autonomous systems, and robust ML research.


Author

Farhan Muhammad Bashir Researching autonomous adaptive systems.

LinkedIn

Topics: machine-learning, adaptive-systems, homeostasis, control-theory, non-stationary-learning, meta-learning, python, research


Copyright 2026 All Rights Reserved.

About

Autonomous agent implementing homeostatic regulation for real-time hyperparameter adaptation in non-stationary environments.

Topics

Resources

License

Stars

Watchers

Forks

Contributors