mxfusion.modules.module

Members

class mxfusion.modules.module.Module(inputs, outputs, input_names, output_names, rand_gen=None, dtype=None, ctx=None)

Bases: mxfusion.components.factor.Factor

The base class for a probabilistic module.

A probabilistic module is a combination of model dentition and Inference algorithms. It acts as a factor and are defined as such during model definition, producing random variables like a plain probabilistic distribution. It differs from a plain distribution in that to compute it’s log_pdf and draw_samples functions, it uses a full Inference method.

Parameters:
  • inputs (List of tuples of name to node e.g. [('random_variable': Variable y)] or None) – the input variables
  • outputs (List of tuples of name to node e.g. [('random_variable': Variable y)] or None) – the output variables
  • input_names ([str]) – the names of all the input variables
  • output_names ([str]) – the names of all the output variables
  • rand_gen (RandomGenerator) – the random generator (default: MXNetRandomGenerator).
  • dtype (numpy.float32 or numpy.float64) – the data type for float point numbers.
  • ctx (None or mxnet.cpu or mxnet.gpu) – the mxnet context (default: None/current context).
set_outputs(variables)

This method overrides the set_outputs method of Factor. It triggers the initialization produces of a probabilistic module including building the factor graphs and attaching default inference algorithms.

Parameters:variables (Variable or (Variable,)) – The list of variables to be set as the outputs of the module
expose_hidden_parameters_as_input(name, variable)

Expose a hidden parameter of the module as an input variable.

Parameters:
  • name (str) – the name of the resulting input variable
  • variable (Variable) – the reference to the internal variable that will be exposed.
hidden_parameters

The UUIDs of all the hidden parameters.

initialize_hidden_parameters(param_dict=None, excluded=None, constants=None)

Initialize all the hidden parameters.

Parameters:
  • param_dict (MXNet ParameterDict) – the MXNet ParameterDict for parameter initialization
  • excluded (set(str(UUID))) – the set of variables that are excluded from initialization
  • constants ({str(UUID): float or int}) – the constants discovered during initialization, to be used for shape inference
get_names_from_uuid(uuids)

Get the names of a set of input or output variables given their UUIDs.

Parameters:uuids ([str(UUID)]) – the list of UUIDs
attach_log_pdf_algorithms(targets, conditionals, algorithm, alg_name=None)

Attach an inference algorithm for computing the log_pdf of the module.

Parameters:
  • targets (tuple of str) – Variables to compute the log probability of.
  • conditionals (tuple of str) – variables to condition the probabilities on.
  • algorithm – the inference algorithm to compute log probability of

the module. :type algorithm: InferenceAlgorithm :param alg_name: The name of the algorithm :type alg_name: str

attach_draw_samples_algorithms(targets, conditionals, algorithm, alg_name=None)

Attach an inference algorithm for drawing samples from the module.

Parameters:
  • targets (tuple of str) – a list of names of arguments to draw samples from.
  • conditionals (tuple of str) – Variables to condition the samples on.
  • algorithm (InferenceAlgorithm) – the inference algorithm to draw samples of the chosen target variables from the module.
  • alg_name (str) – The name of the algorithm
attach_prediction_algorithms(targets, conditionals, algorithm, alg_name=None)

Attach an inference algorithm for prediction from the module.

Parameters:
  • targets (tuple of str) – a list of names of arguments to predict.
  • conditionals (tuple of str) – Variables to condition the prediction on.
  • algorithm (InferenceAlgorithm) – the inference algorithm to predict the chosen target variables from the module.
  • alg_name (str) – The name of the algorithm
log_pdf(F, variables, targets=None)

Compute the logarithm of the probability/probability density of a set of random variables in the Module. The set of random variables are specified in the “target” argument and any necessary conditional variables are specified in the “conditionals” argument. Any relevant constants are specified in the “constants” argument.

Parameters:
  • F – the MXNet computation mode (mxnet.symbol or mxnet.ndarray).
  • variables ({UUID : MXNet NDArray or MXNet Symbol}) – The set of variables
  • targets ({uuid : mxnet NDArray or mxnet Symbol}) – Variables to compute the log probability of.
Returns:

the sum of the log probability of all the target variables.

Return type:

mxnet NDArray or mxnet Symbol

draw_samples(F, variables, num_samples=1, targets=None)

Draw samples from the target variables of the Module. If the targets argument is None, draw samples from all the variables that are not in the conditional variables. If the targets argument is given, this method returns a list of samples of variables in the order of the target argument, otherwise it returns a dict of samples where the keys are the UUIDs of variables and the values are the samples.

Parameters:
  • F – the MXNet computation mode (mxnet.symbol or mxnet.ndarray).
  • variables ({UUID : MXNet NDArray or MXNet Symbol}) – The set of variables
  • num_samples (int) – The number of samples to draw for the target variables.
  • targets ([UUID]) – a list of Variables to draw samples from.
Returns:

the samples of the target variables.

Return type:

(MXNet NDArray or MXNet Symbol,) or {str(UUID): MXNet NDArray or MXNet Symbol}

predict(F, variables, num_samples=1, targets=None)

Predict some variables.

Parameters:
  • F – the MXNet computation mode (mxnet.symbol or mxnet.ndarray).
  • variables ({UUID : MXNet NDArray or MXNet Symbol}) – The set of variables
  • num_samples – The number of samples to draw for the target variables if sampling is used for prediction.

(optional) :type num_samples: int :param targets: a list of Variables to predict. :type targets: [UUID] :returns: the sum of the log probability of all the target variables. :rtype: mxnet NDArray or mxnet Symbol

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

reconcile_with_module(previous_module)
replicate_self(attribute_map=None)

The copy constructor for the function.

load_module(module_json)
as_json()