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.

We compared Hagfish against industry heavyweights (Optuna, Grid Search) to measure efficiency. While Bayesian Optimization (Optuna) chases raw accuracy, Hagfish optimizes for the "Economic Sweet Spot."

Strategy Accuracy (%) Avg. Cost Reward (Efficiency)
Standard (Fixed) 92.69 1,363 0.8996
Random Search 93.45 1,142 0.9117
Grid Search 95.26 1,506 0.9225
Optuna (Bayesian) 96.90 4,197 0.8851
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.1.tar.gz (17.9 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.1-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for hagfish_adaptive_trainer-0.2.1.tar.gz
Algorithm Hash digest
SHA256 88aca2fc9060937b447c3a7bf2a4cce018966cc6eafe9e2e1cab719b83665321
MD5 552bf0340e55d190fc1bd1703087ad68
BLAKE2b-256 e901a2a323b71f7a795bb2eaa213bd02af1f7aa62dea63d4ba56f2551d4ca728

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hagfish_adaptive_trainer-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4ea2f2a9fb0b8516e961dbd2031df9972d91b8e8ec167cb8572818df54790b80
MD5 62ad6e921b81560af487093a49620732
BLAKE2b-256 6ff8bf1b8422ead7574e93035a26f2b5612480c3650a430204d44c7d7372cf2f

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