pystruct.learners.StructuredPerceptron

class pystruct.learners.StructuredPerceptron(model, max_iter=100, verbose=0, batch=False, decay_exponent=0, decay_t0=10, average=False, n_jobs=1, logger=None)

Structured Perceptron training.

Implements a simple structured perceptron. The structured perceptron approximately minimizes the zero-one loss. Therefore the learning does not take model.loss into account. It is just shown to illustrate the learning progress. As the perceptron learning is not margin-based, the model does not need to provide loss_augmented_inference.
Parameters :

model : StructuredModel

Object containing model structure. Has to implement loss, inference.

max_iter : int (default=100)

Maximum number of passes over dataset to find constraints and update parameters.

verbose : int (default=0)

Verbosity

batch : bool (default=False)

Whether to do batch learning or online learning.

decay_exponent : float, default=0

Exponent for decaying learning rate. Effective learning rate is (t0 + t)** decay_exponent. Zero means no decay.

decay_t0 : float, default=10

Offset for decaying learning rate. Effective learning rate is (t0 + t)** decay_exponent. Zero means no decay.

logger : logger object.

Attributes

w nd-array, shape=(model.psi,) The learned weights of the SVM.
loss_curve_ list of float List of loss values after each pass thorugh the dataset.

Methods

fit(X, Y) Learn parameters using structured perceptron.
get_params([deep]) Get parameters for the estimator
predict(X) Predict output on examples in X.
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, verbose=0, batch=False, decay_exponent=0, decay_t0=10, average=False, n_jobs=1, logger=None)
fit(X, Y)

Learn parameters using structured perceptron.

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.

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 :