pystruct.models.GridCRF

class pystruct.models.GridCRF(n_states=2, n_features=None, inference_method='lp', neighborhood=4)

Pairwise CRF on a 2d grid.

Pairwise potentials are symmetric and the same for all edges. This leads to n_classes parameters for unary potentials and n_classes * (n_classes + 1) / 2 parameters for edge potentials.

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

Parameters :

n_states : int, default=2

Number of states for all variables.

inference_method : string, default=”lp”

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

  • ‘qpbo’ for QPBO + alpha expansion.
  • ‘dai’ for LibDAI bindings (which has another parameter).
  • ‘lp’ for Linear Programming relaxation using GLPK.
  • ‘ad3’ for AD3 dual decomposition.

neighborhood : int, default=4

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

Methods

batch_inference(X, w[, relaxed])
batch_loss(Y, Y_hat)
batch_loss_augmented_inference(X, Y, w[, ...])
batch_psi(X, Y[, Y_true])
continuous_loss(y, y_hat)
get_edges(x)
get_features(x)
get_pairwise_potentials(x, w) Computes pairwise potentials for x and w.
get_unary_potentials(x, w) Computes unary potentials for x and w.
inference(x, w[, relaxed, return_energy])
loss(y, y_hat)
loss_augmented_inference(x, y, w[, relaxed, ...])
max_loss(y)
psi(x, y) Feature vector associated with instance (x, y).
__init__(n_states=2, n_features=None, inference_method='lp', neighborhood=4)
get_pairwise_potentials(x, w)

Computes pairwise potentials for x and w.

Parameters :

x : tuple

Instance Representation.

w : ndarray, shape=(size_psi,)

Weight vector for CRF instance.

Returns :

pairwise : ndarray, shape=(n_states, n_states)

Pairwise weights.

get_unary_potentials(x, w)

Computes unary potentials for x and w.

Parameters :

x : tuple

Instance Representation.

w : ndarray, shape=(size_psi,)

Weight vector for CRF instance.

Returns :

unary : ndarray, shape=(n_states)

Unary weights.

psi(x, y)

Feature vector associated with instance (x, y).

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

Parameters :

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).

Returns :

p : ndarray, shape (size_psi,)

Feature vector associated with state (x, y).