qewton.optim.parameters package

Module contents

Submodules

qewton.optim.parameters.categorical_hyperparameter module

class qewton.optim.parameters.categorical_hyperparameter.CategoricalHyperparameter(categories: tuple | list, state: HyperParameterState = HyperParameterState.OPTIMIZE, name: str = '', initial_value=None, active_when: None | HyperParameterCondition = None)[source]

Bases: HyperParameter

A HyperParameter that represents any general value

Parameters:
  • categories (tuple | list) – A list or tuple of all possible values.

  • initial_value (float | None, optional) – An initial value. Defaults to None.

  • state (HyperParameterState, optional) – If this parameter is fixed or should be optimized. Defaults to HyperParameterState.OPTIMIZE.

  • name (str, optional) – The name of the parameter. Defaults to “”.

  • active_when (None | HyperParameterCondition, optional) – A condition that specifies when this Hyperparameter should be active. The condition depends on the values of other Hyperparameters. Accordingly this parameters is only sampled/set when the condition is fulfilled.

property categories
set_value(new_value)[source]
sample_parameter_random()[source]

Samples a random value from the given parameter range.

sample_parameter_grid(n: int) → list[source]

Creates a point grid in the given parameter range.

Parameters:

n (int) – The number of grid points.

Returns:

A list containing the grid of values.

Return type:

list

sample_from_unit(x: float)[source]

Maps a value x from [0,1) to a value in the given parameter range.

class qewton.optim.parameters.categorical_hyperparameter.BooleanHyperparameter(initial_value: bool, state: HyperParameterState = HyperParameterState.OPTIMIZE, name: str = '', active_when: None | HyperParameterCondition = None)[source]

Bases: CategoricalHyperparameter

Represents a boolean HyperParameter.

qewton.optim.parameters.dag module

class qewton.optim.parameters.dag.HyperParameterDAG(hyperparameters: set[HyperParameter])[source]

Bases: object

A directed acyclic graph (DAG) representing HyperParameter dependencies.

build_graph(hyperparameters: set[HyperParameter])[source]
sort()[source]
create_random_samples(n_samples: int) → list[dict[str, Any]][source]
create_grid_samples(n_samples: int) → list[dict[str, Any]][source]

qewton.optim.parameters.helpers module

class qewton.optim.parameters.helpers.HyperParameterState(*values)[source]

Bases: Enum

Denotes the state of a HyperParameter and how it is handled in the tuning process.

FIXED

The Hyperparameter does not change and is ignored while tuning.

OPTIMIZE

The Hyperparameter is changed in the tuning process.

INFERRED

The Hyperparameter is open until matching the configurations of a graph infers it from the connected data; ignored while tuning.

FIXED = 1
OPTIMIZE = 2
INFERRED = 3
class qewton.optim.parameters.helpers.HyperParameterScale(*values)[source]

Bases: Enum

Influence the sampling of HyperParameters.

LINEAR

A uniform sampling in the given parameter range.

LOG

A uniform logarithmic sampling in the given parameter range.

POWER

Sampling via a power law x**power, where power can be set in the HyperParameter class. power > 1 leads to a bias toward lower values, p < 1 leads to a bias toward higher values.

LINEAR = 'linear'
LOG = 'log'
POWER = 'power'
class qewton.optim.parameters.helpers.HyperParameterCondition(func, deps: set)[source]

Bases: object

A condition that describes when HyperParameters should be active.

Parameters:
  • func (Callable) – The function obtains a config dictionary of HyperParameter names and current values and returns true if some condition is fulfilled.

  • deps (set[HyperParameter]) – All HyperParameters that this condition depends on.

evaluate(config: dict) → bool[source]

Evaluates the condition.

Parameters:

config (dict) – A config dictionary of HyperParameter names and current values

qewton.optim.parameters.hyperparameter_base module

class qewton.optim.parameters.hyperparameter_base.HyperParameter(parameter_range: tuple | list, initial_value, state: HyperParameterState = HyperParameterState.OPTIMIZE, name: str = '', active_when: None | HyperParameterCondition = None, default_grid: int | list = 0)[source]

Bases: Serializable

Abstract HyperParameter class that represents tunable parameters.

Parameters:
  • parameter_range (tuple | list) – Allowed range of values for this parameter.

  • initial_value (_type_) – Initial value for this parameter.

  • state (HyperParameterState, optional) – If this value should be fixed or be optimized. Defaults to HyperParameterState.OPTIMIZE.

  • name (str, optional) – Internal name of this parameter. Defaults to “”.

  • active_when (HyperParameterCondition, optional) – A condition that specifies when this Hyperparameter should be active. The condition depends on the values of other Hyperparameters. Accordingly this parameters is only sampled/set when the condition is fulfilled. See both HyperParameterCondition and HyperParameterDAG.

classmethod from_value(x, name: str | None = None) → HyperParameter[source]

Creates a Hyperparameter from a given value.

Parameters:
  • x (_type_) – The object that we want to transform to a HyperParameter.

  • name (str | None, optional) – Internal name of this parameter. Defaults to None. If the input already is an HyperParameter, the name will only be changed if the current name is empty string and the value is not None.

Returns:

The HyperParameter object for the given value.

By default this parameter is fixed and will not be optimized.

Return type:

HyperParameter

property is_fixed: bool

Returns: bool: Whether this parameter is left alone while tuning: it is

fixed, or inferred from data.

classmethod size_from(x, name: str) → HyperParameter[source]

A size that fixes how a node is built: the given one (see from_value()), or, for None, an open one that is inferred from the connected data (see inferred()).

Parameters:
  • x – The size, a HyperParameter, or None.

  • name – Internal name of this parameter.

Returns:

The size as a HyperParameter.

Return type:

HyperParameter

property is_open: bool

Whether this parameter waits to be inferred from the connected data: state INFERRED and no value yet.

classmethod inferred(name: str) → HyperParameter[source]

A whole-number parameter that stays open (value None) until it is inferred from the connected data (infer()).

Parameters:

name – Internal name of this parameter.

Returns:

The open parameter, in state INFERRED.

Return type:

HyperParameter

infer(value) → bool[source]

Learn the value from the connected data: an open parameter in state INFERRED takes it; any other is compared with it. Called by the dims bound to this parameter once their size is known.

Parameters:

value – The value the data needs.

Returns:

Whether the value changed, i.e. an open parameter was

inferred.

Return type:

bool

Raises:

DataConfigMismatchError – If the parameter has another value, e.g. inferred at another place of a graph.

property value

Returns: _type_: The current value of this HyperParameter.

property tuning_grid: list

Returns a grid of values for tuning this parameter.

is_active(config: dict = {}) → bool[source]

Checks whether this HyperParameter is active under the given configuration. This is the case if there is some connection to HyperParameters. The concrete values of the Parameters are provided in the config.

set_value(new_value)[source]
sample_parameter_random()[source]

Samples a random value from the given parameter range.

sample_from_unit(x: float)[source]

Maps a value x from [0,1) to a value in the given parameter range.

sample_parameter_grid(n: int) → list[source]

Creates a point grid in the given parameter range.

Parameters:

n (int) – The number of grid points.

Returns:

A list containing the grid of values.

Return type:

list

property dependencies: set[HyperParameter]
class qewton.optim.parameters.hyperparameter_base.Arithmetic(name: str, constant=None, reflected: bool = False)[source]

Bases: Serializable

An arithmetic operation (operator.add, sub, mul, truediv) of the values of HyperParameters, with a constant number as one operand if one is given. Saved by its name.

Parameters:
  • name (str) – The name of the operation in the operator module.

  • constant (float | int | None, optional) – A constant operand.

  • reflected (bool, optional) – Whether the constant is the first operand.

class qewton.optim.parameters.hyperparameter_base.HyperParameterOperation(operation, dependencies: list[HyperParameter], name: str = '')[source]

Bases: HyperParameter

A HyperParameter that is defined as an operation on other HyperParameters.

Parameters:
  • operation (callable) – A function that takes the values of the dependencies and returns the value of this HyperParameter.

  • dependencies (list[HyperParameter]) – The HyperParameters that this HyperParameter depends on. The HyperParameters need to be provided in the order that the operation expects them!

property dependencies: set[HyperParameter]
set_value(new_value)[source]
property is_open: bool

Whether one of the dependencies waits to be inferred.

property value

The operation on the values of the dependencies; None while one of them is open.

sample_parameter_random()[source]

Samples a random value from the given parameter range.

sample_from_config(config)[source]
sample_from_unit(x: float)[source]

Maps a value x from [0,1) to a value in the given parameter range.

sample_parameter_grid(n: int) → list[source]

Creates a point grid in the given parameter range.

Parameters:

n (int) – The number of grid points.

Returns:

A list containing the grid of values.

Return type:

list

property tuning_grid: list

Returns a grid of values for tuning this parameter.

qewton.optim.parameters.number_hyperparameter module

class qewton.optim.parameters.number_hyperparameter.ContinuousHyperparameter(parameter_range: tuple[float, float] | list[float], initial_value: float | None = None, state: HyperParameterState = HyperParameterState.OPTIMIZE, name: str = '', scale: HyperParameterScale = HyperParameterScale.LINEAR, power: float = 2.0, active_when: None | HyperParameterCondition = None, default_grid: int | list = 5)[source]

Bases: HyperParameter

Represents a continuous HyperParameter given in the interval.

Parameters:
  • parameter_range (tuple | list) – A tuple or list of two floats.

  • initial_value (float | None, optional) – An initial value. Defaults to None.

  • state (HyperParameterState, optional) – If this parameter is fixed or should be optimized. Defaults to HyperParameterState.OPTIMIZE.

  • name (str, optional) – The name of the parameter. Defaults to “”.

  • scale (HyperParameterScale, optional) – If some special scaling should be applied for optimizing this parameter. Defaults to HyperParameterScale.LINEAR.

  • power (float, optional) – The power if power scaling law is used. Defaults to 2.0.

  • active_when (None | HyperParameterCondition, optional) – A condition that specifies when this Hyperparameter should be active. The condition depends on the values of other Hyperparameters. Accordingly this parameters is only sampled/set when the condition is fulfilled.

  • default_grid (int | list, optional) – The grid used for a GridSearchTuning. Either an int can be provided to build the grid automatically, or the grid itself can be passed. Defaults to 5.

Raises:

ValueError – If the parameters range has more than two values.

sample_parameter_random()[source]

Samples a random value from the given parameter range.

sample_parameter_grid(n: int) → list[source]

Creates a point grid in the given parameter range.

Parameters:

n (int) – The number of grid points.

Returns:

A list containing the grid of values.

Return type:

list

sample_from_unit(x: float)[source]

Maps a value x from [0,1) to a value in the given parameter range.

class qewton.optim.parameters.number_hyperparameter.DiscreteHyperparameter(parameter_range: tuple[int, int] | list[int], initial_value: int | None = None, state: HyperParameterState = HyperParameterState.OPTIMIZE, name: str = '', scale: HyperParameterScale = HyperParameterScale.LINEAR, power: float = 2.0, active_when: None | HyperParameterCondition = None, default_grid: int | list | None = 5)[source]

Bases: ContinuousHyperparameter

Represents a discrete HyperParameter given in the interval.

Parameters:
  • parameter_range (tuple | list) – A tuple or list of two ints.

  • initial_value (float | None, optional) – An initial value. Defaults to None.

  • state (HyperParameterState, optional) – If this parameter is fixed or should be optimized. Defaults to HyperParameterState.OPTIMIZE.

  • name (str, optional) – The name of the parameter. Defaults to “”.

  • scale (HyperParameterScale, optional) – If some special scaling should be applied for optimizing this parameter. Defaults to HyperParameterScale.LINEAR.

  • power (float, optional) – The power if power scaling law is used. Defaults to 2.0.

  • default_grid (int | list, optional) – The grid used for a GridSearchTuning. Either an int can be provided to build the grid automatically, or the grid itself can be passed. Defaults to 5.

Raises:

ValueError – If the parameters range has more than two values.

set_value(new_value)[source]
sample_parameter_random()[source]

Samples a random value from the given parameter range.

sample_parameter_grid(n: int) → list[source]

Creates a point grid in the given parameter range.

Parameters:

n (int) – The number of grid points.

Returns:

A list containing the grid of values.

Return type:

list

sample_from_unit(x: float)[source]

Maps a value x from [0,1) to a value in the given parameter range.

qewton.optim.parameters.trainable_parameters module

class qewton.optim.parameters.trainable_parameters.TrainableParameters(node_id: int, parameters, **kwargs)[source]

Bases: _TrainableParameterBase

A class to represent trainable parameters of a node.

classmethod create_empty(node_id: int) → TrainableParameters[source]
property empty: bool
property node_id: int
property parameters
property options
update_option(key, value)[source]
class qewton.optim.parameters.trainable_parameters.TrainableParametersCollection(*groups: _TrainableParameterBase)[source]

Bases: _TrainableParameterBase

add(param: TrainableParameters)[source]
extend(groups: _TrainableParameterBase | Iterable[_TrainableParameterBase])[source]
property empty: bool
property parameters
update_option(key, value)[source]