mrftools.Learner module

Main learner class for log-linear model parameter learning.

class mrftools.Learner.Learner(inference_type=<class 'mrftools.MatrixBeliefPropagator.MatrixBeliefPropagator'>)[source]

Bases: object

Learner class for log-lienar model parameter learning. This class contains methods for calculating various objective functions and gradients, and implements a subgradient optimization for the variational likelihood.

add_data(labels, model)[source]
Add data example to training set. The states variable should be a dictionary containing all the states of the
unary variables.
Parameters:
  • labels – dict containing true states of all labeled variables
  • model – LogLinearModel object containing features for each pairwise and unary potential
Returns:

calculate_expectations(weights, belief_propagators, should_infer=True)[source]

Calculate the feature expectations given the provided model weights.

Parameters:
  • weights – weight vector containing weights for all potentials
  • belief_propagators – iterable of belief propagators whose models should be updated with the weights
  • should_infer – Boolean value of whether to run inference. This value should usually only be False when inference has already been run for this particular weight vector, i.e., if this function is being called immediately after it has been called with the same weights.
Returns:

feature expectation vector

do_inference(belief_propagators)[source]

Perform inference on all stored models.

Parameters:belief_propagators – iterable of inference objects
Returns:None
dual_obj(weights, options=None)[source]

Return the dual regularized negative variational log likelihood including Lagrangian penalty terms for local inconsistencies of estimated marginals (i.e., beliefs) :param weights: weight vector containing weights for all potentials :param options: Unused (for now) options for objective function :return: dual objective value

get_bethe_entropy(belief_propagators)[source]

Compute the average Bethe entropy of all inference objects :param belief_propagators: iterable of inference objects :return: average Bethe entropy of all objectives

get_feature_expectations(belief_propagators)[source]

Run inference and return the marginal in vector form using the order of self.potentials.

Parameters:belief_propagators – iterable of inference objects to use to get feature expectations
Returns:vector of feature expectations
gradient(weights, options=None)[source]

Return the gradient of the regularized negative variational log likelihood :param weights: weight vector containing weights for all potentials :param options: Unused (for now) options for objective function :return: gradient vector

learn(weights, optimizer=<function ada_grad>, callback=None, opt_args=None)[source]

Fit model parameters my maximizing the variational likelihood :param weights: Initial weight vector. Can be used to warm start from a previous solution. :param optimizer: gradient-based optimization function, as defined in opt.py :param callback: callback function run during each iteration of the optimizer. The function receives the

weights as input. Can be useful for diagnostics, live plotting, storing records, etc.
Parameters:opt_args – optimization arguments. Usually a dictionary of parameter values
Returns:learned weights
objective(weights, options=None)[source]

Return the primal regularized negative variational log likelihood :param weights: weight vector containing weights for all potentials :param options: Unused (for now) options for objective function :return: objective value

set_inference_truncation(bp_iter)[source]

Set maximum number of iterations for inference. Useful for faster learning or inner-dual learning to stop inference before they run to convergence. :param bp_iter: maximum iterations each belief propagator can run for each gradient/objective computation :return: NOne

set_regularization(l1, l2)[source]

Set the regularization parameters.

Parameters:
  • l1 – l1 regularization parameter
  • l2 – l2 regularization parameter
Returns:

set_weights(weight_vector, belief_propagators)[source]

Set weights of Markov net from vector using the order in self.potentials. :param weight_vector: weight vector containing weights for all potentials :param belief_propagators: iterable of belief propagators whose models should be updated with the weights :return: None

subgrad_grad(weights, options=None, do_inference=False)[source]

Compute the gradient of the variational negative log likelihood.

Parameters:
  • weights – Weight vector containing the same number of entries as all weights for this model
  • do_inference – Boolean value indicating whether or not to run inference. Defaults to False because typically the objective function was called immediately before, which does inference.
Returns:

gradient with respect to weights

subgrad_obj(weights, options=None, do_inference=True)[source]

Compute the variational negative log likelihood. Performs inference on latent variables in the labeled inference objects before calling the EM objective

Parameters:
  • weights – Weight vector containing the same number of entries as all weights for this model
  • do_inference – Boolean value indicating whether or not to run inference. Defaults to True.
Returns:

objective value (float)