The Generator class#

Aliases#

halerium.Generator
halerium.core.Generator
halerium.core.objectives.Generator
class Generator(graph, data=None, method='Forward', compiler=None, model_args=None, solver_args=None, measure='mean', n_samples=1, unpack_single_examples=True, name='Generator', description=None, copy_graph=True)#

Class for generating examples.

Class for generating examples.

Parameters:
  • graph (halerium.core.Graph) – The graph for which to generate examples.

  • data (dict, halerium.core.DataLinker, optional) – The input data for the generating samples. 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 ({'Forward', 'MAPFisher', 'MAP', 'ADVI', 'MGVI', 'MCMC'}, optional) –

    The solving method. According to the chosen method, either a ForwardModel , MAPFisherModel, MAPModel, ADVIModel, MGVIModel, or MCMCModel instance is created. Note

    • ’Forward’: fast, but ignores any backward influence of data,

    • ’MAP’, ‘MAPFisher’: fast, but no variance in samples,

    • ’ADVI’, ‘MGVI’, ‘MCMC’: slower.

    See the corresponding model class for further information. The default is ‘Forward’.

  • 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.

  • 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 generate in each call. The default is 1.

  • unpack_single_examples (bool) – Whether to unpack examples when n_examples=1. If not, variable values are returned as lists with one element. (Note that for n_examples > 1, values for variables are returned as lists with n_examples elements.)

  • 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)#

Generate examples for given fetches.

Parameters:
  • fetches (halerium.core.scope.Scopee, dict, list or tuple, optional) – The graph elements to generate examples 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 generated for (static) variables of the graph. For other graph elements, the function returns None.

  • n_samples (int, optional) – The number of samples to generate. 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:

examples

Examples

>>> from halerium.core import Graph, Variable
>>> from halerium import Generator
>>>
>>> g = Graph("g")
>>> with g:
>>> x = Variable("x", mean=0, variance=1)
>>> y = Variable("y", mean=x + 1, variance=1)
>>>
>>> generator = Generator(g, {g.x: [0, 1, 2]})
>>>
>>> generator(g.y)
array([2.01309258, 2.9269174 , 3.10647856])
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