class pystruct.models.DirectionalGridCRF(n_states=None, n_features=None, inference_method=None, neighborhood=4)[source]

CRF in which each direction of edges has their own set of parameters.

Pairwise potentials are not symmetric and are independend for each kind of edges. This leads to n_classes * n_features parameters for unary potentials and n_edge_features * n_classes ** 2 parameters for edge potentials. The number of edge-types is two for a 4-connected neighborhood (horizontal and vertical) or 4 for a 8 connected neighborhood (additionally two diagonals).

Unary evidence x is given as array of shape (width, height, n_features), labels y are given as array of shape (width, height). Grid sizes do not need to be constant over the dataset.


n_states : int, default=None

Number of states for all variables.

inference_method : string, default=None

Function to call do 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.

neighborhood : int, default=4

Neighborhood defining connection for each variable in the grid. Possible choices are 4 and 8.


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])
initialize(X, Y)
joint_feature(x, y) Feature vector associated with instance (x, y).
loss(y, y_hat)
loss_augmented_inference(x, y, w[, relaxed, ...])
__init__(n_states=None, n_features=None, inference_method=None, neighborhood=4)[source]
joint_feature(x, y)[source]

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 : ndarray, shape (width, height, n_features)

Unary evidence / input.

y : ndarray or tuple

Either y is an integral ndarray of shape (width, height), giving a complete labeling for x. Or it is the result of a linear programming relaxation. In this case, y=(unary_marginals, pariwise_marginals), where unary_marginals is an array of shape (width, height, n_states) and pairwise_marginals is an array of shape (n_states, n_states) of accumulated pairwise marginals.


p : ndarray, shape (size_joint_feature,)

Feature vector associated with state (x, y).