The ForwardPlaceholderModel class#

Aliases#

halerium.core.model.ForwardPlaceholderModel
class ForwardPlaceholderModel(graph, input_variables=None, data=None, enable_probabilities=False, compiler=None, copy_graph=True, model_graph_options=None)#

Forward placeholder model class.

Forward placeholder model class.

The model uses direct sampling to generate samples along the causal directions of the graph. Information therefore only travels forward along the causal connections as defined by the Variables dependencies.

Compared to the ForwardModel it allows for the efficient replacement of input data and offers additional possibilities to evaluate sampled probabilities.

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

  • input_variables (list, tuple, dict, optional) – The input variables for which values can be provided after initialization. The default is None.

  • data (halerium.core.DataLinker, dict, None, 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.

  • enable_probabilities (bool, str, optional) –

    Whether to compute probabilities. Accepted values are: - False: No probabilities are computed. - ‘itemized’: Probabilities are computed for each example separately. - ‘joint’: Joint probabilities are computed for the whole sample. - ‘upsampled’: Same as ‘itemized’, but creates enables the additional

    method get_upsampled_log_probability_samples_and_weights in which missing values are not marginalized over.

    • True: The same as ‘joint’.

    The default is False.

  • 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, input_values=None)#

Draw samples and apply a function to them.

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

  • function – The function to apply to the sample data.

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

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

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.

property default_input_values#

Default input values.

Default input values that can be used for sampling functions. With the defaults set, the get_… functions can be used in the same way as for the other model classes. The keys of this dict have to be variables compatible to the input_variables specified in the initialization of the model. The values are numpy arrays.

property enable_probabilities#

Whether computing probabilities is enabled.

get_example(fetches, input_values=None)#

Draw an example from the model.

Parameters:
  • fetches – The variables to generate example values for.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

Returns:

The example data.

Return type:

example

get_log_probabilities(fetches, n_samples=1000, input_values=None)#

Get log. probabilies.

Calculate the log probabilities of fetches for the given input values. The probability will be a container with the same structure as fetches. The container elements will be vectors of length n_data, where n_data comes from the DataLinker used to create the model (the default is 1).

Parameters:
  • fetches (Variable, dict, list or tuple) – The variables for which to estimate the log probabilities.

  • n_samples (int, optional) – How many samples are used to calculate the probability. The default is 1000.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

Returns:

log_probabilities – the estimated log-probabilities.

Return type:

array, dict, list or tuple

get_log_probability_samples(fetches, n_samples=1000, input_values=None)#

Get samples for log. probabilities.

Sample the log probabilities of fetches for the given input values. The result will be a container with the same structure as fetches. The container elements will be vectors of shape (n_samples, n_data), where n_data comes from the DataLinker used to create the model (the default is 1).

Parameters:
  • fetches (Variable, dict, list or tuple) – The variables for which to sample the log probabilities.

  • n_samples (int, optional) – How many samples are generated. The default is 1000.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

Returns:

The samples of the log-probabilities.

Return type:

samples

get_means(fetches, n_samples=100, input_values=None)#

Estimate mean values.

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

  • n_samples (int) – The number of samples to estimate the means from.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

Returns:

The estimated means of the variables.

Return type:

means

get_posterior_graph(name=None, n_samples=1000, input_values=None)#

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.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

Returns:

post_graph – The posterior graph.

Return type:

halerium.Graph

get_samples(fetches, n_samples=1, input_values=None)#

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.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

Returns:

The sampled data.

Return type:

samples

get_standard_deviations(fetches, n_samples=100, input_values=None)#

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.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

Returns:

The estimated standard deviations of the variables.

Return type:

standard_deviations

get_total_log_probability(n_samples=1000, return_accuracy=False, input_values=None)#

Get total log. probabilities.

Calculate the total log probability for the given input values. The probability will be a vector of length n_data, where n_data is comes from the DataLinker used to create the model (the default is 1).

Parameters:
  • n_samples (int, optional) – How many samples are used to calculate the probability. The default is 1000.

  • return_accuracy (bool, optional) – Whether to estimate and return the accuracy of the calculated probability.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

get_upsampled_log_probability_samples_and_weights(fetches, n_samples=1000, input_values=None)#

Get samples for upsampled log. probabilities.

The upsampled probabilities are the expected probabilities conditional to the given data. For example, if we have x and y and are only given y then the resulting probabilities for x and y will be averaged over possible values of x (conditional to the given value of y).

Sample the log probabilities of fetches for the given input values. The first output will be a container with the same structure as fetches. The container elements will be vectors of shape (n_samples, n_data), where n_data comes from the DataLinker used to create the model (the default is 1). The second output are the log-weights with which these samples would have to be averaged. This is an array of shape (n_samples, n_data).

Parameters:
  • fetches (Variable, dict, list or tuple) – The variables for which to sample the log probabilities.

  • n_samples (int, optional) – How many samples are generated. The default is 1000.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

Returns:

  • samples (array, dict, list or tuple) – The samples of the log-probabilities.

  • log_weights (array) – The log-weights of the samples.

get_variances(fetches, n_samples=100, input_values=None)#

Estimate variances.

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

  • n_samples (int) – The number of samples to estimate the variances from.

  • input_values (dict, optional) – The input values for the model. If None, previously set input values are reused. Otherwise, the keys of this dict have to be variables compatible with the input_variables specified in the initialization of the model. The values are numpy arrays. Any input_variables without input_values or default_input_values raise an Exception. The default is None.

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

set_default_input_values(default_input_values)#

Sets default input values.

Parameters:

default_input_values – The values to set default model input values to.

solve()#

Solve the model.

The forward placeholder model does not require any solving.

Return type:

None.