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
print_params()

Returns a string with the inference parameters nicely formatted for display, showing which model they came from and their name + uuid.

Format: > infr.print_params() Variable(1ab23)(name=y) - (Model/Posterior(123ge2)) - (first mxnet values/shape)

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) :rtype: {UUID: samples}

set_initializer()

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

load(zip_filename='inference.zip')

Loads back everything needed to rerun an inference algorithm from a zip file. See the save function for details on the structure of the zip file.

Parameters:zip_filename (str of zip filename) – Path to the zip file of the inference method to load back in. Defaults to the default name of inference.zip
load_configuration(configuration, 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:
  • configuration (dict) – The loaded configuration dictionary
  • uuid_map ({ current_model_uuid : loaded_previous_uuid}) – A map of previous/loaded model component uuids to their current variable in the loaded graph.
get_serializable()

Returns the minimum set of properties that the object needs to save in order to be serialized down and loaded back in properly. :returns: A dictionary of configuration properties needed to serialize and reload this inference method. :rtype: Dictionary that is JSON serializable.

save(zip_filename='inference.zip')

Saves down everything needed to reload an inference algorithm. This method writes everything into a single zip archive, with 6 internal files. 1. version.json - This has the version of serialization used to create the zip file. 2. graphs.json - This is a networkx representation of all FactorGraphs used during Inference.

See mxfusion.models.FactorGraph.save for more information.
  1. mxnet_parameters.npz - This is a numpy zip file saved using numpy.savez(), containing one file for each mxnet parameter in the InferenceParameters object. Each parameter is saved in a binary file named by the parameter’s UUID.
  2. mxnet_constants.npz - The same as mxnet_parameters, except only for constant mxnet parameters.
  3. variable_constants.json - Parameters file of primitive data type constants, such as ints or floats. I.E. { UUID : int/float}

6. configuration.json - This has other configuration related to inference such as the observation pattern. :param zip_filename: Path to and name of the zip archive to save the inference method as. :type zip_filename: str

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)