qgon.hpo
Searches hyperparameters (HPO) by running clones of a finished base run with different parameter values.
import qgon
# the base run's training code logged the metric val/acc with run.log_metrics({"acc": acc}, step=ep, prefix="val")opt = qgon.hpo.optimize( base_run="<base run id>", # a finished base run (this script does not call qgon.init()) space=[qgon.hpo.log_uniform("lr", 1e-4, 1e-1), qgon.hpo.choice("batch", [32, 64])], objective="val/acc", mode="max", max_runs=8, concurrency=2,)opt.wait()print(opt.best().params, opt.best().value)- Each trial is a clone of the base run with only the parameters changed, and an agent on the GPU queue (default
gpu) runs it. - The search controller runs inside this process (your workspace). When this process ends, the search stops too.
- Use the parameter names you passed to run.params({...}). The internal section names are found automatically.
import qgon.hpouniform
functionA float between lo and hi; with step, only values on that grid (grid search needs step).
uniform( name: str, lo: float, hi: float, step: Optional[float] = None,) -> _ParamIn the app · Build › Hyperparameter search
log_uniform
functionA float between lo and hi drawn evenly on a log scale, for values that span orders of magnitude such as a learning rate (for example 1e-4 to 1e-1).
log_uniform(name: str, lo: float, hi: float) -> _ParamIn the app · Build › Hyperparameter search
integer
functionAn integer between lo and hi, hi included.
integer(name: str, lo: int, hi: int, step: int=1) -> _ParamIn the app · Build › Hyperparameter search
choice
functionOne of the given values.
choice(name: str, values: Sequence[Any]) -> _ParamIn the app · Build › Hyperparameter search
Starts a search and returns without waiting; call opt.wait() to wait for it.
optimize( base_run: Union[str, Run, Task], space: Sequence[_Param], objective: str, mode: str = 'max', *, queue: Optional[str] = None, max_runs: Optional[int] = None, concurrency: int = 2, method: Union[str, type] = 'random', name: Optional[str] = None, project: Optional[str] = None, use: str = 'last', time_limit_min: Optional[float] = None, run_time_limit_min: Optional[float] = None, local: bool = False, controller_queue: Optional[str] = None, poll_seconds: float = 15, **engine_kwargs: Any,) -> Optimizationspace- a list of uniform/log_uniform/integer/choice. Use the names you passed to run.params.
objective- the target metric, written as "graph/line". acc logged with run.log_metrics({"acc": acc}, step=ep, prefix="val") is "val/acc". A metric whose graph and line share one name (run.log("val_acc", ...)) or run.summary("val_acc", ...) can be given by the name alone, such as "val_acc".
mode- "max" (higher is better) or "min".
use- "last" (the last value, default) or "best" (the best value recorded).
queue- the queue for trials (default gpu; environment variable QGON_QUEUE).
max_runs- the maximum number of trials (default 10 for random and optuna; every combination for grid).
concurrency- how many trials run at once (default 2).
method- "random" (default), "grid" or "optuna" (when installed).
time_limit_min / run_time_limit_min- the time limit in minutes for the whole search / for one trial.
local- if True, trials also run one after another in this process without a queue (for testing).
controller_queue- runs the controller on an agent of this queue, for administrators (the services queue is admin-only). This process then ends here and the script runs again on the agent.
engine_kwargs- passed as they are to the engine (HyperParameterOptimizer).
In the app · Build › Hyperparameter search
Optimization
classA search that is running or has finished.
Optimization( engine: HyperParameterOptimizer, task: Optional[Task], names: Dict[str, _Param], objective: tuple, mode: str, extremum: bool, own_task: bool,)| Member | What it does |
|---|---|
id -> Optional[str] | Id of the search (controller) task, shown under this name on the hyperparameter search screen. |
url -> Optional[str] | Address of this search in the qgon web app; it opens this search on the hyperparameter search screen. |
running -> bool | True while the search is running. |
wait(timeout: Optional[float]=None) -> bool | Waits until the search ends (timeout in seconds), returns True if it ended and marks the search task completed; Ctrl-C while waiting stops the search, aborting running trials, and re-raises the exception. |
stop() -> None | Stops the search; running trials are aborted. |
top(n: int=5) -> List[Trial] | Returns n completed trials ordered from the best objective value. |
best() -> Optional[Trial] | Returns the best trial, or None if no trial has completed. |
engine -> HyperParameterOptimizer | The underlying engine optimizer object, for advanced use. |
In the app · Build › Hyperparameter search
Trial
dataclassThe result of one trial.
Trial()| Field | Type | Notes |
|---|---|---|
id | str | - |
params | Dict[str, Any] | - |
value | Optional[float] | - |
metrics | Dict[str, Any] | - |
| Member | What it does |
|---|---|
run -> Run | The trial's Run, for looking further at its metrics, artifacts and models. |
In the app · Build › Hyperparameter search
kwargs_from_spec
functionConverts a search spec into optimize() arguments; unknown keys or invalid values raise ValueError with a readable message.
kwargs_from_spec(spec: dict) -> Dict[str, Any]In the app · Build › Hyperparameter search