The MAPModel class#

Aliases#

halerium.core.model.MAPModel
class MAPModel(graph, data=None, compiler=None, initial_values=None, initial_source_values=None, random_initial_value_scale=0.01, copy_graph=True, model_graph_options=None)#

Model providing maximum-a-posteriori predictions.

Model providing maximum-a-posteriori predictions.

The model is solved according to the common maximum a posteriori procedure (MAP), see https://en.wikipedia.org/wiki/Maximum_a_posteriori_estimation The procedure is applied in a transformed parameter space in which the a-priori distribution of all parameters is Gaussian.

No uncertainty estimation is included. All generated samples produce exactly the same result (the found maximum).

Parameters:
  • graph (halerium.core.Graph) – The graph of the model.

  • data (halerium.core.DataLinker, dict, optional) – The data linker or dict containing data constraining the model. The default is None.

  • compiler (optional) – The compiler instance or class for compiling the model. The default is None, in which case a Tensorflow compiler is used.

  • initial_values (dict, optional) – A dictionary containing (static) variables in the graph as keys and their initial values as values. The model then tries to convert these into appropriate initial values for the solver.

  • initial_source_values (dict, optional) – A dictionary containing (static) variables in the graph as keys and initial value for their source as values. In addition, strings are accepted as keys, but are ignored unless the model can interpret these. This attribute may be used to pass initial values for trainable model parameters obtained from training of earlier models with the same (or a sufficiently similar) graph. Other usage is not recommended (unless users know exactly how the model handles its sources and their initial values).

  • random_initial_value_scale (float, None, optional) – The scale of any randomly drawn initial values (relative to the scale of the respective variables as given by the graph). The default is 0.01.

  • copy_graph (bool, optional) – Whether the model 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. Such changes to a graph a model holds directly (i.e. not a copy) makes that model inconsistent and likely causes errors.

  • model_graph_options (dict, optional) – The options for creating the model graph. The default is None.

apply_to_samples(fetches, function, n_samples)#

Draw samples and apply a function to them.

Parameters:
  • fetches – The variables to generate sample data for.

  • function (callable) – The function to apply to the sample data.

  • n_samples (int) – The number of samples to draw from the model.

Returns:

The result of applying the function to the sampled data.

Return type:

result

assert_is_trained()#

Check if model is trained.

Return type:

None.

Raises:

RuntimeWarning – If model is not trained.

get_example(fetches)#

Draw an example from the model.

Parameters:

fetches – The variables to generate example values for.

Returns:

The example data.

Return type:

example

get_means(fetches, n_samples=1)#

Estimate mean values.

Parameters:
  • fetches – The variables to estimate mean values for.

  • n_samples (int) – The number of samples to estimate the means from. Here for compatibility only. Its value is ignored.

Returns:

The estimated means of the variables.

Return type:

means

get_posterior_graph(name=None, n_samples=1)#

Create posterior graph from trained model.

Parameters:
  • name (str) – The name to give to the posterior graph.

  • n_samples (int) – The number of samples to estimate the posterior distributions from. Here for compatibility only. Its value is ignored.

Returns:

post_graph – The posterior graph.

Return type:

halerium.core.Graph

get_samples(fetches, n_samples=1)#

Draw samples from the model.

Parameters:
  • fetches – The variables to generate sample data for.

  • n_samples (int) – The number of examples to draw from the model.

Returns:

The sampled data.

Return type:

samples

get_source_values(return_all=False)#

Get source values.

Prior to any training, these are just any initial values provided by the caller to the model upon construction. After training, these are the final values of the sources at the end of the training.

Parameters:

return_all (bool) – Whether to return all, or just the source values for static variables (default).

Returns:

The source values.

Return type:

source_values

get_standard_deviations(fetches, n_samples=1)#

Estimate standard deviations.

Parameters:
  • fetches – The variables to estimate standard deviations for.

  • n_samples (int) – The number of samples to estimate the standard deviations from. Here for compatibility only. Its value is ignored.

Returns:

The estimated standard deviations of the variables.

Return type:

standard_deviations

get_variances(fetches, n_samples=1)#

Estimate variances.

Parameters:
  • fetches – The variables to estimate variances for.

  • n_samples (int) – The number of samples to estimate the variances from. Here for compatibility only. Its value is ignored.

Returns:

The estimated variances of the variables.

Return type:

variances

property is_trained#

Whether the model has been trained.

property model_graph#

The model graph (don’t modify it yourself).

solve(**kwargs)#

Solve the model.

Parameters:

kwargs – Any keyword arguments to pass to the minimizer.

Return type:

None.