meta-hpo
Metaheuristic hyperparameter optimization for Python, with a small numpy core.
You describe a search space and a function that trains your model and returns a score. meta-hpo searches for the parameters that maximise or minimise that score. Three optimizers are available: the Artificial Bee Colony (ABC, the default), Particle Swarm Optimization (PSO) and an enhanced bare-bones PSO (BBPSO); more metaheuristics will follow behind the same interface.
Installation
pip install meta-hpo
meta-hpo needs Python 3.10+ and numpy.
Quick start
import metahpo as mh
space = mh.Space(
{
"lr": mh.Float(1e-4, 1e-2, log=True),
"kernel": mh.Categorical([3, 5, 7]),
"units": mh.Int(16, 256),
"dropout": mh.Float(0.0, 0.5),
}
)
def train(params):
model = build_model(params["kernel"], params["units"], params["dropout"])
return fit_and_score(model, lr=params["lr"])
study = mh.optimize(train, space, direction="max", max_evals=300, seed=1)
print(study.best_params, study.best_score)
build_model and fit_and_score stand for your own model code; they are not part of meta-hpo.
train can do anything: a Keras or PyTorch training run, cross-validation, an F1 score.
Its training budget is entirely yours; meta-hpo only counts evaluations.
Runnable example
This version needs only numpy. A closed-form score with its peak at lr=1e-3, kernel=5, units=128
and dropout=0.2 stands in for training:
import numpy as np
import metahpo as mh
space = mh.Space(
{
"lr": mh.Float(1e-4, 1e-2, log=True),
"kernel": mh.Categorical([3, 5, 7]),
"units": mh.Int(16, 256),
"dropout": mh.Float(0.0, 0.5),
}
)
def score(params):
lr_term = 0.05 * (np.log10(params["lr"]) + 3) ** 2
units_term = ((params["units"] - 128) / 256) ** 2
dropout_term = (params["dropout"] - 0.2) ** 2
kernel_bonus = {3: 0.0, 5: 0.02, 7: 0.01}[params["kernel"]]
return 0.9 - lr_term - units_term - dropout_term + kernel_bonus
study = mh.optimize(score, space, direction="max", max_evals=300, seed=1)
print(study.best_params, study.best_score)
Examples
examples/ has runnable scripts that tune a small convolutional network on CIFAR-10:
Ask and tell
For full control over where and how candidates are evaluated, ask for candidates and tell their scores.
A round is one ask() and one tell(); an evaluation is one call of train.
The first ask() returns the whole initial population, colony_size // 2 candidates.
Every later ask() returns one candidate.
So 300 rounds with colony_size=10 make 304 evaluations.
To set a budget, count evaluations instead of rounds:
opt = mh.ABCOptimizer(space, direction="max", colony_size=10, limit=15, seed=1)
evals = 0
while evals < 300:
candidates = opt.ask()
opt.tell(candidates, [train(c.params) for c in candidates])
evals += len(candidates)
print(opt.best_params, opt.best_score)
This loop makes exactly 300 evaluations.
Optimizers
| Optimizer | Batch per ask() |
Main settings |
|---|---|---|
ABCOptimizer |
the initial population, then one candidate | colony_size=10, limit=15 |
PSOOptimizer |
the whole swarm, every iteration | swarm_size=20, inertia=0.7298, cognitive=1.49618, social=1.49618, max_velocity=1.0 |
BBPSOOptimizer |
the initial population, then one candidate | swarm_size=20, target, crossover=0.5, q_alpha=0.1, q_gamma=0.9 |
Pass one to optimize() with optimizer=; all take direction, seed and max_tries:
opt = mh.PSOOptimizer(space, direction="max", swarm_size=20, seed=1)
study = mh.optimize(train, space, direction="max", max_evals=300, optimizer=opt)
PSOOptimizer moves every particle once per iteration with the velocity update of Shi & Eberhart (1998).
Its defaults are the constriction constants of Clerc & Kennedy (2002); max_velocity is measured in widths
of the unit cube. A coordinate that leaves the cube stops on the wall with zero velocity. Because a whole
iteration is one batch, the candidates of an ask() can be trained in parallel.
BBPSOOptimizer follows Zhang, Lim & Liu (2023): particles move without a velocity, guided by the best
point, a second remote leader and a better neighbour, and cycle through five search operations when they
stop improving. After every iteration the leader and four other particles are refined by a root-finding
step that Q-learning chooses. These steps aim at target, the best score your function can return: 1.0
by default for direction="max" (accuracy, F1) and 0.0 for direction="min" (a loss). Set it when
your score has another bound.
Search space
| Parameter | Values | Example |
|---|---|---|
Float(low, high, log=False) |
real numbers in [low, high] |
Float(1e-4, 1e-2, log=True) |
Int(low, high, log=False) |
integers low..high, inclusive |
Int(16, 256) |
Categorical(choices) |
one of choices |
Categorical(["relu", "tanh"]) |
Optimizers search the unit cube [0, 1]^d; the space maps each point to typed parameters.
Behaviour
- A score of NaN or infinity counts as the worst possible value for the chosen direction.
- With
cache=True(the default), a repeated parameter set reuses its earlier score instead of training again and does not count againstmax_evals. - The same
seedreproduces the same search.
seed makes the search reproducible: the same seed and the same scores give the same candidates.
Training itself is reproducible only if you also seed your framework and configure it to run deterministically.
If train returns different scores for the same parameters, the rest of the search differs too.
Optimizers do not hand out a parameter set twice.
When a new candidate repeats an earlier one, the optimizer draws it again, up to max_tries attempts
(100 by default); only then may a repeat come back.
So the proposed candidates follow a different distribution than in the textbook ABC,
most visibly in small discrete spaces.
max_evals counts real calls of your function.
References
- Karaboga D., Basturk B. (2007). A powerful and efficient algorithm for numerical function optimization: artificial bee colony (ABC) algorithm. Journal of Global Optimization 39, 459–471. doi:10.1007/s10898-007-9149-x
- The official ABC page, with reference implementations: https://abc.erciyes.edu.tr/
- Kennedy J., Eberhart R. (1995). Particle swarm optimization. Proceedings of IEEE International Conference on Neural Networks, 1942–1948. doi:10.1109/ICNN.1995.488968
- Shi Y., Eberhart R. (1998). A modified particle swarm optimizer. IEEE International Conference on Evolutionary Computation, 69–73. doi:10.1109/ICEC.1998.699146
- Clerc M., Kennedy J. (2002). The particle swarm: explosion, stability, and convergence in a multidimensional complex space. IEEE Transactions on Evolutionary Computation 6(1), 58–73. doi:10.1109/4235.985692
- Kennedy J. (2003). Bare bones particle swarms. IEEE Swarm Intelligence Symposium, 80–87.
- Zhang L., Lim C. P., Liu C. (2023). Enhanced bare-bones particle swarm optimization based evolving deep neural networks. Expert Systems with Applications 230, 120642. doi:10.1016/j.eswa.2023.120642
- Oliveira I. F. D., Takahashi R. H. C. (2020). An enhancement of the bisection method average performance preserving minmax optimality. ACM Transactions on Mathematical Software 47(1), 5. doi:10.1145/3423597
meta-hpo follows the phase order of the reference ABC: employed bees, then onlooker bees, then at most one scout per cycle. It deviates on purpose in four points:
- It searches the unit cube
[0, 1]^dand decodes each point to typed parameters. - It accepts a neighbour by comparing raw minimised scores, not fitness values.
- It never hands out a parameter set again while it can still find a new one.
- It treats NaN and infinity as the worst score.
Contributing
See CONTRIBUTING.md. Changes are listed in CHANGELOG.md.
License
Metadata
Release files for meta-hpo 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| meta_hpo-0.4.0.tar.gz | 392.4 kB | With provenance | Details for meta_hpo-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| meta_hpo-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for meta_hpo-0.4.0-py3-none-any.whl |
Total release size: 423.7 kB
Release files / meta_hpo-0.4.0.tar.gz
| Download URL | meta_hpo-0.4.0.tar.gz |
|---|---|
| Size | 392.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8c2d5ca77947dc7f2fa13edaec2114842b85c005a141ad12583272820dbef782
|
|
BLAKE2b-256 checksum How to use checksums |
076f62d898b53b11b0caadd2d7ab80409972a011bafce992971d278c84b66c41
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logRelease files / meta_hpo-0.4.0-py3-none-any.whl
| Download URL | meta_hpo-0.4.0-py3-none-any.whl |
|---|---|
| Size | 31.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e1afa20e0d02b334d6db15e2386fcbbe1e2cd038ccbb03e01af3a0f2d2487361
|
|
BLAKE2b-256 checksum How to use checksums |
c355ea4c0da70fa406eab7bf577a5321a23a1d595d651d015a59aa4c77235443
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logSecurity for meta-hpo 0.4.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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for meta_hpo-0.4.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logA 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.