Skip to main content

Adaptive training budget optimizer (bandit-style agentic framework)

Project description

hagfish-adaptive-trainer

PyPI version License: MIT Python 3.8+

hagfish-adaptive-trainer is a high-efficiency agentic framework for training budget optimization. It dynamically allocates training resources (batch size, epochs, and capacity) using a feedback-driven loop—maximizing model performance while minimizing compute cost.


Why Hagfish?

In traditional machine learning workflows, a large portion of compute is wasted on diminishing returns—running epochs that no longer produce meaningful improvements.

Hagfish introduces an agentic control loop that continuously asks:

"Is the next unit of compute actually worth the improvement it brings?"

Key benefits

  • Cost efficiency — Automatically reduces budgets when performance saturates
  • Stagnation recovery — Escalates resources only when learning stalls
  • Reward-centric — Optimizes the tradeoff between accuracy and cost
  • Plug-and-play — Framework-agnostic (Scikit-Learn, PyTorch, TensorFlow)

Performance benchmarks

In comparative experiments, Hagfish Adaptive Trainer achieves competitive accuracy while using significantly fewer computational resources.

Strategy Accuracy Avg. Cost Reward (Acc − α·Cost)
Fixed Budget 92.69% 1,363 0.8996
Random Search 93.86% 2,663 0.8853
Hagfish (Adaptive) 93.51% 697 0.9212

Dataset: Breast Cancer Wisconsin (Diagnostic) Reward: Accuracy − (2 × 10⁻⁵ × Cost)


Installation

Install from PyPI

pip install hagfish-adaptive-trainer

Install from source (development)

git clone https://github.com/your-repo/hagfish-adaptive-trainer.git
cd hagfish-adaptive-trainer
pip install -e .

Core architecture

The system operates as an episodic agent loop composed of three cooperating components:

  • PlannerAgent Proposes training budgets (batch size, epochs) based on historical performance.

  • CriticAgent Evaluates outcomes and classifies them as:

    • Improvement
    • Stagnation
    • Saturation
  • AgentMemory Tracks reward trends and stagnation to prevent unnecessary escalation.

This mirrors the biological behavior of Hagfish: conserve energy until escalation is justified.


Quick start

Basic usage

from adaptive_trainer import AdaptiveTrainer

# Initialize with cost sensitivity (alpha)
trainer = AdaptiveTrainer(alpha=2e-5)

# Request a training budget
plan = trainer.plan({"dataset_size": 569})
# Example output:
# {'pop_size': 32, 'max_iter': 100, 'elite_size': 2}

# Train your model using the plan
# model = MLPClassifier(
#     batch_size=plan["pop_size"],
#     max_iter=plan["max_iter"]
# )
# model.fit(X_train, y_train)

# Report results back to the agent
trainer.observe(
    metric=0.935,
    cost=697,
    params=plan
)

Advanced configuration

The Alpha (α) parameter

Alpha controls how aggressively cost is penalized.

Alpha Value Behavior
1e-6 Prioritize accuracy (production models)
1e-5 Balanced accuracy vs cost
1e-4 Aggressive cost reduction (large sweeps)

Stability & warnings

  • Backward compatibility The AdaptiveTrainer.plan() and AdaptiveTrainer.observe() APIs are stable across all 0.1.x releases.

  • Convergence warnings Early low-budget plans may trigger ConvergenceWarning in Scikit-Learn. This is expected behavior during cost exploration and not an error.


Testing & robustness

The package is validated against:

  • Deterministic behavior
  • Edge cases (zero cost, negative metrics)
  • Long-run stability
  • External ML pipelines
  • Cross-platform compatibility

All tests are designed to run outside the package directory, ensuring true public API safety.


Contributing

Contributions are welcome!

  1. Fork the repository

  2. Create a feature branch

    git checkout -b feature/YourFeature
    
  3. Commit changes

    git commit -m "Add YourFeature"
    
  4. Push and open a Pull Request


License

Distributed under the MIT License. See the LICENSE file for details.

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

hagfish_adaptive_trainer-0.2.0.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

hagfish_adaptive_trainer-0.2.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file hagfish_adaptive_trainer-0.2.0.tar.gz.

File metadata

File hashes

Hashes for hagfish_adaptive_trainer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 460dcee0fd26580ed6005edf5c9880f96e12d8dae02ec394913654274bf096d1
MD5 3a9a969965677683b582893e23e00d02
BLAKE2b-256 3a890bcbdee1f1998e56677130346dbcb2cb5745fc49d18a779140c80706d6ef

See more details on using hashes here.

File details

Details for the file hagfish_adaptive_trainer-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hagfish_adaptive_trainer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9ba18392095e5e2dfbe60877a088bf9f70bc3098f5a64cccabac047a43cfc70
MD5 488e1fcde0c040b6359f085638925600
BLAKE2b-256 3893adfe3fba38e5dfcb9f3efaf4cb5e126146f547838486ea727a9347e43364

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