mxfusion.inference.inference

Members

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

Bases: object

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), and the inference parameters.

Parameters:
  • inference_algorithm (InferenceAlgorithm) – The applied inference algorithm
  • 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 numerical 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 definition. The values of the keyword arguments are the data of the corresponding variables (mxnet.ndarray) or their shape (tuples).

run(**kwargs)

Run the inference method.

Parameters:**kwargs

The keyword arguments specify the data for inference self. 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 specified, the samples of all the latent variables)
Return type:{UUID: samples}
set_initializer()

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

load(graphs_file=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:
  • graphs_file (str of filename) – The file containing the graphs to load back for this inference algorithm. The first of these is the primary graph.
  • inference_configuration_file – The file containing any inference specific configuration needed to
reload this inference algorithm.
e.g. observation patterns used to train it.
Parameters:parameters_file – These are the parameters of the previous inference algorithm.

These are in a {uuid: mx.nd.array} mapping. :type mxnet_constants_file: file saved down with mx.nd.save(), so a {uuid: mx.nd.array} mapping saved in a binary format. :param mxnet_constants_file: These are the constants in mxnet format from the previous inference algorithm. These are in a {uuid: mx.nd.array} mapping. :type mxnet_constants_file: file saved down with mx.nd.save(), so a {uuid: mx.nd.array} mapping saved in a binary format. :param variable_constants_file: These are the constants in primitive format from the previous inference algorithm. :type variable_constants_file: json dict of {uuid: constant_primitive}

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 transferring the outcome of one inference method to another.

Parameters:
  • inference_algorithm (InferenceAlgorithm) – The applied inference algorithm
  • 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 numerical representation
  • context ({mxnet.cpu or mxnet.gpu}) – The MXNet context
generate_executor(**kw)