Skip to main content

SwarmTorch 🐝🔥

A PyTorch Library for Metaheuristic Optimization in Deep Learning

arXiv License: MIT Python 3.10+ PyTorch


📄 About

SwarmTorch is a comprehensive PyTorch library that brings 120 metaheuristic optimization algorithms to the deep learning ecosystem. It provides a unified API for:

  • 60 model-training optimizers — gradient-free weight optimization for neural networks
  • 60 hyperparameter searchers — intelligent hyperparameter optimization (HPO)

Published on arXiv: arXiv:2503.XXXXX


🚀 Key Features

  • 120 Algorithms Across 6 Families: Swarm Intelligence, Evolutionary, Physics-based, Human-based, Bio-inspired, and Hybrid methods
  • Native PyTorch Integration: Drop-in replacement for torch.optim.Optimizer
  • GPU-Accelerated: Full tensor parallelism using PyTorch's CUDA support
  • Research-Grade Benchmarks: Comprehensive evaluation across 60 training algorithms and 60 HPO searchers with reproducible results

📊 Key Research Findings

Our empirical evaluation on standard benchmarks demonstrates:

  • 63.3% of SwarmTorch's hyperparameter searchers outperform Random Search baseline
  • Metaheuristics achieve significantly lower loss on multimodal (non-convex) test functions where gradient-based methods fail
  • Up to 17.4× GPU speedup for models with >50K parameters

See our paper for detailed results.


📦 Installation

pip install swarmtorch

With development dependencies:

pip install -e ".[dev]"

💻 Usage Examples

Model Weight Optimization

import torch.nn as nn
from swarmtorch import PSO

# Define model
model = nn.Sequential(
    nn.Linear(10, 64),
    nn.ReLU(),
    nn.Linear(64, 1)
)

# Use PSO as optimizer
optimizer = PSO(model.parameters(), swarm_size=30)

# Training loop
for epoch in range(100):
    def closure():
        optimizer.zero_grad()
        output = model(inputs)
        loss = criterion(output, targets)
        loss.backward()
        return loss
    
    optimizer.step(closure)

Hyperparameter Optimization

from swarmtorch import PSOSearch

# Define search space
searcher = PSOSearch(
    model_fn=build_model,
    param_space={
        'lr': (0.001, 0.1),
        'hidden_dim': [32, 64, 128],
        'batch_size': [16, 32, 64]
    },
    train_fn=train_fn,
    iterations=50,
    swarm_size=20
)

# Run optimization
best_params = searcher.search()

📂 Repository Structure

swarmtorch/
├── swarmtorch/          # Main library
│   ├── swarm/          # Swarm Intelligence algorithms
│   ├── evolutionary/   # Evolutionary algorithms
│   ├── physics/       # Physics-based algorithms
│   ├── bio_inspired/  # Bio-inspired algorithms
│   ├── human_based/  # Human-based algorithms
│   └── hybrid/        # Hybrid algorithms
├── benchmarks/         # Experimental benchmarks
├── paper/             # Research paper & figures
└── README.md

📚 Benchmark Results

Detailed experimental results are available in:

Top Performing HPO Searchers

Rank Algorithm Best Accuracy
1 SA Search 98.5%
2 DVBA Search 98.5%
3 PBIL Search 98.0%
4 AFSA Search 98.0%
5 JSO Search 98.0%

Top Performing Training Optimizers

Rank Algorithm Final Loss
1 Adam (baseline) 0.033
2 CA 0.132
3 HHO 0.167
4 CEM 0.199
5 PFA 0.239

🔧 Hardware & Reproducibility

All benchmarks were run on:

  • GPU: NVIDIA T4x2 (Kaggle)
  • CPU: Intel Core i7-12700K (workstation)
  • Software: PyTorch 2.9.0+cu126, Python 3.11, NumPy 1.26

Random seeds fixed at 42 for reproducibility.


📝 Citation

If you use SwarmTorch in your research, please cite:

@article{swarmtorch2026,
  author  = {Halleluyah Darasimi Oludele},
  title   = {SwarmTorch: A PyTorch Library for 120 Metaheuristic Optimization Algorithms in Deep Learning},
  journal = {arXiv preprint arXiv:2503.XXXXX},
  year    = {2026}
}

📄 License

MIT License - See LICENSE for details.


🤝 Acknowledgments

Inspired by pyMetaheuristic and the broader metaheuristic optimization community.

Metadata

Release files for swarmtorch 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swarmtorch 0.3.0
File Size Uploaded Provenance Actions
swarmtorch-0.3.0.tar.gz 84.2 kB Without provenance Details for swarmtorch-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for swarmtorch 0.3.0
File Interpreter ABI Platform Actions
swarmtorch-0.3.0-py3-none-any.whl Python 3 none any Details for swarmtorch-0.3.0-py3-none-any.whl

Total release size: 187.3 kB

Release files / swarmtorch-0.3.0.tar.gz

Download URL swarmtorch-0.3.0.tar.gz
Size 84.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3ab9113a98319d708de70a38207157879ef438848a1f301f4dbf889d9264e0e6
BLAKE2b-256 checksum
How to use checksums
e4dce646665f1201fbfd88d1e7f54585cb6509875ae370a031149e77e64f9a8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / swarmtorch-0.3.0-py3-none-any.whl

Download URL swarmtorch-0.3.0-py3-none-any.whl
Size 103.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
474a0cb30b91785c954dd062a49d4ff800029db44118b503a5e1bb1c18fa1e7d
BLAKE2b-256 checksum
How to use checksums
36b5b5ba90024fa3bab230c45f6df05e2cc281a878495a02964da1d542216535
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Security for swarmtorch 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page