The Optimizer class#

Aliases#

halerium.Optimizer
halerium.core.Optimizer
halerium.core.objectives.Optimizer
class Optimizer(graph, adjustables, cost_function, reduce_dynamic_cost=<class 'halerium.core.operator.operators.Sum'>, data=None, method='ForwardOptimizer', compiler=None, solver=None, batch_size=None, model_args=None, solver_args=None, n_samples=100, name='Optimizer', description=None, copy_graph=True)#

Class for computing optimal values.

Class for computing optimal values.

Parameters:
  • graph (halerium.core.Graph) – The graph for which to compute optimal values.

  • adjustables – The variables to adjust to optimize the objective. Starting values and boundaries for variables can also be provided (see notes).

  • cost_function (callable) – The function that takes the graph as argument and returns an operator whose value is the objective function to minimize (see notes).

  • reduce_dynamic_cost (callable) – The function to apply to dynamic costs to reduce to a scalar (see notes).

  • data (dDict, haleriu.core.DataLinker, optional) – Any data constraining the optimization. Either dictionary with variables as keys and data arrays as values, or a DataLinker holding links to the variables in graph. The default is None.

  • method ({'ForwardOptimizer'}, optional) – The solving method. Currently, only a ForwardOptimizer is supported. See the corresponding model class for further information.

  • compiler (halerium.core.compiler.compiler_base.CompilerBase, optional) – Which compiler to use to create the numerical arrays of the model. If None, a TFCompiler instance is created. The default is None.

  • solver (str, None) – The algorithm used to solve the model. Current choices are ‘L-BFGS’ and ‘Adam’ (see notes). The default is None, in which case the choice depends on whether the cost is deterministic (‘L-BFGS’) or not (‘Adam’).

  • batch_size (int, None) – The number of examples to average over in each step during optimization. The default is None, in which case the batch size depends on whether the cost is deterministic.

  • model_args (dict, optional) – Model arguments that depend on the specified method. See the corresponding model classes for further information.

  • solver_args (dict, optional) – The arguments to pass to the model’s solver function. The default is None.

  • n_samples (int, optional) – The number of examples to compute predictions. The default is 100.

  • name (str, optional) – The name of the objective.

  • description (str, optional) – The description of the objective.

  • copy_graph (bool, optional) – Whether the objective should make a copy of the graph for its own use, or just keep the graph itself as attribute. Users should leave this set to the default True, unless they are certain that the graph won’t be altered by the user or other code.

Raises:

ValueError – If the supplied method is unknown an error is raised.

__call__(fetches=None, n_samples=None)#

Predict values for given fetches.

Parameters:
  • fetches (halerium.core.scope.Scopee, dict, list or tuple, optional) – The graph elements to predict values for. If no fetches are provided, the default is the graph itself and all its subgraphs, entities, and (static) variables. Note, however, values are only computed for (static) variables of the graph. For other graph elements, the prediction returns None.

  • n_samples (int, optional) – The number of samples to estimate the prediction from. The default is to use the number provided at initialization.

Returns:

The predictions for the graph elements. Note that for elements that are not (Static)Variables, None will be returned.

Return type:

prediction

Examples

>>> from halerium.core import Graph, StaticVariable
>>> from halerium import Optimizer
>>>
>>> with Graph("g") as g:
>>>     s = StaticVariable("s", mean=0, variance=1)
>>>
>>> def cost(graph):
>>>     return (graph.s - 3) ** 2
>>>
>>> optimizer = Optimizer(graph=g,
>>>                       adjustables={g.s: [0, -5, 5]},
>>>                       cost_function=cost)
>>>
>>> optimizer(g.s, n_samples=1)
3.0
dump_dict(value_postprocessor=None)#

Dump a dict with information on the objective.

The dict returned contains the name, description and the values resulting from a call of the objective. Additional keys included are used by the GUI for appropriately displaying the results of the objective.

Parameters:

value_postprocessor (optional) – A function to apply to the values returned by the call of the objective. The default is None, in which case no post-processing is done.

Returns:

result – A dictionary containing the name, description, etc. of the objective.

Return type:

dict