mrftools.MatrixBeliefPropagator module¶
BeliefPropagator class.
-
class
mrftools.MatrixBeliefPropagator.MatrixBeliefPropagator(markov_net)[source]¶ Bases:
mrftools.Inference.InferenceObject that can run belief propagation on a MarkovNet. Uses sparse matrices to encode the indexing underlying belief propagation.
-
augment_loss(var, state)[source]¶ Adds a loss penalty to the MRF energy function. Used to create loss-augmented inference for max-margin learning
Parameters: - var (object) – variable to add loss to
- state (int) – state of variable in ground truth labels
Returns: None
-
compute_beliefs()[source]¶ Compute unary log beliefs based on current messages and store them in belief_mat
Returns: None
-
compute_bethe_entropy()[source]¶ Compute Bethe entropy from current beliefs. This method assumes that the beliefs have been computed and are fresh.
Returns: computed Bethe entropy
-
compute_dual_objective()[source]¶ Compute the value of the BP Lagrangian.
Returns: Lagrangian objective function
-
compute_energy()[source]¶ Compute the log-linear energy. Assume that the beliefs have been computed and are fresh.
Returns: computed energy
-
compute_energy_functional()[source]¶ Compute the energy functional, which is the variational approximation of the log-partition function.
Returns: computed energy functional
-
compute_inconsistency()[source]¶ Return the total disagreement between each unary belief and its pairwise beliefs. When message passing converges, the inconsistency should be within numerical error.
Returns: the total absolute disagreement between each unary belief and its pairwise beliefs
-
compute_pairwise_beliefs()[source]¶ Compute pairwise log beliefs based on current messages, and stores them in pair_belief_tensor
Returns: None
-
condition(var, state)[source]¶ Condition a variable, usually because of statistical evidence, to be in a subset of its possible states
Parameters: - var (object) – variable to condition
- state (int or array) – state to condition to or array of states that the variable may be in
Returns: None
-
disallow_impossible_states()[source]¶ Force variables to only allow nonzero probability on their possible states.
Returns: None
-
get_feature_expectations()[source]¶ Computes the feature expectations under the currently estimated marginal probabilities. Only works when the model is a LogLinearModel class with features for edges.
Returns: vector of the marginals in order of the flattened unary features first, then the flattened pairwise features
-
infer(tolerance=1e-08, display='iter')[source]¶ Run belief propagation until messages change less than tolerance.
Parameters: - tolerance – the minimum amount that the messages can change while message passing can be considered not converged
- display –
string parameter indicating how much to display. Options are ‘full’ and ‘iter’ ‘full’ prints the energy functional and dual objective each iteration,
which requires extra computation‘iter’ prints just the change in messages each iteration
Returns: None
-
initialize_messages()[source]¶ Initialize messages to default initialization (set to zeros).
Returns: None
-
load_beliefs()[source]¶ Update the belief dictionaries var_beliefs and pair_beliefs using the current messages.
Returns: None
-
set_max_iter(max_iter)[source]¶ Set the maximum iterations of belief propagation to run before early stopping :param max_iter: integer maximum iterations :return: None
-
-
mrftools.MatrixBeliefPropagator.logsumexp(matrix, dim=None)[source]¶ Compute log(sum(exp(matrix), dim)) in a numerically stable way.
Parameters: - matrix (ndarray) – input ndarray
- dim (int) – integer indicating which dimension to sum along
Returns: numerically stable equivalent of np.log(np.sum(np.exp(matrix), dim)))
Return type: ndarray
-
mrftools.MatrixBeliefPropagator.sparse_dot(full_matrix, sparse_matrix)[source]¶ Convenience function to compute the dot product of a full matrix and a sparse matrix. Useful to avoid writing code with a lot of transposes.
Parameters: - full_matrix (ndarray) – dense matrix
- sparse_matrix (csc_matrix) – sparse matrix
Returns: full_matrix.dot(sparse_matrix)
Return type: ndarray