mrftools.MarkovNet module

Markov network class for storing potential functions and structure.

class mrftools.MarkovNet.MarkovNet[source]

Bases: object

Object containing the definition of a pairwise Markov net.

create_matrices()[source]

Create matrix representations of the MRF structure and potentials to allow inference to be done via matrix operations :return: None

declare_variable(variable, num_states)[source]

Indicate the existence of a variable

Parameters:
  • variable – name of the variable (can be any hashable object)
  • num_states – integer number of states the variable can take
Returns:

None

evaluate_state(states)[source]

Evaluate the energy of a state. states should be a dictionary of variable: state (int) pairs.

Parameters:states – dictionary of variable states with a key-value pair for each variable
Returns:MRF energy value for the state as a float
get_neighbors(variable)[source]

Return the neighbors of variable.

Parameters:variable – name of variable
Returns:set of neighboring variables connected in MRF
get_potential(key)[source]

Return the potential between pair[0] and pair[1]. If (pair[1], pair[0]) is in our dictionary instead, return the transposed potential.

Parameters:key – name of the key whose potential to get. Can either be a variable name or a pair of variables (edge)

:return potential table for the key

set_edge_factor(edge, potential)[source]

Set a factor by inputting the involved variables then the potential function. The potential function should be a np matrix.

Parameters:
  • edge – 2-tuple of the variables in the edge. Can be in any order.
  • potential – k1 by k2 matrix of potential values for the joint state of the two variables
Returns:

None

set_edge_tensor(edge_tensor)[source]

Set the tensor representation of the edge potentials :param edge_tensor: (max states) by (max states) by (num edges) tensor of the edge potentials :return: None

set_unary_factor(variable, potential)[source]

Set the potential function for the unary factor. Implicitly declare variable. Must be called before setting edge factors.

Parameters:
  • variable – name of the variable (can be any hashable object)
  • potential – length-k vector of log potential values for the respective k states
Returns:

None

set_unary_mat(unary_mat)[source]

Set the matrix representation of the unary potentials

Parameters:unary_mat – (num vars) by (max states) of unary potentials
Returns:None