pystruct.learners.NSlackSSVM

class pystruct.learners.NSlackSSVM(model, max_iter=100, C=1.0, check_constraints=True, verbose=0, positive_constraint=None, n_jobs=1, break_on_bad=False, show_loss_every=0, batch_size=100, tol=-10, inactive_threshold=1e-10, inactive_window=0, logger=None)

Structured SVM solver for the n-slack QP with l1 slack penalty.

Implements margin rescaled structural SVM using the n-slack formulation and cutting plane method, solved using CVXOPT. The optimization is restarted in each iteration.
Parameters :

model : StructuredModel

Object containing the model structure. Has to implement loss, inference and loss_augmented_inference.

max_iter : int

Maximum number of passes over dataset to find constraints.

C : float

Regularization parameter

check_constraints : bool (default=True)

Whether to check if the new “most violated constraint” is more violated than previous constraints. Helpful for stopping and debugging, but costly.

verbose : int (default=0)

Verbosity.

positive_constraint: list of ints

Indices of parmeters that are constraint to be positive.

break_on_bad: bool (default=False)

Whether to break (start debug mode) when inference was approximate.

n_jobs : int, default=1

Number of parallel jobs for inference. -1 means as many as cpus.

show_loss_every : int, default=0

Controlls how often the hamming loss is computed (for monitoring purposes). Zero means never, otherwise it will be computed very show_loss_every’th epoch.

batch_size : int, default=100

Number of constraints after which we solve the QP again. batch_size=-1 means that an update is performed only after going once over the whole training set.

tol : float, default=-10

Convergence tolerance. If dual objective decreases less than tol, learning is stopped. The default corresponds to ignoring the behavior of the dual objective and stop only if no more constraints can be found.

inactive_threshold : float, default=1e-5

Threshold for dual variable of a constraint to be considered inactive.

inactive_window : float, default=50

Window for measuring inactivity. If a constraint is inactive for inactive_window iterations, it will be pruned from the QP. If set to 0, no constraints will be removed.

Attributes

w nd-array, shape=(model.psi,)
The learned weights of the SVM.
old_solution : dict
The last solution found by the qp solver.
loss_curve_ list of float List of loss values if show_loss_every > 0.
objective_curve_ list of float Primal objective after each pass through the dataset.

Methods

fit(X, Y[, constraints, warm_start]) Learn parameters using cutting plane method.
get_params([deep]) Get parameters for the estimator
predict(X) Predict output on examples in X.
prune_constraints(constraints, a)
score(X, Y) Compute score as 1 - loss over whole data set.
set_params(**params) Set the parameters of the estimator.
__init__(model, max_iter=100, C=1.0, check_constraints=True, verbose=0, positive_constraint=None, n_jobs=1, break_on_bad=False, show_loss_every=0, batch_size=100, tol=-10, inactive_threshold=1e-10, inactive_window=0, logger=None)
fit(X, Y, constraints=None, warm_start=None)

Learn parameters using cutting plane method.

Parameters :

X : iterable

Traing instances. Contains the structured input objects. No requirement on the particular form of entries of X is made.

Y : iterable

Training labels. Contains the strctured labels for inputs in X. Needs to have the same length as X.

contraints : iterable

Known constraints for warm-starts. List of same length as X. Each entry is itself a list of constraints for a given instance x . Each constraint is of the form [y_hat, delta_psi, loss], where y_hat is a labeling, delta_psi = psi(x, y) - psi(x, y_hat) and loss is the loss for predicting y_hat instead of the true label y.

get_params(deep=True)

Get parameters for the estimator

Parameters :

deep: boolean, optional :

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns :

params : mapping of string to any

Parameter names mapped to their values.

predict(X)

Predict output on examples in X. Parameters ———- X : iterable

Traing instances. Contains the structured input objects.
Returns :

Y_pred : list

List of inference results for X using the learned parameters.

score(X, Y)

Compute score as 1 - loss over whole data set.

Returns the average accuracy (in terms of model.loss) over X and Y.

Parameters :

X : iterable

Evaluation data.

Y : iterable

True labels.

Returns :

score : float

Average of 1 - loss over training examples.

set_params(**params)

Set the parameters of the estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns :self :