Skip to main content

meta-hpo

CI PyPI License: MIT Python

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. Two optimizers are available, the Artificial Bee Colony (ABC, the default) and Particle Swarm Optimization (PSO); 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

Pass one to optimize() with optimizer=; both 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.

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 against max_evals.
  • The same seed reproduces 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

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]^d and 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

MIT

Metadata

Release files for meta-hpo 0.3.1

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

Source distribution (sdist)

Source distribution for meta-hpo 0.3.1
File Size Uploaded Provenance Actions
meta_hpo-0.3.1.tar.gz 368.1 kB With provenance Details for meta_hpo-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for meta-hpo 0.3.1
File Interpreter ABI Platform Actions
meta_hpo-0.3.1-py3-none-any.whl Python 3 none any Details for meta_hpo-0.3.1-py3-none-any.whl

Total release size: 390.3 kB

Release files / meta_hpo-0.3.1.tar.gz

Download URL meta_hpo-0.3.1.tar.gz
Size 368.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8b906614d92d8198e8a9608108f98a11d043338a05994b303adbcd866a8172ec
BLAKE2b-256 checksum
How to use checksums
1fda6163d4e6b8a68cb9a33ceafb869b5665302e3db4f795dc20d00d06d41e65
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 4, 2026.

Transparency log

Release files / meta_hpo-0.3.1-py3-none-any.whl

Download URL meta_hpo-0.3.1-py3-none-any.whl
Size 22.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
68f1d8eefc98d1c1a67f717f69eddd7dc85683acdc92921ba37b00db0eb0d23e
BLAKE2b-256 checksum
How to use checksums
c6a5d85613d75afb4eb8124f77432fb826e14ecdbb1f1a52b06c3eb08b098762
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 4, 2026.

Transparency log

Security for meta-hpo 0.3.1

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.

Select from 2 files

Attestation for meta_hpo-0.3.1.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 4, 2026.

Transparency log

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