Skip to content

Search spaces

See Search spaces in the guide for how these fit together.

RangeParameter pydantic-model

RangeParameter(name: str, minimum: float, maximum: float, **data: Any)

Bases: _Arithmetic, _Parameter

A number between minimum and maximum, optionally restricted to integers.

With log=True the range is searched on a logarithmic scale: values are drawn evenly in their logarithm, so 0.001 to 0.01 gets as much attention as 100 to 1000. Use it for settings that span orders of magnitude, such as learning rates and regularization strengths. A log range needs a positive minimum.

Fields:

  • name (str)
  • kind (Literal['range'])
  • minimum (float)
  • maximum (float)
  • integer (bool)
  • log (bool)

Validators:

  • _check_bounds

to_unit

to_unit(value: Any) -> Any

Where value lies in the range, from 0 at the minimum to 1 at the maximum, on the range's scale.

Accepts a number or an array of them.

from_unit

from_unit(unit: Any) -> Any

The value at position unit in the range, clipped to it, and rounded for an integer range.

The inverse of to_unit. Accepts a number, returning an int or float, or an array.

check

check(value: Any) -> float | int

value as this parameter holds it, or ValueError if it isn't one of its values.

mutate

mutate(value: float | int, rng: Generator, scale: float) -> float | int

Nudge value by Gaussian noise with a standard deviation of scale times the range width.

The result is clipped to the bounds. Integer ranges use a step of at least 1, so a mutation can always move the value. A log range is nudged in its logarithm, so the step is a proportion of the value rather than a fixed amount.

ChoiceParameter pydantic-model

ChoiceParameter(name: str, choices: list[Value], **data: Any)

Bases: _Parameter

One value picked from a list of choices, or a list of choose_n distinct choices.

Fields:

  • name (str)
  • kind (Literal['choice'])
  • choices (list[Value])
  • choose_n (int | None)

Validators:

  • _check_choose_n

check

check(value: Any) -> Value | list[Value]

value as this parameter holds it, or ValueError if it isn't one of its values.

mutate

mutate(value: Value | list[Value], rng: Generator, scale: float) -> Value | list[Value]

Draw a new value. A single choice switches to a different one when there is one to switch to.

Choices have no order, so scale does not apply.

FixedParameter pydantic-model

FixedParameter(name: str, value: Value, **data: Any)

Bases: _Parameter

A parameter held at a single value.

Fields:

  • name (str)
  • kind (Literal['fixed'])
  • value (Value)

check

check(value: Any) -> Value

value as this parameter holds it, or ValueError if it isn't one of its values.

SearchSpace pydantic-model

SearchSpace(parameters: Sequence[Parameter], constraints: Sequence[AnyConstraint] = (), conditions: Sequence[Condition] = (), **data: Any)

Bases: BaseModel

The set of parameters an objective function is optimized over, and any constraints and conditions on them.

Every point an optimizer asks for satisfies every constraint, so the objective is never evaluated where a constraint rules it out. A condition, written test >> parameters, switches parameters on only while the test holds, such as model["svm"] >> [kernel, C]; a parameter that is switched off isn't part of the point.

Config:

  • extra: forbid
  • frozen: True

Fields:

  • parameters (list[Parameter])
  • constraints (list[AnyConstraint])
  • conditions (list[Condition])

Validators:

  • _check_unique_names
  • _check_constraints
  • _check_conditions

is_conditional

is_conditional(name: str) -> bool

Whether a condition switches the parameter on and off.

active

active(params: dict[str, Any]) -> dict[str, Any]

params without the parameters the conditions switch off, in parameter order.

This is the point an objective is called with. params may hold a value for every parameter, or only for those switched on.

sample

sample(rng: Generator) -> dict[str, Any]

Draw one value for every parameter, at a point that satisfies every constraint.

Parameters the conditions switch off get a value too, which active() leaves out. Points are drawn until one is feasible. Raises InfeasibleSearchSpace after MAX_DRAWS draws in a row without one.

with_functions

with_functions(functions: dict[str, Any]) -> SearchSpace

This search space with function constraints given their functions by name, as after loading one.

Raises ValueError if a function constraint is left without one.

feasible

feasible(params: dict[str, Any]) -> bool

Whether params satisfy every constraint that applies to the parameters switched on.

broken

broken(params: dict[str, Any]) -> list[AnyConstraint]

The constraints params don't satisfy.

check

check(params: dict[str, Any]) -> dict[str, Any]

params as a complete point of this space, in parameter order, or ValueError if they aren't one.

Fixed parameters may be left out; every other parameter needs a value, unless the conditions switch it off at this point, when it must be left out.

Constraint pydantic-model

Bases: BaseModel

A limit every point an optimizer asks for must satisfy.

Build one by comparing an expression of range parameters with <= or >=, such as width * height <= 40, or with Constraint.function for anything an expression can't say.

violation

violation(params: dict[str, Any]) -> float

How far params break the constraint: 0 when they satisfy it, infinity when it can't be computed.

names

names() -> set[str]

The parameters the constraint uses.

applies

applies(params: dict[str, Any]) -> bool

Whether the constraint applies at params: not when a parameter it needs is switched off by a condition.

function staticmethod

function(function: Callable[..., float], name: str | None = None) -> FunctionConstraint

A constraint computed by function, which is satisfied when it returns zero or less.

The function takes parameters by name, like an objective function, and may take only some of them. It can use anything, such as square roots or choice parameters, but it isn't saved with the search space: a run record stores only its name.

Condition pydantic-model

Bases: BaseModel

when >> parameters: the parameters exist only while the test when holds.

Config:

  • extra: forbid
  • frozen: True

Fields:

  • when (Test)
  • parameters (list[str])

InfeasibleSearchSpace

Bases: ValueError

No point satisfying every constraint could be found.