mrftools.BeliefPropagator module

BeliefPropagator class.

class mrftools.BeliefPropagator.BeliefPropagator(markov_net)[source]

Bases: mrftools.Inference.Inference

Object that can run belief propagation on a MarkovNet. Uses native python loops to compute message passing, so it can be slow. This class is mostly useful for reference, since the loop-based implementations are similar to how belief propagation is typically written in math.

compute_beliefs()[source]

Compute unary log beliefs based on current messages and store them in var_beliefs dict

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_message(var, neighbor)[source]

Compute the message from var to factor.

Parameters:
  • var – variable sending the message
  • neighbor – neighbor receiving the message
Returns:

message vector from var to neighbor

compute_pairwise_beliefs()[source]

Compute pairwise log beliefs based on current messages, and stores them in pair_beliefs dict

Returns:None
get_feature_expectations()[source]

Computes the feature expectations under the currently estimated marginal probabilities.

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

init_beliefs()[source]

Initialize beliefs to be the normalized potential functions. These beliefs will not be consistent until the are updated with message passing. :return: None

init_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

update_messages()[source]

Update all messages between variables using belief division.

Returns:the float change in messages from previous iteration.
mrftools.BeliefPropagator.logsumexp(matrix, dim=None)[source]

Compute log(sum(exp(matrix), dim)) in a numerically stable way.