mxfusion.inference.inference

Members

class mxfusion.inference.inference.Inference(inference_algorithm, constants=None, hybridize=False, dtype=None, context=None)

Bases: abc.ABC

Abstract class defining an inference method that can be applied to a model. An inference method consists of a few components: the applied inference algorithm, the model definition (optionally a definition of posterior approximation), the inference parameters.

Parameters:
  • inference_algorithm (InferenceAlgorithm) – The applied inference algorithm
  • graphs ([FactorGraph]) – a list of graph definitions required by the inference method. It includes the model definition and necessary posterior approximation.
  • observed ([Variable]) – A list of observed variables
  • constants ({Variable: mxnet.ndarray}) – Specify a list of model variables as constants
  • hybridize (boolean) – Whether to hybridize the MXNet Gluon block of the inference method.
  • dtype ({numpy.float64, numpy.float32, 'float64', 'float32'}) – data type for internal numberical representation
  • context ({mxnet.cpu or mxnet.gpu}) – The MXNet context
observed_variables
observed_variable_UUIDs
observed_variable_names
graphs

Returns the list of graphs the inference algorithm operates over.

inference_algorithm

Return the reference to the used inference algorithm.

create_executor()

Return a MXNet Gluon block responsible for the execution of the inference method.

initialize(**kw)

Initialize the inference method with the shapes of observed variables. The inputs of the keyword arguments are the names of the variables in the model defintion. The values of the keyword arguments are the data of the corresponding variables (mxnet.ndarray) or their shape (tuples).

run(**kw)

Run the inference method.

Parameters:**kwargs – The keyword arguments specify the data for inferenceself. The key of each argument is the name of the corresponding variable in model definition and the value of the argument is the data in numpy array format.
Returns:the samples of target variables (if not spcified, the samples of all the latent variables)
Return type:{UUID: samples}
set_intializer()

Configure the inference method on how to initialize variables and parameters.

load(primary_model_file=None, secondary_graph_files=None, inference_configuration_file=None, parameters_file=None, mxnet_constants_file=None, variable_constants_file=None)

Loads back everything needed to rerun an inference algorithm. The major pieces of this are the InferenceParameters, FactorGraphs, and InferenceConfiguration.

Parameters:
  • primary_model_file (str of filename) – The file containing the primary model to load back for this inference algorithm.
  • secondary_graph_files ([str of filename]) – The files containing any secondary graphs (e.g. a posterior) to load back for this inference algorithm.
  • inference_configuration_file (str of filename) – The file containing any inference specific configuration needed to reload this inference algorithm. e.g. observation patterns used to train it.
  • parameters_file – These are the parameters of the previous inference algorithm. These are in a {uuid: mx.nd.array} mapping.
  • mxnet_constants_file (file saved down with mx.nd.save(), so a {uuid: mx.nd.array} mapping saved in a binary format.) – These are the constants in mxnet format from the previous inference algorithm. These are in a {uuid: mx.nd.array} mapping.
  • variable_constants_file (json dict of {uuid: constant_primitive}) – These are the constants in primitive format from the previous inference algorithm.
load_configuration(config_file, uuid_map)

Loads relevant inference configuration back from a file. Currently only loads the observed variables UUIDs back in, using the uuid_map parameter to store the correct current observed variables.

Parameters:
  • config_file (str) – The file to save the configuration down into.
  • uuid_map ({ current_model_uuid : loaded_previous_uuid}) – A map of previous/loaded model component uuids to their current variable in the loaded graph.
save_configuration(config_file)

Saves relevant inference configuration down into a file. Currently only saves the observed variables UUIDs as {‘observed’: [observed_uuids]}.

Parameters:config_file (str) – The file to save the configuration down into.
save(prefix=None)

Saves down everything needed to reload an inference algorithm. The two primary pieces of this are the InferenceParameters and FactorGraphs.

Parameters:prefix (str , ex. "../saved_inferences/experiment_1") – The directory and any appending tag for the files to save this Inference as.
class mxfusion.inference.inference.TransferInference(inference_algorithm, infr_params, var_tie=None, constants=None, hybridize=False, dtype=None, context=None)

Bases: mxfusion.inference.inference.Inference

The abstract Inference method for transfering the outcome of one inference method to another.

Parameters:
  • inference_algorithm (InferenceAlgorithm) – The applied inference algorithm
  • graphs ([FactorGraph]) – a list of graph definitions required by the inference method. It includes the model definition and necessary posterior approximation.
  • observed ([Variable]) – A list of observed variables
  • constants ({Variable: mxnet.ndarray}) – Specify a list of model variables as constants
  • hybridize (boolean) – Whether to hybridize the MXNet Gluon block of the inference method.
  • dtype ({numpy.float64, numpy.float32, 'float64', 'float32'}) – data type for internal numberical representation
  • context ({mxnet.cpu or mxnet.gpu}) – The MXNet context
generate_executor(**kw)