Skip to content

Report metrics

HyperOptimizer reads trial results from standard output. Your program can print normal logs too; each metric needs its own line with the hpo.metrics. prefix.

stdout
hpo.metrics.loss=0.182
hpo.metrics.accuracy=0.94
hpo.metrics.duration_seconds=42.1

Configure loss as Minimize or accuracy as Maximize, depending on what you want to improve. The objective name must exactly match the part after hpo.metrics.. Other metrics provide context without automatically becoming objectives.

This helper rejects booleans, strings, and nonfinite numbers, then flushes each line immediately:

import json
import math
def emit_metric(name: str, value: int | float) -> None:
if isinstance(value, bool) or not isinstance(value, (int, float)):
raise TypeError(f"{name} must be a number")
if not math.isfinite(value):
raise ValueError(f"{name} must be finite")
print(f"hpo.metrics.{name}={json.dumps(value, allow_nan=False)}", flush=True)
emit_metric("loss", 0.182)
emit_metric("duration_seconds", 42.1)

Convert a framework scalar to a Python float or int explicitly before passing it to the helper. Do not use default=str: it can turn an unsupported numeric value into a string that cannot serve as an objective.

Calculate metrics from the actual evaluation. Emit every configured objective before the process exits successfully. For an initial integration, print each objective once after the calculation finishes; this avoids confusion between intermediate and final values.

If your evaluation fails or a required result is absent, log the reason and exit nonzero. Do not substitute 0 for a missing result: for a loss objective, that could look like your best trial.

Supporting metrics such as runtime, sample count, or validation accuracy help explain the outcome. They do not enforce automatic constraints unless you configure supported objective goals and conditions.

  1. Run the exact container command locally.
  2. Confirm the metric appears on stdout as a number, without quotes or trailing commentary.
  3. Check the objective name and maximize/minimize direction in the experiment.
  4. Launch a small experiment and open one successful trial to inspect its metrics and logs.

See metric format for naming rules and nonnumeric context, or troubleshooting if the value does not appear.