Skip to content

Run your first experiment

This guide takes a small Python program from a local run to a cloud experiment. You will search for the value of x that gives the lowest loss, then inspect the settings behind each result.

The example uses a simple function, not a model or backtest. It needs no dataset, external service, or Python packages. Once it works, replace the calculation with your own workload.

  • Locally: Docker with Buildx, running on your computer.
  • For cloud execution: a HyperOptimizer account, a project, and an active trial or subscription with available compute. See plans and compute usage.
  • For the CLI path: the HPO CLI. You can also launch from the dashboard.

Local checks do not use your HyperOptimizer allowance. Launching cloud trials does.

Create a new directory and save these two files inside it.

main.py
import argparse
import json
import math
parser = argparse.ArgumentParser()
parser.add_argument("--hpo-x", type=float, required=True)
args = parser.parse_args()
if not math.isfinite(args.hpo_x):
raise ValueError("x must be finite")
loss = (args.hpo_x - 3.0) ** 2
print(f"hpo.metrics.loss={json.dumps(loss, allow_nan=False)}", flush=True)

The lowest possible loss is 0, at x = 3. Each run evaluates exactly one value of x and exits.

Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY main.py .
ENTRYPOINT ["python", "-u", "main.py"]

ENTRYPOINT keeps the Python command in place when trial arguments are supplied. -u makes Python print without buffering. See image commands and arguments when adapting an existing image.

Run from the directory containing the two files:

Terminal window
docker buildx build --platform linux/amd64 --load -t hpo-quickstart .
docker run --rm --platform linux/amd64 hpo-quickstart --hpo-x=2

Expected output:

stdout
hpo.metrics.loss=1.0

Try --hpo-x=3 as well: the loss should be 0.0. This checks that the argument reaches your calculation and the metric changes with it.

Choose one path. Both run the same image with the same parameter and objective.

Log in, then create a configuration for an existing project:

Terminal window
hpo login
hpo init

Select your project when prompted. If you need a new project, use hpo init --create-project quickstart instead of hpo init.

Replace the generated hpo.toml with the following, keeping your actual project ID in [target]:

hpo.toml
schemaVersion = 2
algorithm = "bayesian"
machineSizeId = "nano"
maxTrialCount = 10
parallelTrialCount = 1
maxFailedTrialCount = 3
maxRuntimeSecondsPerTrial = 60
fixedArgs = []
requiredEnv = []
[target]
projectId = "YOUR_PROJECT_ID"
[build]
context = "."
dockerfile = "Dockerfile"
repository = "quickstart"
platform = "linux/amd64"
[[parameters]]
name = "x"
slug = "x"
type = "double"
min = -5
max = 5
[[objectives]]
name = "loss"
type = "minimize"

Add .hpo/ to your .gitignore, then validate:

Terminal window
hpo fmt
hpo validate

When you are ready to use cloud compute, launch:

Terminal window
hpo optimize

The CLI builds the image, pushes it to your workspace registry, launches an experiment, and displays trial updates. Open the experiment URL it prints to see results in the dashboard. Validation alone does not launch trials or reserve capacity.

Open a successful trial and confirm that:

  • Its parameters contain x within your configured range.
  • Its metrics contain a numeric loss.
  • Its logs include the same hpo.metrics.loss line.
  • Lower loss corresponds to x closer to 3.

Ten trials are enough to check the integration; they do not guarantee the exact minimum. If trials fail or metrics are absent, use troubleshooting.

Replace the loss calculation with one evaluation of your model, strategy, or simulation. Update the parser, parameter slugs, and objective names together. Keep the local check before each new cloud experiment.

Next: read and compare results, connect a Python workload, or adapt any Docker image.