mxfusion.inference.inference_alg

Members

class mxfusion.inference.inference_alg.ObjectiveBlock(infr_method, constants, data_def, var_trans, var_ties, excluded, prefix='', params=None)

Bases: mxnet.gluon.block.HybridBlock

Wrapping the MXNet codes with a MXNet Gluon HybridBlock for execution.

Parameters:
  • infr_method (a pointer to a function) – the pointer to a function that contains the actual MXNet codes.
  • constants ({Variable UUID: int or float or mxnet.ndarray}) – the variables with constant values
  • data_def – a list of variable UUID, which corresponds to the order of variables expected as the positional

arguments in “hybrid_forward”. :type data_def: [UUID] :param var_trans: the transformations applied variables :type var_trans: {UUID: VariableTransformation} :param var_ties: A dictionary of variables that are tied and use the MXNet Parameter of the dict value uuid. :type var_ties: { UUID of source variable: UUID of target variable} :param excluded: a set of variables excluded from being set as Block parameters. :type excluded: set(UUID) or [UUID] :param prefix: the prefix of this Gluon block :type prefix: str :param params: the ParameterDict of this Gluon block :type params: mxnet.gluon.ParameterDict

hybrid_forward(F, x, *args, **kw)

This function does all the pre-processes and post-processes for the execution of a InferenceAlgorithm.

Parameters:
  • F (mxnet.symbol or mxnet.ndarray) – the MXNet computation mode
  • x (MXNet NDArray or MXNet Symbol) – a dummy variable to enable the execution of this Gluon block
  • *arg

    all the positional arguments, which correspond to the data provided to the InferenceAlgorithm.

  • **kw

    all the keyword arguments, which correspond to the parameters that may require gradients.

Returns:

the outcome of the InferenceAlgorithm that are determined by the inference algorithm.

Rtypes:

{str: MXNet NDArray or MXNet Symbol}

class mxfusion.inference.inference_alg.InferenceAlgorithm(model, observed, extra_graphs=None)

Bases: abc.ABC

The abstract class for an inference algorithm. A concrete inference algorithm will inherit this class and overload the “compute” function with the actual computation logic.

replicate_self(model, extra_graphs=None)
observed_variables

The observed variables in this inference algorithm.

observed_variable_UUIDs

The UUIDs of the observed variables in this inference algorithm.

observed_variable_names

The names (if exist) of the observed variables in this inference algorithm.

model

the model that the inference algorithm applies to

graphs

all the factor graphs that the inference algorithm uses

prepare_executor(rv_scaling=None)

Prepare the creation of an executor. This includes collecting the list of variable transformations and the list of the variables that are inherited from external Gluon blocks, and setting log_pdf_scaling for random variables.

Parameters:rv_scaling – The scaling of log_pdf of the random variables that are set by users for data sub-sampling

or mini-batch learning. :type rv_scaling: {UUID: float} :returns: the list of the variable transformations and the list of the variables that are excluded from being set as Gluon block parameters (see the excluded argument of __init__ of ObjectiveBlock). :rtypes: {str(UUID): Transformation}, set(str(UUID))

create_executor(data_def, params, var_ties, rv_scaling=None)

Create a MXNet Gluon block to carry out the computation.

Parameters:
  • data_def ([UUID of Variable (str)]) – a list of unique ID of data variables. The order of variables in the list corresponds to the order of variable in the positional arguments when calling the Gluon Block.
  • params (InferenceParameters) – The parameters of model involved in inference.
  • var_ties ({ UUID of source variable: UUID of target variable}) – A dictionary of variables that are tied and use the MXNet Parameter of the dict value uuid.
  • rv_scaling – The scaling of log_pdf of the random variables that are set by users for data sub-sampling

or mini-batch learning. :type rv_scaling: {UUID: float} :returns: the Gluon block computing the outcome of inference :rtype: mxnet.gluon.HybridBlock

compute(F, variables)

The abstract method for the computation of the inference algorithm

Parameters:
  • F (Python module) – the execution context (mxnet.ndarray or mxnet.symbol)
  • variables – the set of MXNet arrays that holds the values of

variables at runtime. :type variables: {str(UUID): MXNet NDArray or MXNet Symbol} :returns: the outcome of the inference algorithm :rtype: mxnet.ndarray.ndarray.NDArray or mxnet.symbol.symbol.Symbol

set_parameter(variables, target_variable, target_value)

Set the value of a variable as the artifacts of this inference algorithm. This triggers to set the value to the corresponding variable into InferenceParameters at the end of inference.

Parameters:variables – the set of MXNet arrays that holds the values of

all the variables at runtime. :type variables: {str(UUID): MXNet NDArray or MXNet Symbol} :param target_variable: the variable that a value is set to :type target_variable: Variable :param target_value: the value to be set :type target_value: MXNet NDArray or float

class mxfusion.inference.inference_alg.SamplingAlgorithm(model, observed, num_samples=1, target_variables=None, extra_graphs=None)

Bases: mxfusion.inference.inference_alg.InferenceAlgorithm

The base class of sampling algorithms.

Parameters:
  • model (Model) – the definition of the probabilistic model
  • observed ([Variable]) – A list of observed variables
  • num_samples (int) – the number of samples used in estimating the variational lower bound
  • target_variables ([UUID]) – (optional) the target variables to sample
  • extra_graphs ([FactorGraph]) – a list of extra FactorGraph used in the inference algorithm.
compute(F, variables)

The abstract method for the computation of the inference algorithm.

If inference algorithm is used for gradient based optimizations, it should return two values. The first for the loss function, the second the gradient of the loss function.

Parameters:
  • F (Python module) – the execution context (mxnet.ndarray or mxnet.symbol)
  • variables – the set of MXNet arrays that holds the values of

variables at runtime. :type variables: {str(UUID): MXNet NDArray or MXNet Symbol} :returns: the outcome of the inference algorithm :rtype: mxnet.ndarray.ndarray.NDArray or mxnet.symbol.symbol.Symbol. If gradient based, will return two values. The first the loss function, the second the gradient of the loss function.