class pystruct.models.LatentGraphCRF(n_labels=None, n_features=None, n_states_per_label=2, inference_method=None)[source]

CRF with latent states for variables.

This is also called “hidden dynamics CRF”. For each output variable there is an additional variable which can encode additional states and interactions.


n_labels : int

Number of states of output variables.

n_featues : int or None (default=None).

Number of input features per input variable. None means it is equal to n_labels.

n_states_per_label : int or list (default=2)

Number of latent states associated with each observable state. Can be either an integer, which means the same number of hidden states will be used for all observable states, or a list of integers of length n_labels.

inference_method : string, default=”ad3”

Function to call to do inference and loss-augmented inference. Possible values are:

  • ‘max-product’ for max-product belief propagation.

    Recommended for chains an trees. Loopy belief propagatin in case of a general graph.

  • ‘lp’ for Linear Programming relaxation using cvxopt.

  • ‘ad3’ for AD3 dual decomposition.

  • ‘qpbo’ for QPBO + alpha expansion.

  • ‘ogm’ for OpenGM inference algorithms.


base_loss(y, y_hat)
batch_inference(X, w[, relaxed])
batch_joint_feature(X, Y[, Y_true])
batch_loss(Y, Y_hat)
batch_loss_augmented_inference(X, Y, w[, ...])
continuous_loss(y, y_hat)
inference(x, w[, relaxed, return_energy]) Inference for x using parameters w.
init_latent(X, Y)
initialize(X, Y)
joint_feature(x, y) Feature vector associated with instance (x, y).
latent(x, y, w)
loss(h, h_hat)
loss_augmented_inference(x, h, w[, relaxed, ...])
__init__(n_labels=None, n_features=None, n_states_per_label=2, inference_method=None)[source]
inference(x, w, relaxed=False, return_energy=False)

Inference for x using parameters w.

Finds (approximately) armin_y, joint_feature(x, y)) using self.inference_method.


x : tuple

Instance of a graph with unary evidence. x=(unaries, edges) unaries are an nd-array of shape (n_nodes, n_states), edges are an nd-array of shape (n_edges, 2)

w : ndarray, shape=(size_joint_feature,)

Parameters for the CRF energy function.

relaxed : bool, default=False

Whether relaxed inference should be performed. Only meaningful if inference method is ‘lp’ or ‘ad3’. By default fractional solutions are rounded. If relaxed=True, fractional solutions are returned directly.

return_energy : bool, default=False

Whether to return the energy of the solution (x, y) that was found.


y_pred : ndarray or tuple

By default an inter ndarray of shape=(width, height) of variable assignments for x is returned. If relaxed=True and inference_method is lp or ad3, a tuple (unary_marginals, pairwise_marginals) containing the relaxed inference result is returned. unary marginals is an array of shape (width, height, n_states), pairwise_marginals is an array of shape (n_states, n_states) of accumulated pairwise marginals.

joint_feature(x, y)

Feature vector associated with instance (x, y).

Feature representation joint_feature, such that the energy of the configuration (x, y) and a weight vector w is given by, joint_feature(x, y)).


x : tuple

Unary evidence.

y : ndarray or tuple

Either y is an integral ndarray, giving a complete labeling for x. Or it is the result of a linear programming relaxation. In this case, y=(unary_marginals, pariwise_marginals).


p : ndarray, shape (size_joint_feature,)

Feature vector associated with state (x, y).