meta-hpo
Metaheuristic hyperparameter optimization for Python.
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. Several metaheuristics share one interface (Optimizers), and every run is reported on the console and in a results folder (Results). A run that stops half way continues from that folder as if it had never stopped (Resuming a run).
Installation
pip install meta-hpo
meta-hpo needs Python 3.10+; pip installs numpy and matplotlib with it.
Quick start
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 train(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(train, space, direction="max", max_evals=300, seed=1)
print(study.best_params, study.best_score)
Here train is a closed-form score with its peak at lr=1e-3, kernel=5, units=128 and
dropout=0.2, so the example runs as it is. In your code train builds and trains the model with these
parameters and returns its score: a Keras or PyTorch training run, cross-validation, an F1 score.
Its training budget is entirely yours; meta-hpo only counts evaluations.
If the run stops before it ends (Ctrl+C, a crash, a power cut, the computer switched off for the night),
no finished evaluation is lost. Pass train and the results folder of the run to mh.resume and the
search goes on as if it had never stopped (Resuming a run):
study = mh.resume(train, "hpo_results/2026-11-09_18-26-15_abc")
print(study.best_params, study.best_score)
Results
optimize() prints one line per generation and, while it runs, writes everything to hpo_results/ in the
working directory:
[meta-hpo] run 1 | ABCOptimizer | direction=max | max_evals=300 | seed=1 | 4 parameters
gen 0 | evals 15/300 | best 0.884134 (trial 13) | gen best 0.884134 mean 0.807182 std 0.0518115 | ...
gen 29 | evals 300/300 | best 0.92 (trial 288) | gen best 0.919993 mean 0.913963 std 0.00801146 | ...
[meta-hpo] run 1 finished (budget) after 300 calls in 00:00:00 | best 0.92 at trial 288 | {...}
hpo_results/2026-11-09_18-26-15_abc/ one folder per optimize() call: start time and optimizer
├── config.json search space, optimizer settings, budget, seed and stopping rules
├── summary.json best result, stop reason, calls and duration
├── report.md readable summary with the plots
└── run_001/
├── best.json the best so far, replaced on every improvement
├── trials.csv, trials.jsonl every trial: params, score, phase, generation, time, duration
├── state.json start time and random generator state, for resuming
├── best_history.csv every improvement of the best
├── generations.csv score and population statistics per generation
├── population.csv the population after every generation
├── parameters.csv, categories.csv how each parameter relates to the score
└── plots/ convergence, diversity, distributions, correlations, ...
best.jsonis replaced on every improvement, so the best so far survives a crash.- Ctrl+C stops the search and returns the study so far.
- An error raised by your function is re-raised after the results are written.
- A stopped run continues from this folder with
mh.resume(Resuming a run).
| Setting | Effect |
|---|---|
output="hpo_results" |
folder for the results; None writes nothing |
verbose=1 |
0 silent, 1 one line per generation, 2 one line per trial as well |
log_every=1 |
print every n-th generation |
callbacks=None |
your own mh.Callback objects receive the same events |
The environment variables METAHPO_OUTPUT and METAHPO_VERBOSE override output and verbose.
Repeated runs
studies = mh.optimize(train, space, direction="max", max_evals=300, runs=5, seed=[1, 2, 3, 4, 5])
runsrepeats the search; every run gets a fresh optimizer, its own cache and the fullmax_evals. The result is a list with oneStudyper run (a singleStudywhenruns=1).seedis one seed per run, one integer that every run uses, orNonefor random runs.- With several runs, pass your own optimizer as a function of the seed:
optimizer=lambda seed: mh.PSOOptimizer(space, direction="max", seed=seed). - All runs go into one results folder (
run_001,run_002, ...) with a summary and plots that compare the runs.
Stopping early
Stopping rules end a search before max_evals is used up. The first rule that says stop ends the run.
study = mh.optimize(
train,
space,
direction="max",
max_evals=500,
stop=[mh.Patience(60), mh.MaxTime(hours=8), mh.TargetScore(0.95)],
)
| Rule | Stops when | Stop reason |
|---|---|---|
Patience(evals, min_delta=0.0) |
evals calls in a row brought no improvement of more than min_delta |
patience |
MaxTime(seconds, minutes, hours) |
the run has searched that long | time |
TargetScore(score) |
the best score reaches score |
target |
- Rules are checked after every evaluation and never interrupt a running one.
- Cached repeats do not count as calls for
Patience, as formax_evals. - Every run works on its own copy of the rules.
- The rule that ended a run is in
study.stop_reasonand insummary.json. - A rule of your own subclasses
mh.Stopper:
class TooManyFailures(mh.Stopper):
reason = "failures"
def should_stop(self, state):
return sum(t.failed for t in state.study.trials) >= 20
Resuming a run
A run that stopped (Ctrl+C, a crash, a power cut) continues from its results folder and gives exactly the search an uninterrupted run would have given, also without a seed.
study = mh.resume(train, "hpo_results/2026-11-09_18-26-15_abc")
print(study.best_params, study.best_score)
Stopped after 120 of its 300 evaluations and resumed, the Quick start run restores those 120 trials without training again and ends on the same lines as the uninterrupted run in Results:
[meta-hpo] run 1 | ABCOptimizer | direction=max | max_evals=300 | seed=1 | 4 parameters
[meta-hpo] run 1 | resumed | 120 trials restored (120 calls) | best 0.919322
gen 11 | evals 125/300 | best 0.919326 (trial 122) | gen best 0.919326 mean 0.900067 std 0.0231047 | ...
gen 29 | evals 300/300 | best 0.92 (trial 288) | gen best 0.919993 mean 0.913963 std 0.00801146 | ...
[meta-hpo] run 1 finished (budget) after 300 calls in 00:00:00 | best 0.92 at trial 288 | {...}
- The folder provides the space, the optimizer and its settings, the budget, the seeds and every finished
trial; only
trainis passed again. - The trial that was running when the run stopped is evaluated again; finished trials are not.
max_evals=extends a run to a larger budget.- The stopping rules of the run apply again;
stop=replaces them, for examplestop=mh.MaxTime(hours=16)for a run thatMaxTime(hours=8)ended. Search time counts over all sessions. optimizer=is needed for an optimizer of your own andspace=for Categorical choices that JSON cannot store, such as classes or functions.- With several runs, the finished runs are restored, the unfinished run continues and the runs that never started run with their recorded seeds.
- A folder whose runs finished returns its studies without calling
train. - The results go on in the same folder.
Examples
Runnable scripts that tune a small convolutional network on CIFAR-10 are in examples/:
Ask and tell
For full control over where and how candidates are evaluated, ask for candidates and tell their scores.
ABCOptimizer's first ask() returns the initial population (colony_size // 2 candidates) and every later
ask() one candidate, so count evaluations, not rounds, to set a budget:
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; 300 rounds of one ask() and one tell() would make 304.
Optimizers
| Optimizer | Description |
|---|---|
ABCOptimizer |
Artificial Bee Colony, the default |
PSOOptimizer |
classic particle swarm optimization |
BBPSOOptimizer |
enhanced bare-bones PSO with Q-learning refinement |
opt = mh.PSOOptimizer(space, direction="max", seed=1)
study = mh.optimize(train, space, direction="max", max_evals=300, optimizer=opt)
Settings, algorithm details and references: docs/optimizers.md.
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.
Bounds may be Python or numpy numbers. A choice can be any object (a string, a number, a function, a numpy array); your function receives the choice itself, not a copy, so it must not change a mutable choice in place.
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.
max_evals counts real calls of your function. The same seed and the same scores give the same
candidates; training itself is reproducible only if you also seed your framework and make it
deterministic, and different scores for the same parameters change the rest of the search.
Optimizers do not hand out a parameter set twice: a repeat is drawn again, up to max_tries times (100
by default), before it may come back. So candidates follow a different distribution than in the textbook
algorithms, most visibly in small discrete spaces.
Warnings
meta-hpo warns with mh.MetaHpoWarning, a UserWarning, once per condition and call:
seed=given together with an optimizer object, which ignores it, before the search starts.- Runs that ended without a finite score (only NaN or infinite scores), when the call has finished.
- Plots that could not be drawn (
notesinsummary.jsonlists them), when the call has finished.
The warnings after the search come once every result file is written, so turning them into errors costs no
result. warnings.filterwarnings("ignore", category=mh.MetaHpoWarning) silences them;
warnings.simplefilter("error", mh.MetaHpoWarning) or python -W error::metahpo.MetaHpoWarning turns them
into errors. Deprecations will use FutureWarning.
Contributing
See CONTRIBUTING.md; changes are listed in CHANGELOG.md.
License
Metadata
Release files for meta-hpo 0.8.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.8.0.tar.gz | 531.8 kB | With provenance | Details for meta_hpo-0.8.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| meta_hpo-0.8.0-py3-none-any.whl | Python 3 | none | any | Details for meta_hpo-0.8.0-py3-none-any.whl |
Total release size: 609.3 kB
Release files / meta_hpo-0.8.0.tar.gz
| Download URL | meta_hpo-0.8.0.tar.gz |
|---|---|
| Size | 531.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
141c656482d8e87af14bde7fca00e0b49198b8218567c3ec2696e121eee8c540
|
|
BLAKE2b-256 checksum How to use checksums |
06b8f5d6a1a6567ef52e6c85674c630120d47689f88c0472101c3e2153cfb2a2
|
| 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 18, 2026.
Transparency logRelease files / meta_hpo-0.8.0-py3-none-any.whl
| Download URL | meta_hpo-0.8.0-py3-none-any.whl |
|---|---|
| Size | 77.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
85dad81f5b12374ca2665d75e9e443917d5a7bb695ca04c9439491da6af4291c
|
|
BLAKE2b-256 checksum How to use checksums |
5fa1189c4fdeeb6d4cf29a0561d1e385f23fd6ecf765e43e92ad459e11e71144
|
| 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 18, 2026.
Transparency logSecurity for meta-hpo 0.8.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.8.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 18, 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.